2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2007, 2009, 2011 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 perf_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"
32 #define VERBOSE GNUNET_NO
35 * Target datastore size (in bytes). Realistic sizes are
36 * more like 16 GB (not the default of 16 MB); however,
37 * those take too long to run them in the usual "make check"
38 * sequence. Hence the value used for shipping is tiny.
40 #define MAX_SIZE 1024LL * 1024 * 16 * 1
45 * Number of put operations equivalent to 1/10th of MAX_SIZE
47 #define PUT_10 (MAX_SIZE / 32 / 1024 / ITERATIONS)
49 static char category[256];
51 static unsigned int hits[PUT_10 / 8 + 1];
53 static unsigned long long stored_bytes;
55 static unsigned long long stored_entries;
57 static unsigned long long stored_ops;
59 static const char *plugin_name;
77 struct GNUNET_TIME_Absolute start;
78 struct GNUNET_TIME_Absolute end;
79 const struct GNUNET_CONFIGURATION_Handle *cfg;
80 struct GNUNET_DATASTORE_PluginFunctions * api;
89 * Function called by plugins to notify us about a
90 * change in their disk utilization.
92 * @param cls closure (NULL)
93 * @param delta change in disk utilization,
94 * 0 for "reset to empty"
97 disk_utilization_change_cb (void *cls,
104 putValue (struct GNUNET_DATASTORE_PluginFunctions * api,
109 static GNUNET_HashCode key;
114 /* most content is 32k */
116 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
117 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
118 size = size - (size & 7); /* always multiple of 8 */
120 /* generate random key */
121 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value;
122 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
123 memset (value, i, size);
125 memset (value, i - 255, size / 2);
127 memcpy (&value[4], &i, sizeof (i));
129 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
130 if (GNUNET_OK != api->put (api->cls,
134 1 + i % 4 /* type */,
136 i % 4 /* anonymity */,
138 GNUNET_TIME_relative_to_absolute
139 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
140 60 * 60 * 60 * 1000 +
141 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
144 fprintf (stderr, "ERROR: `%s'\n", msg);
145 GNUNET_free_non_null (msg);
149 stored_bytes += size;
156 const struct GNUNET_SCHEDULER_TaskContext *tc);
160 iterate_zeros (void *cls,
161 const GNUNET_HashCode * key,
164 enum GNUNET_BLOCK_Type type,
167 struct GNUNET_TIME_Absolute
171 struct CpsRunContext *crc = cls;
173 const char *cdata = data;
175 GNUNET_assert (key != NULL);
176 GNUNET_assert (size >= 8);
177 memcpy (&i, &cdata[4], sizeof (i));
178 hits[i/8] |= (1 << (i % 8));
181 fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
182 type, priority, size,
183 (unsigned long long) expiration.abs_value);
186 if (crc->cnt == PUT_10 / 4 - 1)
191 for (i = 0;i<PUT_10;i++)
192 if (0 != (hits[i/8] & (1 << (i % 8))))
195 crc->end = GNUNET_TIME_absolute_get();
196 printf ("%s took %llu ms yielding %u/%u items\n",
197 "Select random zero-anonymity item",
198 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
203 "Select random zero-anonymity item",
204 (crc->end.abs_value - crc->start.abs_value) / crc->cnt, "ms/item");
205 memset (hits, 0, sizeof (hits));
208 crc->start = GNUNET_TIME_absolute_get ();
210 GNUNET_SCHEDULER_add_now (&test, crc);
216 expiration_get (void *cls,
217 const GNUNET_HashCode * key,
220 enum GNUNET_BLOCK_Type type,
223 struct GNUNET_TIME_Absolute
227 struct CpsRunContext *crc = cls;
229 const char *cdata = data;
231 GNUNET_assert (size >= 8);
232 memcpy (&i, &cdata[4], sizeof (i));
233 hits[i/8] |= (1 << (i % 8));
235 if (PUT_10 <= crc->cnt)
240 for (i = 0;i<PUT_10;i++)
241 if (0 != (hits[i/8] & (1 << (i % 8))))
244 crc->end = GNUNET_TIME_absolute_get();
245 printf ("%s took %llu ms yielding %u/%u items\n",
246 "Selecting and deleting by expiration",
247 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
249 (unsigned int) PUT_10);
252 "Selecting and deleting by expiration",
253 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
255 memset (hits, 0, sizeof (hits));
256 if (++crc->iter == ITERATIONS)
261 crc->start = GNUNET_TIME_absolute_get ();
263 GNUNET_SCHEDULER_add_now (&test, crc);
269 replication_get (void *cls,
270 const GNUNET_HashCode * key,
273 enum GNUNET_BLOCK_Type type,
276 struct GNUNET_TIME_Absolute
280 struct CpsRunContext *crc = cls;
282 const char *cdata = data;
284 GNUNET_assert (NULL != key);
285 GNUNET_assert (size >= 8);
286 memcpy (&i, &cdata[4], sizeof (i));
287 hits[i/8] |= (1 << (i % 8));
289 if (PUT_10 <= crc->cnt)
294 for (i = 0;i<PUT_10;i++)
295 if (0 != (hits[i/8] & (1 << (i % 8))))
298 crc->end = GNUNET_TIME_absolute_get();
299 printf ("%s took %llu ms yielding %u/%u items\n",
300 "Selecting random item for replication",
301 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
303 (unsigned int) PUT_10);
306 "Selecting random item for replication",
307 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
309 memset (hits, 0, sizeof (hits));
313 crc->start = GNUNET_TIME_absolute_get ();
316 GNUNET_SCHEDULER_add_now (&test, crc);
322 * Function called when the service shuts
323 * down. Unloads our datastore plugin.
325 * @param api api to unload
326 * @param cfg configuration to use
329 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
330 const struct GNUNET_CONFIGURATION_Handle *cfg)
336 GNUNET_CONFIGURATION_get_value_string (cfg,
337 "DATASTORE", "DATABASE", &name))
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 _("No `%s' specified for `%s' in configuration!\n"),
345 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
346 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
347 GNUNET_free (libname);
354 * Last task run during shutdown. Disconnects us from
355 * the transport and core.
358 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
360 struct CpsRunContext *crc = cls;
362 unload_plugin (crc->api, crc->cfg);
369 const struct GNUNET_SCHEDULER_TaskContext *tc)
371 struct CpsRunContext *crc = cls;
374 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
377 crc->phase = RP_ERROR;
380 fprintf (stderr, "In phase %d, iteration %u\n",
388 crc->api->drop (crc->api->cls);
390 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
391 &cleaning_task, crc);
394 crc->start = GNUNET_TIME_absolute_get ();
395 for (j=0;j<PUT_10;j++)
396 putValue (crc->api, j, crc->i);
397 crc->end = GNUNET_TIME_absolute_get ();
399 printf ("%s took %llu ms for %llu items\n",
401 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
406 (crc->end.abs_value - crc->start.abs_value) / PUT_10,
410 crc->start = GNUNET_TIME_absolute_get ();
412 GNUNET_SCHEDULER_add_now (&test, crc);
415 crc->api->get_replication (crc->api->cls,
420 crc->api->get_zero_anonymity (crc->api->cls,
427 crc->api->get_expiration (crc->api->cls,
432 crc->api->drop (crc->api->cls);
434 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
435 &cleaning_task, crc);
442 * Load the datastore plugin.
444 static struct GNUNET_DATASTORE_PluginFunctions *
445 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
447 static struct GNUNET_DATASTORE_PluginEnvironment env;
448 struct GNUNET_DATASTORE_PluginFunctions * ret;
453 GNUNET_CONFIGURATION_get_value_string (cfg,
454 "DATASTORE", "DATABASE", &name))
456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
457 _("No `%s' specified for `%s' in configuration!\n"),
463 env.duc = &disk_utilization_change_cb;
465 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
466 _("Loading `%s' datastore plugin\n"), name);
467 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
468 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
471 "Failed to load plugin `%s'!\n",
475 GNUNET_free (libname);
485 const struct GNUNET_CONFIGURATION_Handle *c)
487 struct GNUNET_DATASTORE_PluginFunctions *api;
488 struct CpsRunContext *crc;
490 api = load_plugin (c);
494 "Could not initialize plugin, assuming database not configured. Test not run!\n");
497 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
502 GNUNET_SCHEDULER_add_now (&test, crc);
510 char *const argv[] = {
511 "perf-plugin-datastore",
519 struct GNUNET_GETOPT_CommandLineOption options[] = {
520 GNUNET_GETOPT_OPTION_END
523 GNUNET_snprintf (category, sizeof (category),
526 GNUNET_snprintf (cfg_name,
528 "perf_plugin_datastore_data_%s.conf",
530 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
531 argv, "perf-plugin-datastore", "nohelp",
532 options, &run, NULL);
534 fprintf (stderr, "Missed some testcases: %u\n", ok);
540 main (int argc, char *argv[])
547 /* determine name of plugin to use */
548 plugin_name = argv[0];
549 while (NULL != (pos = strstr(plugin_name, "_")))
551 if (NULL != (pos = strstr(plugin_name, ".")))
554 pos = (char *) plugin_name;
556 GNUNET_snprintf (dir_name,
558 "/tmp/perf-gnunet-datastore-%s",
560 GNUNET_DISK_directory_remove (dir_name);
561 GNUNET_log_setup ("perf-plugin-datastore",
569 if (pos != plugin_name)
571 GNUNET_DISK_directory_remove (dir_name);
576 /* end of perf_plugin_datastore.c */