2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file test_plugin_datastore.c
22 * @brief Profile database plugin directly, focusing on iterators.
23 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_plugin.h"
31 #define VERBOSE GNUNET_NO
34 * Target datastore size (in bytes). Realistic sizes are
35 * more like 16 GB (not the default of 16 MB); however,
36 * those take too long to run them in the usual "make check"
37 * sequence. Hence the value used for shipping is tiny.
39 #define MAX_SIZE 1024LL * 1024 * 128
44 * Number of put operations equivalent to 1/10th of MAX_SIZE
46 #define PUT_10 (MAX_SIZE / 32 / 1024 / ITERATIONS)
48 static unsigned long long stored_bytes;
50 static unsigned long long stored_entries;
52 static unsigned long long stored_ops;
54 static const char *plugin_name;
71 struct GNUNET_TIME_Absolute start;
72 struct GNUNET_TIME_Absolute end;
73 const struct GNUNET_CONFIGURATION_Handle *cfg;
74 struct GNUNET_DATASTORE_PluginFunctions * api;
82 * Function called by plugins to notify us about a
83 * change in their disk utilization.
85 * @param cls closure (NULL)
86 * @param delta change in disk utilization,
87 * 0 for "reset to empty"
90 disk_utilization_change_cb (void *cls,
97 putValue (struct GNUNET_DATASTORE_PluginFunctions * api, int i, int k)
101 static GNUNET_HashCode key;
106 /* most content is 32k */
109 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
110 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
111 size = size - (size & 7); /* always multiple of 8 */
113 /* generate random key */
114 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value;
115 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
116 memset (value, i, size);
118 memset (value, i - 255, size / 2);
121 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
122 if (GNUNET_OK != api->put (api->cls,
130 GNUNET_TIME_relative_to_absolute
131 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
132 60 * 60 * 60 * 1000 +
133 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
136 fprintf (stderr, "ERROR: `%s'\n", msg);
137 GNUNET_free_non_null (msg);
141 stored_bytes += size;
148 const struct GNUNET_SCHEDULER_TaskContext *tc);
152 iterateDummy (void *cls,
154 const GNUNET_HashCode * key,
157 enum GNUNET_BLOCK_Type type,
160 struct GNUNET_TIME_Absolute
164 struct CpsRunContext *crc = cls;
168 crc->end = GNUNET_TIME_absolute_get();
171 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
173 if (crc->phase != RP_ZA_GET)
179 if (crc->i == ITERATIONS)
180 crc->phase = RP_DONE;
185 crc->start = GNUNET_TIME_absolute_get ();
186 GNUNET_SCHEDULER_add_now (&test, crc);
190 fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
191 type, priority, size,
192 (unsigned long long) expiration.abs_value);
195 crc->api->next_request (next_cls,
203 dummy_get (void *cls,
205 const GNUNET_HashCode * key,
208 enum GNUNET_BLOCK_Type type,
211 struct GNUNET_TIME_Absolute
215 struct CpsRunContext *crc = cls;
218 if (1000 == crc->cnt)
220 crc->end = GNUNET_TIME_absolute_get();
223 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
227 crc->start = GNUNET_TIME_absolute_get ();
229 GNUNET_SCHEDULER_add_now (&test, crc);
234 * Function called when the service shuts
235 * down. Unloads our datastore plugin.
237 * @param api api to unload
238 * @param cfg configuration to use
241 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
242 const struct GNUNET_CONFIGURATION_Handle *cfg)
248 GNUNET_CONFIGURATION_get_value_string (cfg,
249 "DATASTORE", "DATABASE", &name))
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252 _("No `%s' specified for `%s' in configuration!\n"),
257 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
258 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
259 GNUNET_free (libname);
266 * Last task run during shutdown. Disconnects us from
267 * the transport and core.
270 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
272 struct CpsRunContext *crc = cls;
274 unload_plugin (crc->api, crc->cfg);
281 const struct GNUNET_SCHEDULER_TaskContext *tc)
283 struct CpsRunContext *crc = cls;
286 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
288 crc->phase = RP_DONE;
294 crc->start = GNUNET_TIME_absolute_get ();
295 for (j=0;j<PUT_10;j++)
296 putValue (crc->api, j, crc->i);
297 crc->end = GNUNET_TIME_absolute_get ();
298 printf ("%3u insertion took %20llums for %u\n",
300 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
301 (unsigned int) PUT_10);
303 crc->start = GNUNET_TIME_absolute_get ();
304 crc->phase = RP_LP_GET;
305 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
309 crc->msg = "%3u replication iteration took %20llums for %u\n";
310 crc->api->replication_get (crc->api->cls,
315 crc->msg = "%3u expiration iteration took %20llums for %u\n";
316 crc->api->expiration_get (crc->api->cls,
321 crc->msg = "%3u zero anonymity iteration took %20llums for %u\n";
322 crc->api->iter_zero_anonymity (crc->api->cls, 0,
327 crc->api->drop (crc->api->cls);
328 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
329 &cleaning_task, crc);
336 * Load the datastore plugin.
338 static struct GNUNET_DATASTORE_PluginFunctions *
339 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
341 static struct GNUNET_DATASTORE_PluginEnvironment env;
342 struct GNUNET_DATASTORE_PluginFunctions * ret;
347 GNUNET_CONFIGURATION_get_value_string (cfg,
348 "DATASTORE", "DATABASE", &name))
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 _("No `%s' specified for `%s' in configuration!\n"),
357 env.duc = &disk_utilization_change_cb;
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
360 _("Loading `%s' datastore plugin\n"), name);
361 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
362 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
365 "Failed to load plugin `%s'!\n",
369 GNUNET_free (libname);
379 const struct GNUNET_CONFIGURATION_Handle *c)
381 struct GNUNET_DATASTORE_PluginFunctions *api;
382 struct CpsRunContext *crc;
384 api = load_plugin (c);
388 "Could not initialize plugin, assuming database not configured. Test not run!\n");
391 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
395 GNUNET_SCHEDULER_add_now (&test, crc);
403 char *const argv[] = {
404 "test-plugin-datastore",
412 struct GNUNET_GETOPT_CommandLineOption options[] = {
413 GNUNET_GETOPT_OPTION_END
416 GNUNET_snprintf (cfg_name,
418 "test_plugin_datastore_data_%s.conf",
420 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
421 argv, "test-plugin-datastore", "nohelp",
422 options, &run, NULL);
424 fprintf (stderr, "Missed some testcases: %u\n", ok);
430 main (int argc, char *argv[])
436 /* determine name of plugin to use */
437 plugin_name = argv[0];
438 while (NULL != (pos = strstr(plugin_name, "_")))
440 if (NULL != (pos = strstr(plugin_name, ".")))
443 pos = (char *) plugin_name;
445 GNUNET_snprintf (dir_name,
447 "/tmp/test-gnunet-datastore-%s",
449 GNUNET_DISK_directory_remove (dir_name);
450 GNUNET_log_setup ("test-plugin-datastore",
458 if (pos != plugin_name)
460 GNUNET_DISK_directory_remove (dir_name);
465 /* end of test_plugin_datastore.c */