nostrdb

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

bench-ingest-many.c (1568B)


      1 
      2 
      3 #include "io.h"
      4 #include "nostrdb.h"
      5 #include <sys/mman.h>
      6 #include <time.h>
      7 #include <stdlib.h>
      8 #include <assert.h>
      9 #include <unistd.h>
     10 #include <fcntl.h>
     11 #include <sys/stat.h>
     12 
     13 int map_file(const char *filename, unsigned char **p, size_t *flen)
     14 {
     15 	struct stat st;
     16 	int des;
     17 	stat(filename, &st);
     18 	*flen = st.st_size;
     19 
     20 	des = open(filename, O_RDONLY);
     21 
     22 	*p = mmap(NULL, *flen, PROT_READ, MAP_PRIVATE, des, 0);
     23 	close(des);
     24 
     25 	return *p != MAP_FAILED;
     26 }
     27 
     28 static int bench_parser()
     29 {
     30 	long nanos, ms;
     31 	size_t written;
     32 	struct ndb *ndb;
     33 	struct timespec t1, t2;
     34 	char *json;
     35 	int times = 1;
     36 	struct ndb_config config;
     37 	ndb_default_config(&config);
     38 
     39 	ndb_config_set_mapsize(&config, 1024ULL * 1024ULL * 400ULL * 10ULL);
     40 	ndb_config_set_ingest_threads(&config, 8);
     41 	ndb_config_set_flags(&config, NDB_FLAG_SKIP_NOTE_VERIFY);
     42 
     43 	assert(ndb_init(&ndb, "testdata/db", &config));
     44 	const char *filename = "testdata/many-events.json";
     45 	if (!map_file(filename, (unsigned char**)&json, &written)) {
     46 		printf("mapping testdata/many-events.json failed\n");
     47 		return 2;
     48 	}
     49 
     50 	printf("mapped %ld bytes in %s\n", written, filename);
     51 
     52 	clock_gettime(CLOCK_MONOTONIC, &t1);
     53 
     54 	ndb_process_events(ndb, json, written);
     55 
     56 	ndb_destroy(ndb);
     57 
     58 	clock_gettime(CLOCK_MONOTONIC, &t2);
     59 
     60 	nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec);
     61 	ms = nanos / 1e6;
     62 	printf("ns/run\t%ld\nms/run\t%f\nns\t%ld\nms\t%ld\n",
     63 		nanos/times, (double)ms/(double)times, nanos, ms);
     64 
     65 	return 1;
     66 }
     67 
     68 int main(int argc, char *argv[], char **env)
     69 {
     70 	if (!bench_parser())
     71 		return 2;
     72 	
     73 	return 0;
     74 }
     75