From 076c398e1b84881f5c9d0da081bd2ef8027c2b2b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 1 Dec 2010 23:22:55 +1030 Subject: [PATCH] tdb2: Add speed test to tdb and tdb2 Current results of speed test: $ ./speed 1000000 Adding 1000000 records: 14726 ns (67244816 bytes) Finding 1000000 records: 2844 ns (67244816 bytes) Missing 1000000 records: 2528 ns (67244816 bytes) Traversing 1000000 records: 2572 ns (67244816 bytes) Deleting 1000000 records: 5358 ns (67244816 bytes) Re-adding 1000000 records: 9176 ns (67244816 bytes) Appending 1000000 records: 3035 ns (67244816 bytes) Churning 1000000 records: 18139 ns (67565840 bytes) $ ./speed 100000 Adding 100000 records: 13270 ns (14349584 bytes) Finding 100000 records: 2769 ns (14349584 bytes) Missing 100000 records: 2422 ns (14349584 bytes) Traversing 100000 records: 2595 ns (14349584 bytes) Deleting 100000 records: 5331 ns (14349584 bytes) Re-adding 100000 records: 5875 ns (14349584 bytes) Appending 100000 records: 2751 ns (14349584 bytes) Churning 100000 records: 20666 ns (25771280 bytes) vs tdb1 (with hashsize 100003): $ ./speed 1000000 Adding 1000000 records: 8547 ns (44306432 bytes) Finding 1000000 records: 5595 ns (44306432 bytes) Missing 1000000 records: 3469 ns (44306432 bytes) Traversing 1000000 records: 4571 ns (44306432 bytes) Deleting 1000000 records: 12115 ns (44306432 bytes) Re-adding 1000000 records: 10505 ns (44306432 bytes) Appending 1000000 records: 10610 ns (44306432 bytes) Churning 1000000 records: 28697 ns (44306432 bytes) $ ./speed 100000 Adding 100000 records: 6030 ns (4751360 bytes) Finding 100000 records: 3141 ns (4751360 bytes) Missing 100000 records: 3143 ns (4751360 bytes) Traversing 100000 records: 4659 ns (4751360 bytes) Deleting 100000 records: 7891 ns (4751360 bytes) Re-adding 100000 records: 5913 ns (4751360 bytes) Appending 100000 records: 4242 ns (4751360 bytes) Churning 100000 records: 15300 ns (4751360 bytes) --- ccan/tdb/tools/Makefile | 4 +- ccan/tdb/tools/speed.c | 247 ++++++++++++++++++++++++++++++++++++++ ccan/tdb2/tools/Makefile | 7 +- ccan/tdb2/tools/speed.c | 252 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 505 insertions(+), 5 deletions(-) create mode 100644 ccan/tdb/tools/speed.c create mode 100644 ccan/tdb2/tools/speed.c diff --git a/ccan/tdb/tools/Makefile b/ccan/tdb/tools/Makefile index d7611a2b..6ba6f998 100644 --- a/ccan/tdb/tools/Makefile +++ b/ccan/tdb/tools/Makefile @@ -2,7 +2,7 @@ LDLIBS:=../../tdb.o ../../tally.o CFLAGS:=-I../../.. -Wall -O3 #-g -pg LDFLAGS:=-L../../.. -default: replay_trace tdbtorture tdbdump tdbtool starvation mktdb +default: replay_trace tdbtorture tdbdump tdbtool starvation mktdb speed benchmark: replay_trace @trap "rm -f /tmp/trace.$$$$" 0; for f in benchmarks/*.rz; do if runzip -k $$f -o /tmp/trace.$$$$ && echo -n "$$f": && ./replay_trace --quiet -n 5 replay.tdb /tmp/trace.$$$$ && rm /tmp/trace.$$$$; then rm -f /tmp/trace.$$$$; else exit 1; fi; done @@ -30,4 +30,4 @@ check: replay_trace @sed 's/\(^[0-9]* traverse\) .*/\1fn/' < $^ > $@ clean: - rm -f replay_trace tdbtorture tdbdump tdbtool *.o + rm -f replay_trace tdbtorture tdbdump tdbtool speed *.o diff --git a/ccan/tdb/tools/speed.c b/ccan/tdb/tools/speed.c new file mode 100644 index 00000000..cf4b7187 --- /dev/null +++ b/ccan/tdb/tools/speed.c @@ -0,0 +1,247 @@ +/* Simple speed test for TDB */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Nanoseconds per operation */ +static size_t normalize(const struct timeval *start, + const struct timeval *stop, + unsigned int num) +{ + struct timeval diff; + + timersub(stop, start, &diff); + + /* Floating point is more accurate here. */ + return (double)(diff.tv_sec * 1000000 + diff.tv_usec) + / num * 1000; +} + +static size_t file_size(void) +{ + struct stat st; + + if (stat("/tmp/speed.tdb", &st) != 0) + return -1; + return st.st_size; +} + +static int count_record(struct tdb_context *tdb, + TDB_DATA key, TDB_DATA data, void *p) +{ + int *total = p; + *total += *(int *)data.dptr; + return 0; +} + +int main(int argc, char *argv[]) +{ + unsigned int i, j, num = 1000, stage = 0, stopat = -1; + int flags = TDB_DEFAULT; + TDB_DATA key, data; + struct tdb_context *tdb; + struct timeval start, stop; + bool transaction = false; + + if (argv[1] && strcmp(argv[1], "--internal") == 0) { + flags = TDB_INTERNAL; + argc--; + argv++; + } + + if (argv[1] && strcmp(argv[1], "--transaction") == 0) { + transaction = true; + argc--; + argv++; + } + + tdb = tdb_open("/tmp/speed.tdb", 100003, flags, O_RDWR|O_CREAT|O_TRUNC, + 0600); + if (!tdb) + err(1, "Opening /tmp/speed.tdb"); + + key.dptr = (void *)&i; + key.dsize = sizeof(i); + data = key; + + if (argv[1]) { + num = atoi(argv[1]); + argv++; + argc--; + } + + if (argv[1]) { + stopat = atoi(argv[1]); + argv++; + argc--; + } + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Add 1000 records. */ + printf("Adding %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (i = 0; i < num; i++) + if (tdb_store(tdb, key, data, TDB_INSERT) != 0) + errx(1, "Inserting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Finding 1000 records. */ + printf("Finding %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (i = 0; i < num; i++) { + int *dptr; + dptr = (int *)tdb_fetch(tdb, key).dptr; + if (!dptr || *dptr != i) + errx(1, "Fetching key %u in tdb gave %u", + i, dptr ? *dptr : -1); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Missing 1000 records. */ + printf("Missing %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (i = num; i < num*2; i++) { + int *dptr; + dptr = (int *)tdb_fetch(tdb, key).dptr; + if (dptr) + errx(1, "Fetching key %u in tdb gave %u", i, *dptr); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Traverse 1000 records. */ + printf("Traversing %u records: ", num); fflush(stdout); + i = 0; + gettimeofday(&start, NULL); + if (tdb_traverse(tdb, count_record, &i) != num) + errx(1, "Traverse returned wrong number of records"); + if (i != (num - 1) * (num / 2)) + errx(1, "Traverse tallied to %u", i); + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Delete 1000 records (not in order). */ + printf("Deleting %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (j = 0; j < num; j++) { + i = (j + 100003) % num; + if (tdb_delete(tdb, key) != 0) + errx(1, "Deleting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Re-add 1000 records (not in order). */ + printf("Re-adding %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (j = 0; j < num; j++) { + i = (j + 100003) % num; + if (tdb_store(tdb, key, data, TDB_INSERT) != 0) + errx(1, "Inserting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Append 1000 records. */ + printf("Appending %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (i = 0; i < num; i++) + if (tdb_append(tdb, key, data) != 0) + errx(1, "Appending key %u in tdb: %s", + i, tdb_errorstr(tdb)); + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Churn 1000 records: not in order! */ + printf("Churning %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (j = 0; j < num; j++) { + i = (j + 1000019) % num; + if (tdb_delete(tdb, key) != 0) + errx(1, "Deleting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + i += num; + if (tdb_store(tdb, key, data, TDB_INSERT) != 0) + errx(1, "Inserting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + + return 0; +} diff --git a/ccan/tdb2/tools/Makefile b/ccan/tdb2/tools/Makefile index c16f27bf..19bb731e 100644 --- a/ccan/tdb2/tools/Makefile +++ b/ccan/tdb2/tools/Makefile @@ -1,12 +1,13 @@ OBJS:=../../tdb2.o ../../hash.o ../../tally.o -CFLAGS:=-I../../.. -Wall -g #-g -O3 #-g -pg +CFLAGS:=-I../../.. -Wall -g -O3 #-g -pg LDFLAGS:=-L../../.. -default: tdbtorture tdbtool mktdb +default: tdbtorture tdbtool mktdb speed tdbtorture: tdbtorture.c $(OBJS) tdbtool: tdbtool.c $(OBJS) mktdb: mktdb.c $(OBJS) +speed: speed.c $(OBJS) clean: - rm -f tdbtorture tdbtool mktdb + rm -f tdbtorture tdbtool mktdb speed diff --git a/ccan/tdb2/tools/speed.c b/ccan/tdb2/tools/speed.c new file mode 100644 index 00000000..84dff996 --- /dev/null +++ b/ccan/tdb2/tools/speed.c @@ -0,0 +1,252 @@ +/* Simple speed test for TDB */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Nanoseconds per operation */ +static size_t normalize(const struct timeval *start, + const struct timeval *stop, + unsigned int num) +{ + struct timeval diff; + + timersub(stop, start, &diff); + + /* Floating point is more accurate here. */ + return (double)(diff.tv_sec * 1000000 + diff.tv_usec) + / num * 1000; +} + +static size_t file_size(void) +{ + struct stat st; + + if (stat("/tmp/speed.tdb", &st) != 0) + return -1; + return st.st_size; +} + +static int count_record(struct tdb_context *tdb, + TDB_DATA key, TDB_DATA data, void *p) +{ + int *total = p; + *total += *(int *)data.dptr; + return 0; +} + +int main(int argc, char *argv[]) +{ + unsigned int i, j, num = 1000, stage = 0, stopat = -1; + int flags = TDB_DEFAULT; + bool transaction = false; + TDB_DATA key, data; + struct tdb_context *tdb; + struct timeval start, stop; + union tdb_attribute seed; + + /* Try to keep benchmarks even. */ + seed.base.attr = TDB_ATTRIBUTE_SEED; + seed.base.next = NULL; + seed.seed.seed = 0; + + if (argv[1] && strcmp(argv[1], "--internal") == 0) { + flags = TDB_INTERNAL; + argc--; + argv++; + } + if (argv[1] && strcmp(argv[1], "--transaction") == 0) { + transaction = true; + argc--; + argv++; + } + + tdb = tdb_open("/tmp/speed.tdb", flags, O_RDWR|O_CREAT|O_TRUNC, + 0600, &seed); + if (!tdb) + err(1, "Opening /tmp/speed.tdb"); + + key.dptr = (void *)&i; + key.dsize = sizeof(i); + data = key; + + if (argv[1]) { + num = atoi(argv[1]); + argv++; + argc--; + } + + if (argv[1]) { + stopat = atoi(argv[1]); + argv++; + argc--; + } + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Add 1000 records. */ + printf("Adding %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (i = 0; i < num; i++) + if (tdb_store(tdb, key, data, TDB_INSERT) != 0) + errx(1, "Inserting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Finding 1000 records. */ + printf("Finding %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (i = 0; i < num; i++) { + int *dptr; + dptr = (int *)tdb_fetch(tdb, key).dptr; + if (!dptr || *dptr != i) + errx(1, "Fetching key %u in tdb gave %u", + i, dptr ? *dptr : -1); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Missing 1000 records. */ + printf("Missing %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (i = num; i < num*2; i++) { + int *dptr; + dptr = (int *)tdb_fetch(tdb, key).dptr; + if (dptr) + errx(1, "Fetching key %u in tdb gave %u", i, *dptr); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Traverse 1000 records. */ + printf("Traversing %u records: ", num); fflush(stdout); + i = 0; + gettimeofday(&start, NULL); + if (tdb_traverse(tdb, count_record, &i) != num) + errx(1, "Traverse returned wrong number of records"); + if (i != (num - 1) * (num / 2)) + errx(1, "Traverse tallied to %u", i); + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Delete 1000 records (not in order). */ + printf("Deleting %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (j = 0; j < num; j++) { + i = (j + 100003) % num; + if (tdb_delete(tdb, key) != 0) + errx(1, "Deleting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Re-add 1000 records (not in order). */ + printf("Re-adding %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (j = 0; j < num; j++) { + i = (j + 100003) % num; + if (tdb_store(tdb, key, data, TDB_INSERT) != 0) + errx(1, "Inserting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Append 1000 records. */ + printf("Appending %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (i = 0; i < num; i++) + if (tdb_append(tdb, key, data) != 0) + errx(1, "Appending key %u in tdb: %s", + i, tdb_errorstr(tdb)); + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + if (++stage == stopat) + exit(0); + + if (transaction && tdb_transaction_start(tdb)) + errx(1, "starting transaction: %s", tdb_errorstr(tdb)); + + /* Churn 1000 records: not in order! */ + printf("Churning %u records: ", num); fflush(stdout); + gettimeofday(&start, NULL); + for (j = 0; j < num; j++) { + i = (j + 1000019) % num; + if (tdb_delete(tdb, key) != 0) + errx(1, "Deleting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + i += num; + if (tdb_store(tdb, key, data, TDB_INSERT) != 0) + errx(1, "Inserting key %u in tdb: %s", + i, tdb_errorstr(tdb)); + } + gettimeofday(&stop, NULL); + if (transaction && tdb_transaction_commit(tdb)) + errx(1, "committing transaction: %s", tdb_errorstr(tdb)); + printf(" %zu ns (%zu bytes)\n", + normalize(&start, &stop, num), file_size()); + + return 0; +} -- 2.39.2