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) {
26 // Wait for all the worker threads to be ready.
27 (void)SYNC_ADD(&wait_, -1);
30 #ifdef TEST_STRING_KEYS
31 nstring_t *key_str = ns_alloc(10);
34 for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
35 unsigned r = nbd_rand();
37 #ifdef TEST_STRING_KEYS
38 key_str->len = sprintf(key_str->data, "%X", key) + 1;
39 assert(key_str->len <= 10);
41 map_set(map_, (map_key_t)key_str, 1);
43 map_remove(map_, (map_key_t)key_str);
47 map_set(map_, (map_key_t)(key + 1), 1);
49 map_remove(map_, (map_key_t)(key + 1));
59 int main (int argc, char **argv) {
61 lwt_set_trace_level("r0m3s3");
63 char* program_name = argv[0];
64 pthread_t thread[MAX_NUM_THREADS];
67 fprintf(stderr, "Usage: %s num_threads\n", program_name);
71 num_threads_ = MAX_NUM_THREADS;
75 num_threads_ = strtol(argv[1], NULL, 10);
77 fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
80 if (num_threads_ <= 0) {
81 fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
84 if (num_threads_ > MAX_NUM_THREADS) {
85 fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
90 static const map_impl_t *map_types[] = { &MAP_IMPL_LL, &MAP_IMPL_SL, &MAP_IMPL_HT };
91 for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
92 #ifdef TEST_STRING_KEYS
93 map_ = map_alloc(map_types[i], &DATATYPE_NSTRING);
95 map_ = map_alloc(map_types[i], NULL);
98 struct timeval tv1, tv2;
99 gettimeofday(&tv1, NULL);
101 wait_ = num_threads_;
103 for (int i = 0; i < num_threads_; ++i) {
104 int rc = pthread_create(thread + i, NULL, worker, (void*)(size_t)i);
105 if (rc != 0) { perror("pthread_create"); return rc; }
108 for (int i = 0; i < num_threads_; ++i) {
109 pthread_join(thread[i], NULL);
112 gettimeofday(&tv2, NULL);
113 int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
114 map_print(map_, FALSE);
115 printf("Th:%ld Time:%dms\n\n", num_threads_, ms);