]> pd.if.org Git - nbds/blob - test/perf_test.c
work in progress
[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 int num_threads_;
20 static volatile int start_, stop_, load_;
21 static map_t *map_;
22 static int get_range_, put_range_;
23 static size_t num_keys_;
24 static double load_time_;
25 static int duration_;
26
27 #define OP_SELECT_RANGE (1ULL << 20)
28
29 void *worker (void *arg) {
30     nbd_thread_init();
31
32     // Wait for all the worker threads to be ready.
33     (void)SYNC_ADD(&load_, -1);
34     do {} while (load_);
35
36     // Pre-load map
37     int n = num_keys_ / 2 / num_threads_;
38     for (int i = 0; i < n; ++i) {
39         map_key_t key = (nbd_rand() & (num_keys_ - 1)) + 1;
40         map_set(map_, key, key);
41     }
42
43     // Wait for all the worker threads to be done loading.
44     (void)SYNC_ADD(&start_, -1);
45     do {} while (start_);
46
47     uint64_t ops = 0;
48     while (!stop_) {
49         ++ops;
50         map_key_t key = (nbd_rand() & (num_keys_ - 1)) + 1;
51         map_key_t x = nbd_rand() & (OP_SELECT_RANGE - 1);
52         if (x < get_range_) {
53 #ifndef NDEBUG
54             map_val_t val =
55 #endif
56                 map_get(map_, key);
57 #ifdef TEST_STRING_KEYS
58             ASSERT(val == DOES_NOT_EXIST || ns_cmp((nstring_t *)key, (nstring_t *)val) == 0);
59 #else
60             ASSERT(val == DOES_NOT_EXIST || key == val);
61 #endif
62         } else if (x < put_range_) {
63             map_add(map_, key, key);
64         } else {
65             map_remove(map_, key);
66         }
67         rcu_update();
68     }
69
70     return (void *)ops;
71 }
72
73 uint64_t run_test (void) {
74     load_ = num_threads_ + 1;
75     start_ = num_threads_ + 1;
76
77     stop_ = 0;
78
79     pthread_t thread[MAX_NUM_THREADS];
80     for (int i = 0; i < num_threads_; ++i) {
81         int rc = pthread_create(thread + i, NULL, worker, (void*)(size_t)i);
82         if (rc != 0) { perror("pthread_create"); exit(rc); }
83     }
84
85     do { /* nothing */ } while (load_ != 1);
86     load_ = 0;
87
88     struct timeval tv1, tv2;
89     gettimeofday(&tv1, NULL);
90
91     do { /* nothing */ } while (start_ != 1);
92
93     gettimeofday(&tv2, NULL);
94     load_time_ = (double)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000000;
95
96     start_ = 0;
97     sleep(duration_);
98     stop_ = 1;
99
100     uint64_t ops = 0;
101     for (int i = 0; i < num_threads_; ++i) {
102         void *count;
103         pthread_join(thread[i], &count);
104         ops += (size_t)count;
105     }
106     return ops;
107 }
108
109 int main (int argc, char **argv) {
110     char* program_name = argv[0];
111
112     if (argc > 3) {
113         fprintf(stderr, "Usage: %s num_threads\n", program_name);
114         return -1;
115     }
116
117     num_threads_ = 2;
118     if (num_threads_ > MAX_NUM_THREADS) { num_threads_ = MAX_NUM_THREADS; }
119     if (argc > 1)
120     {
121         errno = 0;
122         num_threads_ = strtol(argv[1], NULL, 10);
123         if (errno) {
124             fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
125             return -1;
126         }
127         if (num_threads_ <= 0) {
128             fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
129             return -1;
130         }
131     }
132     if (num_threads_ > MAX_NUM_THREADS) {
133         fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
134         return -1;
135     }
136
137     int table_scale = 12;
138     if (argc > 2) {
139         table_scale = strtol(argv[2], NULL, 10);
140         if (errno) {
141             fprintf(stderr, "%s: Invalid argument for the scale of the collection\n", program_name);
142             return -1;
143         }
144         table_scale = strtol(argv[2], NULL, 10);
145         if (table_scale < 0 || table_scale > 36) {
146             fprintf(stderr, "%s: The scale of the collection must be between 0 and 36\n", program_name);
147             return -1;
148         }
149     }
150
151     int read_ratio = 90;
152     int put_ratio = 50;
153     get_range_ = (int)((double)OP_SELECT_RANGE / 100 * read_ratio);
154     put_range_ = get_range_ + (int)(((double)OP_SELECT_RANGE - get_range_) / 100 * put_ratio);
155
156     nbd_thread_init();
157     static const map_impl_t *map_types[] = { &MAP_IMPL_HT };
158     for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
159 #ifdef TEST_STRING_KEYS
160         map_ = map_alloc(map_types[i], &DATATYPE_NSTRING);
161 #else
162         map_ = map_alloc(map_types[i], NULL);
163 #endif
164
165         num_keys_ = 1ULL << table_scale;
166
167         duration_ = 1 + table_scale/4;
168         double mops_per_sec = (double)run_test() / 1000000.0 / duration_;
169
170         printf("Threads:%-2d  Size:2^%-2d  load time:%-4.2f  Mops/s:%-4.2f  per-thread:%-4.2f  ",
171                 num_threads_, table_scale, load_time_, mops_per_sec, mops_per_sec/num_threads_);
172         map_print(map_, FALSE);
173         fflush(stdout);
174
175         map_free(map_);
176     }
177
178     return 0;
179 }