commit e68ea9594346104531583be690c8599e7936c00a
parent e87228d19ad6965fd143bdf9a962f7d904fc4e33
Author: William Casarin <jb55@jb55.com>
Date: Thu, 10 Aug 2023 10:21:21 -0700
bench: move ingest benchmark to its own file
Diffstat:
4 files changed, 63 insertions(+), 28 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -7,6 +7,7 @@ shell.nix
TODO.bak
test_contacts_ndb_note
bench
+bench-ingest
configurator
config.h
libsecp256k1.a
diff --git a/Makefile b/Makefile
@@ -12,7 +12,7 @@ C_BINDINGS_COMMON=bindings/c/flatbuffers_common_builder.h bindings/c/flatbuffers
C_BINDINGS=$(C_BINDINGS_COMMON) $(C_BINDINGS_PROFILE)
BINDINGS=bindings
-lib: bench test
+lib: benches test
all: lib bindings
@@ -24,15 +24,14 @@ check: test
clean:
rm -rf test bench bindings
+benches: bench bench-ingest
+
distclean: clean
rm -rf deps
tags:
ctags *.c *.h
-benchmark: bench
- ./bench
-
configurator: configurator.c
$(CC) $< -o $@
@@ -103,6 +102,9 @@ deps/lmdb/liblmdb.a: deps/lmdb/lmdb.h
bench: bench.c $(DEPS)
$(CC) $(CFLAGS) bench.c $(LDS) -o $@
+bench-ingest: bench-ingest.c $(DEPS)
+ $(CC) $(CFLAGS) bench-ingest.c $(LDS) -o $@
+
testdata/db/.dir:
@mkdir -p testdata/db
touch testdata/db/.dir
diff --git a/bench-ingest.c b/bench-ingest.c
@@ -0,0 +1,56 @@
+
+
+#include "io.h"
+#include "nostrdb.h"
+#include <sys/mman.h>
+#include <time.h>
+#include <stdlib.h>
+#include <assert.h>
+
+static int bench_parser(int times)
+{
+ char *json = malloc(1024 * 100);
+ int i, mapsize, written, ingester_threads;
+ long nanos, ms;
+ static const int alloc_size = 2 << 18;
+ struct ndb *ndb;
+ struct timespec t1, t2;
+
+ mapsize = 1024 * 1024 * 100;
+ ingester_threads = 8;
+ assert(ndb_init(&ndb, mapsize, ingester_threads));
+ read_file("testdata/contacts-event.json", (unsigned char*)json, alloc_size, &written);
+
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
+
+ for (i = 0; i < times; i++) {
+ ndb_process_event(ndb, json, written);
+ }
+
+ free(json);
+ ndb_destroy(ndb);
+
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
+
+ nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec);
+ ms = nanos / 1e6;
+ printf("ns/run\t%ld\nms/run\t%f\nns\t%ld\nms\t%ld\n",
+ nanos/times, (double)ms/(double)times, nanos, ms);
+
+ return 1;
+}
+
+int main(int argc, char *argv[], char **env)
+{
+ int times = 50000;
+
+ if (argc >= 2)
+ times = atoi(argv[1]);
+
+ fprintf(stderr, "benching %d duplicate contact events\n", times);
+ if (!bench_parser(times))
+ return 2;
+
+ return 0;
+}
+
diff --git a/test.c b/test.c
@@ -10,29 +10,6 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
-static void test_lmdb_put()
-{
- struct ndb *ndb;
- static const int alloc_size = 2 << 18;
- char *json = malloc(alloc_size);
- int i, mapsize, written, ingester_threads;
-
- mapsize = 1024 * 1024 * 100;
- ingester_threads = 8;
-
- // 256MB
- assert(ndb_init(&ndb, mapsize, ingester_threads));
-
- read_file("testdata/contacts-event.json", (unsigned char*)json, alloc_size, &written);
-
- for (i = 0; i < 50000; i++) {
- ndb_process_event(ndb, json, written);
- }
-
- free(json);
-
- ndb_destroy(ndb);
-}
static void test_basic_event() {
unsigned char buf[512];
@@ -500,7 +477,6 @@ int main(int argc, const char *argv[]) {
test_queue_thread_safety();
test_queue_boundary_conditions();
- test_lmdb_put();
printf("All tests passed!\n"); // Print this if all tests pass.
}