13 #include "hashtable.h"
15 #define NUM_ITERATIONS 10000000
17 //#define TEST_STRING_KEYS
19 static volatile int wait_;
20 static long num_threads_;
23 void *worker (void *arg) {
25 // Wait for all the worker threads to be ready.
29 #ifdef TEST_STRING_KEYS
30 nstring_t *key_str = ns_alloc(10);
33 for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
34 unsigned r = nbd_rand();
36 #ifdef TEST_STRING_KEYS
37 key_str->len = sprintf(key_str->data, "%X", key) + 1;
38 assert(key_str->len <= 10);
40 map_set(map_, (map_key_t)key_str, 1);
42 map_remove(map_, (map_key_t)key_str);
46 map_set(map_, (map_key_t)(key + 1), 1);
48 map_remove(map_, (map_key_t)(key + 1));
58 int main (int argc, char **argv) {
59 lwt_set_trace_level("r0m0l3");
61 char* program_name = argv[0];
62 pthread_t thread[MAX_NUM_THREADS];
65 fprintf(stderr, "Usage: %s num_threads\n", program_name);
69 num_threads_ = MAX_NUM_THREADS;
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);