nostrdb

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

bench.c (1088B)


      1 
      2 #include "io.h"
      3 #include "nostrdb.h"
      4 #include <sys/mman.h>
      5 #include <time.h>
      6 #include <stdlib.h>
      7 
      8 static int bench_parser(int times, const char *json, int len)
      9 {
     10 	static unsigned char buf[2<<18];
     11 
     12 	struct timespec t1, t2;
     13 	int i;
     14 	long nanos, ms;
     15 	struct ndb_note *note;
     16 
     17 	clock_gettime(CLOCK_MONOTONIC, &t1);
     18 	for (i = 0; i < times; i++) {
     19 		if (!ndb_note_from_json(json, len, &note, buf, sizeof(buf))) {
     20 			return 0;
     21 		}
     22 	}
     23 	clock_gettime(CLOCK_MONOTONIC, &t2);
     24 
     25 	nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec);
     26 	ms = nanos / 1e6;
     27 	printf("ns/run\t%ld\nms/run\t%f\nns\t%ld\nms\t%ld\n",
     28 		nanos/times, (double)ms/(double)times, nanos, ms);
     29 
     30 	return 1;
     31 }
     32 
     33 int main(int argc, char *argv[], char **env)
     34 {
     35 	static const int alloc_size = 2 << 18;
     36 	int times = 10000, len = 0;
     37 	unsigned char buf[alloc_size];
     38 
     39 	if (!read_file("testdata/contacts.json", buf, alloc_size, &len))
     40 		return 1;
     41 
     42 	if (argc >= 2)
     43 		times = atoi(argv[1]);
     44 
     45 	fprintf(stderr, "benching parser %d times\n", times);
     46 	if (!bench_parser(times, (const char*)&buf[0], len))
     47 		return 2;
     48 
     49 	return 0;
     50 }
     51