11 #define NUM_ITERATIONS 10000000
13 //#define TEST_STRING_KEYS
15 static volatile int wait_;
16 static long num_threads_;
19 void *worker (void *arg) {
21 // Wait for all the worker threads to be ready.
25 #ifdef TEST_STRING_KEYS
26 nstring_t *key_str = ns_alloc(10);
29 for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
30 unsigned r = nbd_rand();
31 uint64_t key = r & 0xF;
32 #ifdef TEST_STRING_KEYS
33 key_str->len = sprintf(key_str->data, "%llX", key) + 1;
34 assert(key_str->len <= 10);
36 map_set(map_, key_str, 1);
38 map_remove(map_, key_str);
42 map_set(map_, (void *)(key + 1), 1);
44 map_remove(map_, (void *)(key + 1));
54 int main (int argc, char **argv) {
56 lwt_set_trace_level("l3");
58 char* program_name = argv[0];
59 pthread_t thread[MAX_NUM_THREADS];
62 fprintf(stderr, "Usage: %s num_threads\n", program_name);
70 num_threads_ = strtol(argv[1], NULL, 10);
72 fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
75 if (num_threads_ <= 0) {
76 fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
79 if (num_threads_ > MAX_NUM_THREADS) {
80 fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
85 map_type_t map_types[] = { MAP_TYPE_LIST, MAP_TYPE_SKIPLIST, MAP_TYPE_HASHTABLE };
86 for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
87 #ifdef TEST_STRING_KEYS
88 map_ = map_alloc(map_types[i], (cmp_fun_t)ns_cmp, (hash_fun_t)ns_hash, (clone_fun_t)ns_dup);
90 map_ = map_alloc(map_types[i], NULL, NULL, NULL);
93 struct timeval tv1, tv2;
94 gettimeofday(&tv1, NULL);
98 for (int i = 0; i < num_threads_; ++i) {
99 int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
100 if (rc != 0) { perror("pthread_create"); return rc; }
103 for (int i = 0; i < num_threads_; ++i) {
104 pthread_join(thread[i], NULL);
107 gettimeofday(&tv2, NULL);
108 int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
110 printf("Th:%ld Time:%dms\n\n", num_threads_, ms);