Skip to content

Commit

Permalink
ndb: measure query performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jb55 committed Jan 6, 2024
1 parent 8540083 commit de0c13a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions ndb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "nostrdb.h"
#include "print_util.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -94,13 +95,19 @@ int ndb_print_kind_keys(struct ndb_txn *txn);

static void print_note(struct ndb_note *note)
{
printf("note[%d-%d]: \"%s\"\n", ndb_note_kind(note), ndb_note_created_at(note), ndb_note_content(note));
printf("%d\t%d\t%s",
ndb_note_kind(note),
ndb_note_created_at(note),
ndb_note_content(note));

printf("\n");
}

int main(int argc, char *argv[])
{
struct ndb *ndb;
int i, flags, limit, count;
long nanos;
struct ndb_stat stat;
struct ndb_txn txn;
struct ndb_text_search_results results;
Expand All @@ -109,6 +116,7 @@ int main(int argc, char *argv[])
unsigned char *data;
size_t data_len;
struct ndb_config config;
struct timespec t1, t2;
struct ndb_text_search_config search_config;
ndb_default_config(&config);
ndb_default_text_search_config(&search_config);
Expand Down Expand Up @@ -202,11 +210,16 @@ int main(int argc, char *argv[])
}
}

struct ndb_query_result results[500];
struct ndb_query_result results[10000];
ndb_begin_query(ndb, &txn);
ndb_query(&txn, f, 1, results, 500, &count);

fprintf(stderr, "%d results\n", count);
clock_gettime(CLOCK_MONOTONIC, &t1);
ndb_query(&txn, f, 1, results, 10000, &count);
clock_gettime(CLOCK_MONOTONIC, &t2);

nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec);

fprintf(stderr, "%d results in %f ms\n", count, nanos/1000000.0);
for (i = 0; i < count; i++) {
print_note(results[i].note);
}
Expand Down
2 changes: 1 addition & 1 deletion src/nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2352,7 +2352,7 @@ static int compare_query_results(const void *pa, const void *pb)
b = (struct ndb_query_result *)pb;

if (a->note->created_at == b->note->created_at) {
return memcmp(a->note->id, b->note->id, 32);
return 0;
} else if (a->note->created_at > b->note->created_at) {
return -1;
} else {
Expand Down

0 comments on commit de0c13a

Please sign in to comment.