]> pd.if.org Git - nbds/blob - test/map_test1.c
refactor header files
[nbds] / test / map_test1.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <pthread.h>
4 #include <sys/time.h>
5
6 #include "common.h"
7 #include "nstring.h"
8 #include "runtime.h"
9 #include "map.h"
10 #include "list.h"
11 #include "skiplist.h"
12 #include "hashtable.h"
13
14 #define NUM_ITERATIONS 10000000
15
16 #define TEST_STRING_KEYS
17
18 static volatile int wait_;
19 static long num_threads_;
20 static map_t *map_;
21
22 void *worker (void *arg) {
23
24     // Wait for all the worker threads to be ready.
25     SYNC_ADD(&wait_, -1);
26     do {} while (wait_); 
27
28 #ifdef TEST_STRING_KEYS
29         nstring_t *key_str = ns_alloc(10);
30 #endif
31
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);
38         if (r & (1 << 8)) {
39             map_set(map_, key_str, 1);
40         } else {
41             map_remove(map_, key_str);
42         }
43 #else
44         if (r & (1 << 8)) {
45             map_set(map_, (void *)(key + 1), 1);
46         } else {
47             map_remove(map_, (void *)(key + 1));
48         }
49 #endif
50
51         rcu_update();
52     }
53
54     return NULL;
55 }
56
57 int main (int argc, char **argv) {
58     nbd_init();
59     lwt_set_trace_level("l3");
60
61     char* program_name = argv[0];
62     pthread_t thread[MAX_NUM_THREADS];
63
64     if (argc > 2) {
65         fprintf(stderr, "Usage: %s num_threads\n", program_name);
66         return -1;
67     }
68
69     num_threads_ = 2;
70     if (argc == 2)
71     {
72         errno = 0;
73         num_threads_ = strtol(argv[1], NULL, 10);
74         if (errno) {
75             fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
76             return -1;
77         }
78         if (num_threads_ <= 0) {
79             fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
80             return -1;
81         }
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);
84             return -1;
85         }
86     }
87
88     map_type_t map_types[] = { MAP_TYPE_LIST, MAP_TYPE_SKIPLIST, MAP_TYPE_HASHTABLE };
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);
92 #else
93         map_ = map_alloc(map_types[i], NULL);
94 #endif
95
96         struct timeval tv1, tv2;
97         gettimeofday(&tv1, NULL);
98
99         wait_ = num_threads_;
100
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; }
104         }
105
106         for (int i = 0; i < num_threads_; ++i) {
107             pthread_join(thread[i], NULL);
108         }
109
110         gettimeofday(&tv2, NULL);
111         int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
112         map_print(map_);
113         printf("Th:%ld Time:%dms\n\n", num_threads_, ms);
114         fflush(stdout);
115     }
116
117     return 0;
118 }