12 #include "hashtable.h"
14 #define NUM_ITERATIONS 10000000
16 #define TEST_STRING_KEYS
18 static volatile int wait_;
19 static long num_threads_;
22 void *worker (void *arg) {
24 // Wait for all the worker threads to be ready.
28 #ifdef TEST_STRING_KEYS
29 nstring_t *key_str = ns_alloc(10);
32 for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
33 unsigned r = nbd_rand();
34 uint64_t key = r & 0xF;
35 #ifdef TEST_STRING_KEYS
36 key_str->len = sprintf(key_str->data, "%llX", key) + 1;
37 assert(key_str->len <= 10);
39 map_set(map_, key_str, 1);
41 map_remove(map_, key_str);
45 map_set(map_, (void *)(key + 1), 1);
47 map_remove(map_, (void *)(key + 1));
57 int main (int argc, char **argv) {
59 lwt_set_trace_level("l3");
61 char* program_name = argv[0];
62 pthread_t thread[MAX_NUM_THREADS];
65 fprintf(stderr, "Usage: %s num_threads\n", program_name);
73 num_threads_ = strtol(argv[1], NULL, 10);
75 fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
78 if (num_threads_ <= 0) {
79 fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
82 if (num_threads_ > MAX_NUM_THREADS) {
83 fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
88 static const map_impl_t *map_types[] = { &ll_map_impl, &sl_map_impl, &ht_map_impl };
89 for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
90 #ifdef TEST_STRING_KEYS
91 map_ = map_alloc(map_types[i], &DATATYPE_NSTRING);
93 map_ = map_alloc(map_types[i], NULL);
96 struct timeval tv1, tv2;
97 gettimeofday(&tv1, NULL);
101 for (int i = 0; i < num_threads_; ++i) {
102 int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
103 if (rc != 0) { perror("pthread_create"); return rc; }
106 for (int i = 0; i < num_threads_; ++i) {
107 pthread_join(thread[i], NULL);
110 gettimeofday(&tv2, NULL);
111 int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
113 printf("Th:%ld Time:%dms\n\n", num_threads_, ms);