From b87365d8c71868bc6ceb440671b9695165d209fd Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 26 Jul 2009 19:15:11 +0000 Subject: [PATCH] fixing bugs, adding testcases --- src/datacache/Makefile.am | 49 +++---- src/datacache/datacache_api.c | 9 +- src/datacache/perf_datacache_api.c | 161 +++++++++++++++++++++ src/datacache/perf_datacache_api_data.conf | 4 + src/datacache/plugin_datacache_sqlite.c | 6 +- src/datacache/test_datacache_api.c | 130 ++++++++++------- src/datacache/test_datacache_api_data.conf | 4 + 7 files changed, 278 insertions(+), 85 deletions(-) create mode 100644 src/datacache/perf_datacache_api.c create mode 100644 src/datacache/perf_datacache_api_data.conf create mode 100644 src/datacache/test_datacache_api_data.conf diff --git a/src/datacache/Makefile.am b/src/datacache/Makefile.am index 80b56bba1..f3b17757b 100644 --- a/src/datacache/Makefile.am +++ b/src/datacache/Makefile.am @@ -46,31 +46,24 @@ libgnunet_plugin_datacache_template_la_LDFLAGS = \ $(GN_PLUGIN_LDFLAGS) -#check_PROGRAMS = \ -# test_datacache_api \ -# perf_datacache_api \ -# perf_plugin_datacache -# -#TESTS = $(check_PROGRAMS) -# -#test_datacache_api_SOURCES = \ -# test_datacache_api.c -#test_datacache_api_LDADD = \ -# $(top_builddir)/src/datacache/libgnunetdatacache.la \ -# $(top_builddir)/src/util/libgnunetutil.la -# -#perf_datacache_api_SOURCES = \ -# perf_datacache_api.c -#perf_datacache_api_LDADD = \ -# $(top_builddir)/src/datacache/libgnunetdatacache.la \ -# $(top_builddir)/src/util/libgnunetutil.la -# -#perf_plugin_datacache_SOURCES = \ -# perf_plugin_datacache.c -#perf_plugin_datacache_LDADD = \ -# $(top_builddir)/src/util/libgnunetutil.la -# -# -#EXTRA_DIST = \ -# test_datacache_api_data.conf \ -# perf_plugin_datacache_data.conf +check_PROGRAMS = \ + test_datacache_api \ + perf_datacache_api + +TESTS = $(check_PROGRAMS) + +test_datacache_api_SOURCES = \ + test_datacache_api.c +test_datacache_api_LDADD = \ + $(top_builddir)/src/datacache/libgnunetdatacache.la \ + $(top_builddir)/src/util/libgnunetutil.la + +perf_datacache_api_SOURCES = \ + perf_datacache_api.c +perf_datacache_api_LDADD = \ + $(top_builddir)/src/datacache/libgnunetdatacache.la \ + $(top_builddir)/src/util/libgnunetutil.la + +EXTRA_DIST = \ + test_datacache_api_data.conf \ + perf_datacache_api_data.conf diff --git a/src/datacache/datacache_api.c b/src/datacache/datacache_api.c index 05dfb89e2..0958c1d2c 100644 --- a/src/datacache/datacache_api.c +++ b/src/datacache/datacache_api.c @@ -197,13 +197,18 @@ GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched, */ void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h) { - GNUNET_CONTAINER_bloomfilter_free (h->filter); + if (h->filter != NULL) + GNUNET_CONTAINER_bloomfilter_free (h->filter); if (h->api != NULL) GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api)); GNUNET_free (h->lib_name); GNUNET_free (h->short_name); GNUNET_free (h->section); - GNUNET_free (h->bloom_name); + if (h->bloom_name != NULL) + { + UNLINK (h->bloom_name); + GNUNET_free (h->bloom_name); + } GNUNET_free (h); } diff --git a/src/datacache/perf_datacache_api.c b/src/datacache/perf_datacache_api.c new file mode 100644 index 000000000..cf527f0df --- /dev/null +++ b/src/datacache/perf_datacache_api.c @@ -0,0 +1,161 @@ +/* + This file is part of GNUnet. + (C) 2006, 2009 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +/* + * @file datacache/perf_datacache_api.c + * @brief Performance evaluation for the datacache implementations. + * @author Nils Durner + */ +#include "platform.h" +#include "gnunet_util_lib.h" +#include "gnunet_datacache_lib.h" + +#define VERBOSE GNUNET_YES + +#define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0) + +#define ITERATIONS 10000 + +static int ok; + +static unsigned int found; + +static int +checkIt (void *cls, + const GNUNET_HashCode * key, + uint32_t size, + const char *data, + uint32_t type) +{ + if ( (size == sizeof (GNUNET_HashCode)) && + (0 == memcmp (data, cls, size)) ) + found++; + return GNUNET_OK; +} + + +static void +run (void *cls, + struct GNUNET_SCHEDULER_Handle *sched, + char *const *args, + const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg) +{ + struct GNUNET_DATACACHE_Handle *h; + GNUNET_HashCode k; + GNUNET_HashCode n; + struct GNUNET_TIME_Absolute exp; + struct GNUNET_TIME_Absolute start; + unsigned int i; + + ok = 0; + h = GNUNET_DATACACHE_create (sched, + cfg, + "perfcache"); + + ASSERT (NULL != h); + exp = GNUNET_TIME_absolute_get (); + start = exp; + exp.value += 5 * 60 * 1000; + memset (&k, 0, sizeof (GNUNET_HashCode)); + for (i = 0; i < ITERATIONS; i++) + { + if (0 == i % (ITERATIONS / 80)) + fprintf (stderr, "."); + GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n); + ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h, + &k, + sizeof (GNUNET_HashCode), + (const char *) &n, + 1+i%16, + exp)); + k = n; + } + fprintf (stderr, "\n"); + fprintf (stdout, "Stored %u items in %llums\n", + ITERATIONS, + (unsigned long long) GNUNET_TIME_absolute_get_duration(start).value); + start = GNUNET_TIME_absolute_get (); + memset (&k, 0, sizeof (GNUNET_HashCode)); + for (i = 0; i < ITERATIONS; i++) + { + if (0 == i % (ITERATIONS / 80)) + fprintf (stderr, "."); + GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n); + GNUNET_DATACACHE_get (h, &k, 1+i%16, + &checkIt, &n); + k = n; + } + fprintf (stderr, "\n"); + fprintf (stdout, "Found %u/%u items in %llums (%u were deleted during storage processing)\n", + found, ITERATIONS, + (unsigned long long) GNUNET_TIME_absolute_get_duration(start).value, + ITERATIONS - found); + + GNUNET_DATACACHE_destroy (h); + ASSERT (ok == 0); + return; +FAILURE: + if (h != NULL) + GNUNET_DATACACHE_destroy (h); + ok = GNUNET_SYSERR; +} + + +static int +check () +{ + char *const argv[] = { "perf-datacache-api", + "-c", + "perf_datacache_api_data.conf", +#if VERBOSE + "-L", "DEBUG", +#endif + NULL + }; + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, + argv, "perf-datacache-api", "nohelp", + options, &run, NULL); + if (ok != 0) + fprintf (stderr, "Missed some perfcases: %d\n", ok); + return ok; +} + + +int +main (int argc, char *argv[]) +{ + int ret; + + GNUNET_DISK_directory_remove ("/tmp/perf-gnunetd-datacache"); + GNUNET_log_setup ("perf-datacache-api", +#if VERBOSE + "DEBUG", +#else + "WARNING", +#endif + NULL); + ret = check (); + + return ret; +} + +/* end of perf_datacache_api.c */ diff --git a/src/datacache/perf_datacache_api_data.conf b/src/datacache/perf_datacache_api_data.conf new file mode 100644 index 000000000..55e178d99 --- /dev/null +++ b/src/datacache/perf_datacache_api_data.conf @@ -0,0 +1,4 @@ + +[perfcache] +QUOTA = 500000 +DATABASE = sqlite diff --git a/src/datacache/plugin_datacache_sqlite.c b/src/datacache/plugin_datacache_sqlite.c index 800241126..636303efc 100644 --- a/src/datacache/plugin_datacache_sqlite.c +++ b/src/datacache/plugin_datacache_sqlite.c @@ -236,7 +236,7 @@ sqlite_plugin_get (void *cls, if (sqlite3_step (stmt) != SQLITE_ROW) break; size = sqlite3_column_bytes (stmt, 0); - dat = sqlite3_column_blob (stmt, 1); + dat = sqlite3_column_blob (stmt, 0); cnt++; if (GNUNET_OK != iter (iter_cls, key, @@ -279,7 +279,7 @@ sqlite_plugin_del (void *cls) "SELECT type, key, value FROM ds090 ORDER BY expire ASC LIMIT 1", &stmt) != SQLITE_OK) || (sq_prepare (plugin->dbh, - "DELETE FROM ds080 " + "DELETE FROM ds090 " "WHERE key=? AND value=? AND type=?", &dstmt) != SQLITE_OK)) { @@ -398,7 +398,7 @@ libgnunet_plugin_datacache_sqlite_init (void *cls) " expire INTEGER NOT NULL DEFAULT 0," " key BLOB NOT NULL DEFAULT ''," " value BLOB NOT NULL DEFAULT '')"); - SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds080 (key,type,expire)"); + SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds090 (key,type,expire)"); plugin = GNUNET_malloc (sizeof (struct Plugin)); plugin->env = env; plugin->dbh = dbh; diff --git a/src/datacache/test_datacache_api.c b/src/datacache/test_datacache_api.c index d4fea584f..124f75a12 100644 --- a/src/datacache/test_datacache_api.c +++ b/src/datacache/test_datacache_api.c @@ -22,102 +22,128 @@ * @brief Test for the datacache implementations. * @author Nils Durner */ - #include "platform.h" #include "gnunet_util_lib.h" #include "gnunet_datacache_lib.h" -#if 0 +#define VERBOSE GNUNET_NO + #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0) -static int error; +static int ok; + static int -checkIt (const GNUNET_HashCode * key, - unsigned int type, unsigned int size, const char *data, void *cls) +checkIt (void *cls, + const GNUNET_HashCode * key, + uint32_t size, + const char *data, + uint32_t type) { if (size != sizeof (GNUNET_HashCode)) { printf ("ERROR: Invalid size\n"); - error = 2; + ok = 2; } if (0 != memcmp (data, cls, size)) { printf ("ERROR: Invalid data\n"); - error = 3; + ok = 3; } return GNUNET_OK; } -/** - * Add testcode here! - */ -static int -test (GNUNET_Dstore_ServiceAPI * api) + +static void +run (void *cls, + struct GNUNET_SCHEDULER_Handle *sched, + char *const *args, + const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg) { + struct GNUNET_DATACACHE_Handle *h; GNUNET_HashCode k; GNUNET_HashCode n; - GNUNET_CronTime exp; + struct GNUNET_TIME_Absolute exp; unsigned int i; - exp = GNUNET_get_time () + 5 * GNUNET_CRON_MINUTES; + ok = 0; + h = GNUNET_DATACACHE_create (sched, + cfg, + "testcache"); + + ASSERT (NULL != h); + exp = GNUNET_TIME_absolute_get (); + exp.value += 5 * 60 * 1000; memset (&k, 0, sizeof (GNUNET_HashCode)); for (i = 0; i < 100; i++) { - GNUNET_hash (&k, sizeof (GNUNET_HashCode), &n); - ASSERT (GNUNET_OK == api->put (&k, - i % 2, - exp, sizeof (GNUNET_HashCode), - (const char *) &n)); + GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n); + ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h, + &k, + sizeof (GNUNET_HashCode), + (const char *) &n, + 1+i%16, + exp)); k = n; } memset (&k, 0, sizeof (GNUNET_HashCode)); for (i = 0; i < 100; i++) { - GNUNET_hash (&k, sizeof (GNUNET_HashCode), &n); - ASSERT (1 == api->get (&k, i % 2, &checkIt, &n)); + GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n); + ASSERT (1 == + GNUNET_DATACACHE_get (h, &k, 1+i%16, + &checkIt, &n)); k = n; } - return GNUNET_OK; + GNUNET_DATACACHE_destroy (h); + ASSERT (ok == 0); + return; FAILURE: - return GNUNET_SYSERR; + if (h != NULL) + GNUNET_DATACACHE_destroy (h); + ok = GNUNET_SYSERR; } -#define TEST_DB "/tmp/GNUnet_dstore_test/" + +static int +check () +{ + char *const argv[] = { "test-datacache-api", + "-c", + "test_datacache_api_data.conf", +#if VERBOSE + "-L", "DEBUG", #endif + NULL + }; + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, + argv, "test-datacache-api", "nohelp", + options, &run, NULL); + if (ok != 0) + fprintf (stderr, "Missed some testcases: %d\n", ok); + return ok; +} + int main (int argc, char *argv[]) { -#if 0 - GNUNET_Dstore_ServiceAPI *api; - int ok; - struct GNUNET_GC_Configuration *cfg; - struct GNUNET_CronManager *cron; - - GNUNET_disable_entropy_gathering (); - cfg = GNUNET_GC_create (); - if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf")) - { - GNUNET_GC_free (cfg); - return -1; - } - cron = GNUNET_cron_create (NULL); - GNUNET_CORE_init (NULL, cfg, cron, NULL); - api = GNUNET_CORE_request_service ("dstore"); - if (api != NULL) - { - ok = test (api); - GNUNET_CORE_release_service (api); - } - else - ok = GNUNET_SYSERR; - GNUNET_CORE_done (); - if (ok == GNUNET_SYSERR) - return 1; - return error; + int ret; + + GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datacache"); + GNUNET_log_setup ("test-datacache-api", +#if VERBOSE + "DEBUG", +#else + "WARNING", #endif - return 0; + NULL); + ret = check (); + + return ret; } /* end of test_datacache_api.c */ diff --git a/src/datacache/test_datacache_api_data.conf b/src/datacache/test_datacache_api_data.conf new file mode 100644 index 000000000..a64ff291d --- /dev/null +++ b/src/datacache/test_datacache_api_data.conf @@ -0,0 +1,4 @@ + +[testcache] +QUOTA = 50000 +DATABASE = sqlite -- 2.25.1