commit 65c0479b5f6b22a1f9ab6835008b6d164ba5cb2c
parent 47dbefd0db784d8268d1835868a3052effd7e893
Author: William Casarin <jb55@jb55.com>
Date: Sat, 22 Jul 2023 11:32:26 -0700
add benchmarking
Diffstat:
5 files changed, 62 insertions(+), 3 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -6,3 +6,4 @@ test
shell.nix
*.dSYM
test_contacts_ndb_note
+bench
diff --git a/Makefile b/Makefile
@@ -1,4 +1,5 @@
CFLAGS = -Wall -Wno-unused-function -Werror -O2 -g
+DEPS = nostrdb.c nostrdb.h cursor.h hex.h jsmn.h
check: test
./test
@@ -9,7 +10,11 @@ clean:
tags:
ctags *.c *.h
-test: test.c nostrdb.c nostrdb.h
+bench: bench.c $(DEPS)
+ $(CC) $(CFLAGS) bench.c nostrdb.c -o $@
+ ./bench
+
+test: test.c $(DEPS)
$(CC) $(CFLAGS) test.c nostrdb.c -o $@
%.o: %.c
diff --git a/bench.c b/bench.c
@@ -0,0 +1,51 @@
+
+#include "io.h"
+#include "nostrdb.h"
+#include <sys/mman.h>
+#include <time.h>
+#include <stdlib.h>
+
+static int bench_parser(int times, const char *json, int len)
+{
+ static unsigned char buf[2<<18];
+
+ struct timespec t1, t2;
+ int i;
+ long nanos, ms;
+ struct ndb_note *note;
+
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
+ for (i = 0; i < times; i++) {
+ if (!ndb_note_from_json(json, len, ¬e, buf, sizeof(buf))) {
+ return 0;
+ }
+ }
+ 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)
+{
+ static const int alloc_size = 2 << 18;
+ int times = 10000, len;
+ unsigned char buf[alloc_size];
+
+ if (!read_file("contacts.json", buf, alloc_size, &len))
+ return 1;
+
+ if (argc >= 2)
+ times = atoi(argv[1]);
+
+ fprintf(stderr, "benching parser %d times\n", times);
+ if (!bench_parser(times, (const char*)&buf[0], len))
+ return 2;
+
+ return 0;
+}
+
diff --git a/io.h b/io.h
@@ -1,4 +1,6 @@
+#include <stdio.h>
+
static int read_fd(FILE *fd, unsigned char *buf, int buflen, int *written)
{
unsigned char *p = buf;
diff --git a/nostrdb.c b/nostrdb.c
@@ -416,12 +416,12 @@ int ndb_note_from_json(const char *json, int len, struct ndb_note **note,
} else if (start[0] == 'k' && jsoneq(json, tok, tok_len, "kind")) {
// kind
tok = &parser.toks[i+1];
- printf("json_kind %.*s\n", toksize(tok), json + tok->start);
+ //printf("json_kind %.*s\n", toksize(tok), json + tok->start);
} else if (start[0] == 'c') {
if (jsoneq(json, tok, tok_len, "created_at")) {
// created_at
tok = &parser.toks[i+1];
- printf("json_created_at %.*s\n", toksize(tok), json + tok->start);
+ //printf("json_created_at %.*s\n", toksize(tok), json + tok->start);
} else if (jsoneq(json, tok, tok_len, "content")) {
// content
tok = &parser.toks[i+1];