summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2016-02-11 03:41:05 +0100
committerMagnus Ahltorp <map@kth.se>2016-02-11 03:41:05 +0100
commit133ae09a89296dea4689a9ff6e067ad71193ae4f (patch)
tree5856576bb1044044392391f4661e0045e8c18ef7
parentf3f931811feb902b651cfd223943fc8c44715bfe (diff)
Added permdbtest
-rw-r--r--c_src/Makefile14
-rw-r--r--c_src/permdbtest.c123
2 files changed, 134 insertions, 3 deletions
diff --git a/c_src/Makefile b/c_src/Makefile
index e9b0394..c58dc9e 100644
--- a/c_src/Makefile
+++ b/c_src/Makefile
@@ -25,18 +25,20 @@ CFLAGS = -Wall -Werror -std=gnu99 $(LOCAL_CFLAGS) $(OS_CFLAGS)
LDFLAGS = $(LOCAL_CFLAGS) -lnettle $(OS_LDFLAGS)
PORTS = fsynchelper hsmhelper permdbport
+OTHER_BIN = permdb.so permdbtest
common_OBJS = erlport.o net_read_write.o
fsynchelper_OBJS = fsynchelper.o $(common_OBJS)
hsmhelper_OBJS = hsmhelper.o pkcs11.o $(common_OBJS)
permdbport_OBJS = permdb.o permdbport.o arlamath.o hash.o $(common_OBJS)
permdbso_OBJS = permdb.o arlamath.o hash.o permdbpy.o $(common_OBJS)
+permdbtest_OBJS = permdb.o arlamath.o hash.o permdbtest.o $(common_OBJS)
-all: $(PORTS) permdb.so
+all: $(PORTS) $(OTHER_BIN)
clean:
- rm -f $(fsynchelper_OBJS) $(hsmhelper_OBJS) $(permdbport_OBJS) $(PORTS)
+ rm -f $(fsynchelper_OBJS) $(hsmhelper_OBJS) $(permdbport_OBJS) $(permdbso_OBJS) $(permdbtest_OBJS) $(PORTS)
fsynchelper: $(fsynchelper_OBJS)
$(CC) -o fsynchelper $(fsynchelper_OBJS) $(LDFLAGS)
@@ -48,4 +50,10 @@ permdbport: $(permdbport_OBJS)
$(CC) -o permdbport $(permdbport_OBJS) $(LDFLAGS)
permdb.so: $(permdbso_OBJS)
- $(CC) $(LDFLAGS) $(OS_SHLIB_LINKFLAGS) $(permdbso_OBJS) -o permdb.so
+ $(CC) $(permdbso_OBJS) $(LDFLAGS) $(OS_SHLIB_LINKFLAGS) -o permdb.so
+
+permdbtest: $(permdbtest_OBJS)
+ $(CC) $(permdbtest_OBJS) $(LDFLAGS) -o permdbtest
+
+permdb-valgrind:
+ valgrind --leak-check=full ./permdbtest /tmp/permdb-valgrind-test 10000 2048 10000
diff --git a/c_src/permdbtest.c b/c_src/permdbtest.c
new file mode 100644
index 0000000..0d3bab7
--- /dev/null
+++ b/c_src/permdbtest.c
@@ -0,0 +1,123 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <fcntl.h>
+#include <err.h>
+#include <sys/resource.h>
+#include "permdb.h"
+#ifdef HAVE_COMMON_CRYPTO
+#include <CommonCrypto/CommonDigest.h>
+#else
+#include <rhash.h>
+#endif
+static void
+usage()
+{
+ errx(1, "usage: permdbtest <store> <entries> <datasize> <fsync>");
+}
+
+void *
+gentestdata(long long nentries)
+{
+#ifndef HAVE_COMMON_CRYPTO
+ rhash_library_init();
+#endif
+ unsigned char *testdata = malloc(nentries * 32 * 2);
+ for (long long i = 0; i < nentries; i++) {
+ uint32_t key[2];
+ uint32_t value[2];
+ key[0] = htonl(i);
+ key[1] = htonl(0);
+ value[0] = htonl(i);
+ value[1] = htonl(1);
+#ifdef HAVE_COMMON_CRYPTO
+ CC_SHA256(key, sizeof(key), testdata + i * 32 * 2);
+ CC_SHA256(value, sizeof(value), testdata + i * 32 * 2 + 32);
+#else
+ rhash_msg(RHASH_SHA256, key, sizeof(key), testdata + i * 32 * 2);
+ rhash_msg(RHASH_SHA256, value, sizeof(value), testdata + i * 32 * 2 + 32);
+#endif
+ }
+ return testdata;
+}
+
+int
+main(int argc, char *argv[])
+{
+ if (argc != 5) {
+ usage();
+ }
+ const char *store = argv[1];
+ long long nentries = atol(argv[2]);
+ int datasize = atoi(argv[3]);
+ int nfsync = atoi(argv[4]);
+
+ permdb_object *state = permdb_alloc(store);
+
+ printf("generating test data\n");
+ void *testdata = gentestdata(nentries);
+ printf("inserting test data\n");
+ int written_since_fsync = 0;
+ int datamultiplier = datasize / 32;
+ if (datasize % 32 != 0) {
+ printf("datasize %d not multiple of 32, truncating to %d\n", datasize, datamultiplier * 32);
+ }
+ datasize = datamultiplier * 32;
+ unsigned char *value = malloc(datasize);
+ for (long long i = 0; i < nentries; i++) {
+ unsigned char *key = testdata + i * 32 * 2;
+ for (int j = 0; j < datamultiplier; j++) {
+ memcpy(value + j * 32, testdata + i * 32 * 2 + 32, 32);
+ }
+
+ int result = addvalue(state, key, 32, value, datasize);
+ if (result < 0) {
+ errx(1, "addvalue: %d\n", result);
+ }
+ written_since_fsync += 1;
+ if (written_since_fsync >= nfsync) {
+ result = committree(state);
+ if (result < 0) {
+ errx(1, "committree: %d\n", result);
+ }
+ written_since_fsync = 0;
+ }
+ }
+ free(value);
+ int result = committree(state);
+ if (result < 0) {
+ errx(1, "committree: %d\n", result);
+ }
+ written_since_fsync = 0;
+
+ printf("reading test data\n");
+ for (long long i = 0; i < nentries; i++) {
+ unsigned char *key = testdata + i * 32 * 2;
+ size_t datalen;
+ unsigned char *result = getvalue(state, key, 32, &datalen);
+ if (datalen != datasize) {
+ errx(1, "getvalue returned datalen %zd\n", datalen);
+ }
+ for (int j = 0; j < datamultiplier; j++) {
+ if (memcmp(result + j * 32, testdata + i * 32 * 2 + 32, 32)) {
+ errx(1, "getvalue returned incorrect data\n");
+ }
+ }
+ free(result);
+ }
+
+ struct rusage rusage;
+ getrusage(RUSAGE_SELF, &rusage);
+ fprintf(stderr, "user %ld.%d sys %ld.%d maxrss %ld M\n", rusage.ru_utime.tv_sec, (int)rusage.ru_utime.tv_usec, rusage.ru_stime.tv_sec, (int)rusage.ru_utime.tv_usec, rusage.ru_maxrss/1024);
+
+ free(testdata);
+ permdb_free(state);
+
+ return 0;
+}