]> pd.if.org Git - nbds/blobdiff - test/perf_test.c
improved perf_test to measure steady state behavior
[nbds] / test / perf_test.c
index cc55ce04797b5741270920c0348d57795a28ef3f..1ea0bcb3eb11f831c9ca50991213121b0e659cf8 100644 (file)
 
 //#define TEST_STRING_KEYS
 
-static volatile int wait_;
-static volatile int stop_;
 static int num_threads_;
-static int duration_;
+static volatile int start_, stop_, load_;
 static map_t *map_;
-static int get_range_;
-static int put_range_;
+static int get_range_, put_range_;
 static size_t num_keys_;
-static map_key_t *keys_ = NULL;
-static int ops_[MAX_NUM_THREADS] = {};
+static double load_time_;
+static int duration_;
 
-#define FOO (1ULL << 20)
+#define OP_SELECT_RANGE (1ULL << 20)
 
 void *worker (void *arg) {
-    int tid = (int)(size_t)arg;
-    uint64_t s = nbd_rand_seed(tid);
-    int get_ops = 0, put_ops = 0, del_ops = 0;
+    volatile uint64_t ops = 0;
 
     // Wait for all the worker threads to be ready.
-    (void)SYNC_ADD(&wait_, -1);
-    do {} while (wait_); 
+    (void)SYNC_ADD(&load_, -1);
+    do {} while (load_);
+
+    // Pre-load map
+    int n = num_keys_ / 2 / num_threads_;
+    for (int i = 0; i < n; ++i) {
+        map_key_t key = (nbd_rand() & (num_keys_ - 1)) + 1;
+        map_set(map_, key, key);
+    }
+
+    // Wait for all the worker threads to be done loading.
+    (void)SYNC_ADD(&start_, -1);
+    do {} while (start_);
 
     while (!stop_) {
-        map_key_t key = keys_[ nbd_next_rand(&s) & (num_keys_ - 1) ];
-        uint32_t x = nbd_next_rand(&s) & (FOO - 1);
+        ++ops;
+        map_key_t key = (nbd_rand() & (num_keys_ - 1)) + 1;
+        map_key_t x = nbd_rand() & (OP_SELECT_RANGE - 1);
         if (x < get_range_) {
 #ifndef NDEBUG
-            map_val_t val = 
+            map_val_t val =
 #endif
                 map_get(map_, key);
 #ifdef TEST_STRING_KEYS
@@ -51,39 +58,20 @@ void *worker (void *arg) {
 #else
             ASSERT(val == DOES_NOT_EXIST || key == val);
 #endif
-            get_ops++;
         } else if (x < put_range_) {
             map_add(map_, key, key);
-            put_ops++;
         } else {
             map_remove(map_, key);
-            del_ops++;
         }
         rcu_update();
     }
 
-    ops_[tid] = get_ops + put_ops + del_ops;
-
-    return NULL;
+    return (void *)ops;
 }
 
-int run_test (void) {
-    int ops;
-    wait_ = num_threads_ + 1;
-
-    // Quicky sanity check
-    int n = 100;
-    if (num_keys_ < n) { n = num_keys_; }
-    for (int i = 0; i < n; ++i) {
-        map_set(map_, keys_[i], keys_[i]);
-        for(int j = 0; j < i; ++j) {
-#ifdef TEST_STRING_KEYS
-            ASSERT(ns_cmp((nstring_t *)map_get(map_, keys_[i]), (nstring_t *)keys_[i]) == 0);
-#else
-            ASSERT(map_get(map_, keys_[i]) == keys_[i]);
-#endif
-        }
-    }
+uint64_t run_test (void) {
+    load_ = num_threads_ + 1;
+    start_ = num_threads_ + 1;
 
     stop_ = 0;
 
@@ -93,18 +81,26 @@ int run_test (void) {
         if (rc != 0) { perror("pthread_create"); exit(rc); }
     }
 
-    do { /* nothing */ } while (wait_ != 1);
+    do { /* nothing */ } while (load_ != 1);
+    load_ = 0;
+
+    struct timeval tv1, tv2;
+    gettimeofday(&tv1, NULL);
 
-    wait_ = 0;
+    do { /* nothing */ } while (start_ != 1);
+
+    gettimeofday(&tv2, NULL);
+    load_time_ = (double)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000000;
+
+    start_ = 0;
     sleep(duration_);
     stop_ = 1;
 
+    uint64_t ops = 0;
     for (int i = 0; i < num_threads_; ++i) {
-        pthread_join(thread[i], NULL);
-    }
-    ops = 0;
-    for (int i = 0; i < num_threads_; ++i) {
-        ops += ops_[i];
+        void *count;
+        pthread_join(thread[i], &count);
+        ops += (size_t)count;
     }
     return ops;
 }
@@ -118,6 +114,7 @@ int main (int argc, char **argv) {
     }
 
     num_threads_ = 2;
+    if (num_threads_ > MAX_NUM_THREADS) { num_threads_ = MAX_NUM_THREADS; }
     if (argc > 1)
     {
         errno = 0;
@@ -130,10 +127,10 @@ int main (int argc, char **argv) {
             fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
             return -1;
         }
-        if (num_threads_ > MAX_NUM_THREADS) {
-            fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
-            return -1;
-        }
+    }
+    if (num_threads_ > MAX_NUM_THREADS) {
+        fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
+        return -1;
     }
 
     int table_scale = 12;
@@ -144,19 +141,18 @@ int main (int argc, char **argv) {
             return -1;
         }
         table_scale = strtol(argv[2], NULL, 10);
-        if (table_scale < 0 || table_scale > 31) {
-            fprintf(stderr, "%s: The scale of the collection must be between 0 and 31\n", program_name);
+        if (table_scale < 0 || table_scale > 36) {
+            fprintf(stderr, "%s: The scale of the collection must be between 0 and 36\n", program_name);
             return -1;
         }
     }
 
-
     int read_ratio = 90;
     int put_ratio = 50;
-    get_range_ = (int)((double)FOO / 100 * read_ratio);
-    put_range_ = get_range_ + (int)(((double)FOO - get_range_) / 100 * put_ratio);
+    get_range_ = (int)((double)OP_SELECT_RANGE / 100 * read_ratio);
+    put_range_ = get_range_ + (int)(((double)OP_SELECT_RANGE - get_range_) / 100 * put_ratio);
 
-    static const map_impl_t *map_types[] = { &MAP_IMPL_SL };
+    static const map_impl_t *map_types[] = { &MAP_IMPL_HT };
     for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
 #ifdef TEST_STRING_KEYS
         map_ = map_alloc(map_types[i], &DATATYPE_NSTRING);
@@ -164,33 +160,14 @@ int main (int argc, char **argv) {
         map_ = map_alloc(map_types[i], NULL);
 #endif
 
-        // Do some warmup
         num_keys_ = 1ULL << table_scale;
-        keys_ = nbd_malloc(sizeof(map_key_t) * num_keys_);
-        ASSERT(keys_ != NULL);
-        for (uint64_t j = 0; j < num_keys_; ++j) {
-#ifdef TEST_STRING_KEYS
-            char tmp[64];
-            snprintf(tmp, sizeof(tmp), "%dabc%d", j, j*17+123);
-            int n = strlen(tmp);
-            keys_[j] = ns_alloc(n);
-            memcpy(keys_[j], tmp, n);
-#else
-            keys_[j] = j*17+123;
-#endif
-        }
 
-        duration_ = 10;
-        int num_trials = 1;
-        int ops = 0;
-        for (int i = 0; i < num_trials; ++i) {
-            ops += run_test();
-        }
-        double ops_per_sec = ops / num_trials / duration_;
+        duration_ = 1 + table_scale/4;
+        double mops_per_sec = (double)run_test() / 1000000.0 / duration_;
 
-        //map_print(map_);
-        printf("Threads:%-2d  Size:2^%-2d  Mops/Sec:%-4.3g  per-thread:%-4.3g\n\n", 
-                num_threads_, table_scale, ops_per_sec/1000000, ops_per_sec/num_threads_/1000000);
+        printf("Threads:%-2d  Size:2^%-2d  load time:%-4.2f  Mops/s:%-4.2f  per-thread:%-4.2f  ",
+                num_threads_, table_scale, load_time_, mops_per_sec, mops_per_sec/num_threads_);
+        map_print(map_, FALSE);
         fflush(stdout);
 
         map_free(map_);