15 #include "hashtable.h"
17 //#define TEST_STRING_KEYS
19 static volatile int wait_;
20 static volatile int stop_;
21 static long num_threads_;
23 static int get_range_;
24 static int put_range_;
26 static map_key_t *keys_ = NULL;
27 static uint64_t times_[MAX_NUM_THREADS] = {};
28 static int ops_[MAX_NUM_THREADS] = {};
30 void *worker (void *arg) {
31 int tid = (int)(size_t)arg;
32 uint64_t s = nbd_rand_seed(tid);
33 int get_ops = 0, put_ops = 0, del_ops = 0;
35 // Wait for all the worker threads to be ready.
36 (void)SYNC_ADD(&wait_, -1);
39 uint64_t t1 = rdtsc();
42 int r = nbd_next_rand(&s);
43 int x = r & ( (1 << 20) - 1 );
44 int i = nbd_next_rand(&s) & (num_keys_ - 1);
45 map_key_t key = keys_[i];
47 map_val_t val = map_get(map_, key);
48 #ifdef TEST_STRING_KEYS
49 ASSERT(val == DOES_NOT_EXIST || ns_cmp((nstring_t *)key, (nstring_t *)val) == 0);
51 ASSERT(val == DOES_NOT_EXIST || key == val);
54 } else if (x < put_range_) {
55 map_add(map_, key, key);
58 map_remove(map_, key);
64 times_[tid] = rdtsc() - t1;
65 ops_[tid] = get_ops + put_ops + del_ops;
70 void run_test (void) {
71 wait_ = num_threads_ + 1;
73 // Quicky sanity check
75 if (num_keys_ < n) { n = num_keys_; }
76 for (int i = 0; i < n; ++i) {
77 map_set(map_, keys_[i], keys_[i]);
78 for(int j = 0; j < i; ++j) {
79 #ifdef TEST_STRING_KEYS
80 ASSERT(ns_cmp((nstring_t *)map_get(map_, keys_[i]), (nstring_t *)keys_[i]) == 0);
82 ASSERT(map_get(map_, keys_[i]) == keys_[i]);
89 pthread_t thread[MAX_NUM_THREADS];
90 for (int i = 0; i < num_threads_; ++i) {
91 int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
92 if (rc != 0) { perror("pthread_create"); exit(rc); }
95 do { /* nothing */ } while (wait_ != 1);
101 for (int i = 0; i < num_threads_; ++i) {
102 pthread_join(thread[i], NULL);
106 int main (int argc, char **argv) {
107 char* program_name = argv[0];
110 fprintf(stderr, "Usage: %s num_threads\n", program_name);
118 num_threads_ = strtol(argv[1], NULL, 10);
120 fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
123 if (num_threads_ <= 0) {
124 fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
127 if (num_threads_ > MAX_NUM_THREADS) {
128 fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
134 int table_scale = 10;
136 get_range_ = (read_ratio << 20) / 100;
137 put_range_ = (((1 << 20) - get_range_) >> 1) + get_range_;
139 static const map_impl_t *map_types[] = { &MAP_IMPL_HT };
140 for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
141 #ifdef TEST_STRING_KEYS
142 map_ = map_alloc(map_types[i], &DATATYPE_NSTRING);
144 map_ = map_alloc(map_types[i], NULL);
148 num_keys_ = 1 << table_scale;
149 keys_ = nbd_malloc(sizeof(map_key_t) * num_keys_);
150 for (int j = 0; j < num_keys_; ++j) {
151 #ifdef TEST_STRING_KEYS
153 snprintf(tmp, sizeof(tmp), "%dabc%d", j, j*17+123);
155 keys_[j] = ns_alloc(n);
156 memcpy(keys_[j], tmp, n);
162 struct timeval tv1, tv2;
163 gettimeofday(&tv1, NULL);
166 for (int i = 0; i < num_trials; ++i) {
170 gettimeofday(&tv2, NULL);
171 int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
173 printf("Th:%ld Time:%dms\n\n", num_threads_, ms);