nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

load_test.c (2120B)


      1 #include <assert.h>
      2 #include <sys/time.h>
      3 #include <stdio.h>
      4 
      5 //#define INT_SET_PRIVATE
      6 #ifdef INT_SET_PRIVATE
      7 /* Make all hash functions private to this module for better
      8  * performance. This may not be necessary depending on compiler
      9  * optimizations. clang 4.2 -O3 benefits while -O4 figures it and get
     10  * same speed with external linkage. */
     11 #define HT_PRIVATE
     12 #include "int_set.h"
     13 #include "ptr_set.c"
     14 #undef HT_PRIVATE
     15 #else
     16 /* Use external linkage. Link with ptr_set.c which int_set depends upon. */
     17 #include "int_set.h"
     18 #endif
     19 
     20 struct timeval time_diff(struct timeval start, struct timeval end)
     21 {
     22     struct timeval temp;
     23     if ((end.tv_usec-start.tv_usec)<0) {
     24         temp.tv_sec = end.tv_sec-start.tv_sec-1;
     25         temp.tv_usec = 1000000+end.tv_usec-start.tv_usec;
     26     } else {
     27         temp.tv_sec = end.tv_sec-start.tv_sec;
     28         temp.tv_usec = end.tv_usec-start.tv_usec;
     29     }
     30     return temp;
     31 }
     32 
     33 double elapsed_ms(struct timeval td)
     34 {
     35     return (double)td.tv_sec * 1000 + (double)td.tv_usec / 1000;
     36 }
     37 
     38 void test_int_set()
     39 {
     40     int i, x;
     41     const int N = 1000000;
     42     //const int N = 1000;
     43     int_set_t ht = {0};
     44     int_set_t *S = &ht;
     45     double ms, nsop, opms;
     46     struct timeval t1, t2, td;
     47 
     48     for (i = 1; i <= N; ++i) {
     49         int_set_add(S, i);
     50         assert(int_set_exists(S, i));
     51     }
     52     assert(int_set_count(S) == N);
     53 
     54     for (i = 1; i <= N; ++i) {
     55         assert(int_set_exists(S, i));
     56     }
     57 
     58     gettimeofday(&t1, 0);
     59     for (x = 0, i = 1; i <= N; ++i) {
     60         x += int_set_exists(S, i);
     61     }
     62     gettimeofday(&t2, 0);
     63 
     64     td = time_diff(t1, t2);
     65     ms = elapsed_ms(td);
     66 
     67     nsop = ms * 1000000 / x;
     68     opms = (double)x / ms;
     69     printf("%d out of %d keys found in time %0.03f ms or %0.01f ns per op\n",
     70             x, N, ms, nsop);
     71     printf("ops / ms: %0.0f\n", opms);
     72 
     73     for (i = 1; i <= N; ++i) {
     74         assert(int_set_count(S) == N - i + 1);
     75         assert(int_set_exists(S, i));
     76         int_set_remove(S, i);
     77         assert(!int_set_exists(S, i));
     78     }
     79     assert(int_set_count(S) == 0);
     80 }
     81 
     82 int main(int argc, char *argv[])
     83 {
     84     test_int_set();
     85     return 0;
     86 }