]> pd.if.org Git - nbds/blob - test/perf_test.c
add perf test driver
[nbds] / test / perf_test.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <unistd.h>
4 #include <pthread.h>
5 #include <sys/time.h>
6
7 #include "common.h"
8 #include "nstring.h"
9 #include "runtime.h"
10 #include "map.h"
11 #include "rcu.h"
12 #include "mem.h"
13 #include "list.h"
14 #include "skiplist.h"
15 #include "hashtable.h"
16
17 //#define TEST_STRING_KEYS
18
19 static volatile int wait_;
20 static volatile int stop_;
21 static int num_threads_;
22 static int duration_;
23 static map_t *map_;
24 static int get_range_;
25 static int put_range_;
26 static size_t num_keys_;
27 static map_key_t *keys_ = NULL;
28 static int ops_[MAX_NUM_THREADS] = {};
29
30 #define FOO (1ULL << 20)
31
32 void *worker (void *arg) {
33     int tid = (int)(size_t)arg;
34     uint64_t s = nbd_rand_seed(tid);
35     int get_ops = 0, put_ops = 0, del_ops = 0;
36
37     // Wait for all the worker threads to be ready.
38     (void)SYNC_ADD(&wait_, -1);
39     do {} while (wait_); 
40
41     while (!stop_) {
42         map_key_t key = keys_[ nbd_next_rand(&s) & (num_keys_ - 1) ];
43         uint32_t x = nbd_next_rand(&s) & (FOO - 1);
44         if (x < get_range_) {
45 #ifndef NDEBUG
46             map_val_t val = 
47 #endif
48                 map_get(map_, key);
49 #ifdef TEST_STRING_KEYS
50             ASSERT(val == DOES_NOT_EXIST || ns_cmp((nstring_t *)key, (nstring_t *)val) == 0);
51 #else
52             ASSERT(val == DOES_NOT_EXIST || key == val);
53 #endif
54             get_ops++;
55         } else if (x < put_range_) {
56             map_add(map_, key, key);
57             put_ops++;
58         } else {
59             map_remove(map_, key);
60             del_ops++;
61         }
62         rcu_update();
63     }
64
65     ops_[tid] = get_ops + put_ops + del_ops;
66
67     return NULL;
68 }
69
70 int run_test (void) {
71     int ops;
72     wait_ = num_threads_ + 1;
73
74     // Quicky sanity check
75     int n = 100;
76     if (num_keys_ < n) { n = num_keys_; }
77     for (int i = 0; i < n; ++i) {
78         map_set(map_, keys_[i], keys_[i]);
79         for(int j = 0; j < i; ++j) {
80 #ifdef TEST_STRING_KEYS
81             ASSERT(ns_cmp((nstring_t *)map_get(map_, keys_[i]), (nstring_t *)keys_[i]) == 0);
82 #else
83             ASSERT(map_get(map_, keys_[i]) == keys_[i]);
84 #endif
85         }
86     }
87
88     stop_ = 0;
89
90     pthread_t thread[MAX_NUM_THREADS];
91     for (int i = 0; i < num_threads_; ++i) {
92         int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
93         if (rc != 0) { perror("pthread_create"); exit(rc); }
94     }
95
96     do { /* nothing */ } while (wait_ != 1);
97
98     wait_ = 0;
99     sleep(duration_);
100     stop_ = 1;
101
102     for (int i = 0; i < num_threads_; ++i) {
103         pthread_join(thread[i], NULL);
104     }
105     ops = 0;
106     for (int i = 0; i < num_threads_; ++i) {
107         ops += ops_[i];
108     }
109     return ops;
110 }
111
112 int main (int argc, char **argv) {
113     char* program_name = argv[0];
114
115     if (argc > 3) {
116         fprintf(stderr, "Usage: %s num_threads\n", program_name);
117         return -1;
118     }
119
120     num_threads_ = 2;
121     if (argc > 1)
122     {
123         errno = 0;
124         num_threads_ = strtol(argv[1], NULL, 10);
125         if (errno) {
126             fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
127             return -1;
128         }
129         if (num_threads_ <= 0) {
130             fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
131             return -1;
132         }
133         if (num_threads_ > MAX_NUM_THREADS) {
134             fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
135             return -1;
136         }
137     }
138
139     int table_scale = 12;
140     if (argc > 2) {
141         table_scale = strtol(argv[2], NULL, 10);
142         if (errno) {
143             fprintf(stderr, "%s: Invalid argument for the scale of the collection\n", program_name);
144             return -1;
145         }
146         table_scale = strtol(argv[2], NULL, 10);
147         if (table_scale < 0 || table_scale > 31) {
148             fprintf(stderr, "%s: The scale of the collection must be between 0 and 31\n", program_name);
149             return -1;
150         }
151     }
152
153
154     int read_ratio = 90;
155     int put_ratio = 50;
156     get_range_ = (int)((double)FOO / 100 * read_ratio);
157     put_range_ = get_range_ + (int)(((double)FOO - get_range_) / 100 * put_ratio);
158
159     static const map_impl_t *map_types[] = { &MAP_IMPL_SL };
160     for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
161 #ifdef TEST_STRING_KEYS
162         map_ = map_alloc(map_types[i], &DATATYPE_NSTRING);
163 #else
164         map_ = map_alloc(map_types[i], NULL);
165 #endif
166
167         // Do some warmup
168         num_keys_ = 1ULL << table_scale;
169         keys_ = nbd_malloc(sizeof(map_key_t) * num_keys_);
170         ASSERT(keys_ != NULL);
171         for (uint64_t j = 0; j < num_keys_; ++j) {
172 #ifdef TEST_STRING_KEYS
173             char tmp[64];
174             snprintf(tmp, sizeof(tmp), "%dabc%d", j, j*17+123);
175             int n = strlen(tmp);
176             keys_[j] = ns_alloc(n);
177             memcpy(keys_[j], tmp, n);
178 #else
179             keys_[j] = j*17+123;
180 #endif
181         }
182
183         duration_ = 10;
184         int num_trials = 1;
185         int ops = 0;
186         for (int i = 0; i < num_trials; ++i) {
187             ops += run_test();
188         }
189         double ops_per_sec = ops / num_trials / duration_;
190
191         //map_print(map_);
192         printf("Threads:%-2d  Size:2^%-2d  Mops/Sec:%-4.3g  per-thread:%-4.3g\n\n", 
193                 num_threads_, table_scale, ops_per_sec/1000000, ops_per_sec/num_threads_/1000000);
194         fflush(stdout);
195
196         map_free(map_);
197     }
198
199     return 0;
200 }