/* * Copyright (c) 2016, NORDUnet A/S. * See LICENSE for licensing information. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include "permdb.h" #ifdef HAVE_COMMON_CRYPTO #include #else #include #endif static void usage() { errx(1, "usage: permdbtest "); } 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); if (state == NULL) { errx(1, "permdb object creation failed\n"); } printf("generating test data\n"); void *testdata = gentestdata(nentries); printf("inserting test data\n"); int written_since_fsync = 0; 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 < datasize / 32; j++) { memcpy(value + j * 32, testdata + i * 32 * 2 + 32, 32); } if (datasize % 32 > 0) { memcpy(value + (datasize / 32) * 32, testdata + i * 32 * 2 + 32, datasize % 32); } int result = addvalue(state, key, 32, value, datasize, 0); if (result < 0) { free(testdata); permdb_free(state); errx(1, "addvalue: %d\n", result); } written_since_fsync += 1; if (written_since_fsync >= nfsync) { result = committree(state); if (result < 0) { free(testdata); permdb_free(state); errx(1, "committree: %d\n", result); } written_since_fsync = 0; } } free(value); int result = committree(state); if (result < 0) { free(testdata); permdb_free(state); 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) { free(testdata); permdb_free(state); free(result); errx(1, "getvalue returned datalen %zd\n", datalen); } for (int j = 0; j < datasize / 32; j++) { if (memcmp(result + j * 32, testdata + i * 32 * 2 + 32, 32)) { free(testdata); permdb_free(state); free(result); errx(1, "getvalue returned incorrect data\n"); } } if (datasize % 32 > 0) { if (memcmp(result + (datasize / 32) * 32, testdata + i * 32 * 2 + 32, datasize % 32)) { free(testdata); permdb_free(state); free(result); 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; } /* Local Variables: */ /* c-file-style: "BSD" */ /* End: */