15 #include "hashtable.h"
17 //#define TEST_STRING_KEYS
19 static int num_threads_;
20 static volatile int start_, stop_, load_;
22 static int get_range_, put_range_;
23 static size_t num_keys_;
24 static double load_time_;
27 #define OP_SELECT_RANGE (1ULL << 20)
29 void *worker (void *arg) {
30 volatile uint64_t ops = 0;
32 // Wait for all the worker threads to be ready.
33 (void)SYNC_ADD(&load_, -1);
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);
43 // Wait for all the worker threads to be done loading.
44 (void)SYNC_ADD(&start_, -1);
49 map_key_t key = (nbd_rand() & (num_keys_ - 1)) + 1;
50 map_key_t x = nbd_rand() & (OP_SELECT_RANGE - 1);
56 #ifdef TEST_STRING_KEYS
57 ASSERT(val == DOES_NOT_EXIST || ns_cmp((nstring_t *)key, (nstring_t *)val) == 0);
59 ASSERT(val == DOES_NOT_EXIST || key == val);
61 } else if (x < put_range_) {
62 map_add(map_, key, key);
64 map_remove(map_, key);
72 uint64_t run_test (void) {
73 load_ = num_threads_ + 1;
74 start_ = num_threads_ + 1;
78 pthread_t thread[MAX_NUM_THREADS];
79 for (int i = 0; i < num_threads_; ++i) {
80 int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
81 if (rc != 0) { perror("pthread_create"); exit(rc); }
84 do { /* nothing */ } while (load_ != 1);
87 struct timeval tv1, tv2;
88 gettimeofday(&tv1, NULL);
90 do { /* nothing */ } while (start_ != 1);
92 gettimeofday(&tv2, NULL);
93 load_time_ = (double)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000000;
100 for (int i = 0; i < num_threads_; ++i) {
102 pthread_join(thread[i], &count);
103 ops += (size_t)count;
108 int main (int argc, char **argv) {
109 char* program_name = argv[0];
112 fprintf(stderr, "Usage: %s num_threads\n", program_name);
117 if (num_threads_ > MAX_NUM_THREADS) { num_threads_ = MAX_NUM_THREADS; }
121 num_threads_ = strtol(argv[1], NULL, 10);
123 fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
126 if (num_threads_ <= 0) {
127 fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
131 if (num_threads_ > MAX_NUM_THREADS) {
132 fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
136 int table_scale = 12;
138 table_scale = strtol(argv[2], NULL, 10);
140 fprintf(stderr, "%s: Invalid argument for the scale of the collection\n", program_name);
143 table_scale = strtol(argv[2], NULL, 10);
144 if (table_scale < 0 || table_scale > 36) {
145 fprintf(stderr, "%s: The scale of the collection must be between 0 and 36\n", program_name);
152 get_range_ = (int)((double)OP_SELECT_RANGE / 100 * read_ratio);
153 put_range_ = get_range_ + (int)(((double)OP_SELECT_RANGE - get_range_) / 100 * put_ratio);
155 static const map_impl_t *map_types[] = { &MAP_IMPL_HT };
156 for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
157 #ifdef TEST_STRING_KEYS
158 map_ = map_alloc(map_types[i], &DATATYPE_NSTRING);
160 map_ = map_alloc(map_types[i], NULL);
163 num_keys_ = 1ULL << table_scale;
165 duration_ = 1 + table_scale/4;
166 double mops_per_sec = (double)run_test() / 1000000.0 / duration_;
168 printf("Threads:%-2d Size:2^%-2d load time:%-4.2f Mops/s:%-4.2f per-thread:%-4.2f ",
169 num_threads_, table_scale, load_time_, mops_per_sec, mops_per_sec/num_threads_);
170 map_print(map_, FALSE);