]> pd.if.org Git - nbds/blob - test/map_test1.c
work in progress
[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 "rcu.h"
11 #include "list.h"
12 #include "skiplist.h"
13 #include "hashtable.h"
14
15 #define NUM_ITERATIONS 10000000
16
17 //#define TEST_STRING_KEYS
18
19 static volatile int wait_;
20 static long num_threads_;
21 static map_t *map_;
22
23 void *worker (void *arg) {
24     nbd_thread_init();
25
26     // Wait for all the worker threads to be ready.
27     (void)SYNC_ADD(&wait_, -1);
28     do {} while (wait_); 
29
30 #ifdef TEST_STRING_KEYS
31     nstring_t *key_str = ns_alloc(10);
32 #endif
33
34     for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
35         unsigned r = nbd_rand();
36         int key = r & 0xF;
37 #ifdef TEST_STRING_KEYS
38         key_str->len = sprintf(key_str->data, "%X", key) + 1;
39         assert(key_str->len <= 10);
40         if (r & (1 << 8)) {
41             map_set(map_, (map_key_t)key_str, 1);
42         } else {
43             map_remove(map_, (map_key_t)key_str);
44         }
45 #else
46         if (r & (1 << 8)) {
47             map_set(map_, (map_key_t)(key + 1), 1);
48         } else {
49             map_remove(map_, (map_key_t)(key + 1));
50         }
51 #endif
52
53         rcu_update();
54     }
55
56     return NULL;
57 }
58
59 int main (int argc, char **argv) {
60     nbd_thread_init();
61     lwt_set_trace_level("r0m3s3");
62
63     char* program_name = argv[0];
64     pthread_t thread[MAX_NUM_THREADS];
65
66     if (argc > 2) {
67         fprintf(stderr, "Usage: %s num_threads\n", program_name);
68         return -1;
69     }
70
71     num_threads_ = MAX_NUM_THREADS;
72     if (argc == 2)
73     {
74         errno = 0;
75         num_threads_ = strtol(argv[1], NULL, 10);
76         if (errno) {
77             fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
78             return -1;
79         }
80         if (num_threads_ <= 0) {
81             fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
82             return -1;
83         }
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);
86             return -1;
87         }
88     }
89
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);
94 #else
95         map_ = map_alloc(map_types[i], NULL);
96 #endif
97
98         struct timeval tv1, tv2;
99         gettimeofday(&tv1, NULL);
100
101         wait_ = num_threads_;
102
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; }
106         }
107
108         for (int i = 0; i < num_threads_; ++i) {
109             pthread_join(thread[i], NULL);
110         }
111
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);
116         fflush(stdout);
117     }
118
119     return 0;
120 }