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 datastore/perf_datastore_api.c
22 * @brief performance measurement for the datastore implementation
23 * @author Christian Grothoff
25 * This testcase inserts a bunch of (variable size) data and then
26 * deletes data until the (reported) database size drops below a given
27 * threshold. This is iterated 10 times, with the actual size of the
28 * content stored and the number of operations performed being printed
29 * for each iteration. The code also prints a "I" for every 40 blocks
30 * inserted and a "D" for every 40 blocks deleted. The deletion
31 * strategy uses the "random" iterator. Priorities and expiration
32 * dates are set using a pseudo-random value within a realistic range.
36 #include "gnunet_util_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_datastore_service.h"
40 #define VERBOSE GNUNET_NO
43 * How long until we give up on transmitting the message?
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
47 static const char *plugin_name;
49 static struct GNUNET_DATASTORE_Handle *datastore;
52 * Target datastore size (in bytes).
54 #define MAX_SIZE 1024LL * 1024 * 4
57 * Report progress outside of major reports? Should probably be GNUNET_YES if
60 #define REPORT_ID GNUNET_YES
63 * Number of put operations equivalent to 1/3rd of MAX_SIZE
65 #define PUT_10 MAX_SIZE / 32 / 1024 / 3
68 * Total number of iterations (each iteration doing
69 * PUT_10 put operations); we report full status every
70 * 10 iterations. Abort with CTRL-C.
75 static unsigned long long stored_bytes;
77 static unsigned long long stored_entries;
79 static unsigned long long stored_ops;
81 static struct GNUNET_TIME_Absolute start_time;
96 struct GNUNET_SCHEDULER_Handle *sched;
97 const struct GNUNET_CONFIGURATION_Handle *cfg;
100 unsigned long long size;
111 run_continuation (void *cls,
112 const struct GNUNET_SCHEDULER_TaskContext *tc);
118 check_success (void *cls,
122 struct CpsRunContext *crc = cls;
123 if (GNUNET_OK != success)
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
126 GNUNET_assert (GNUNET_OK == success);
128 fprintf (stderr, "I");
130 stored_bytes += crc->size;
134 if (crc->j == PUT_10)
138 if (crc->i == ITERATIONS)
139 crc->phase = RP_DONE;
143 GNUNET_SCHEDULER_add_continuation (crc->sched,
146 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
151 * Continuation called to notify client about result of the
155 * @param success GNUNET_SYSERR on failure
156 * @param msg NULL on success, otherwise an error message
159 remove_next(void *cls,
163 struct CpsRunContext *crc = cls;
166 fprintf (stderr, "D");
168 GNUNET_assert (GNUNET_OK == success);
169 GNUNET_SCHEDULER_add_continuation (crc->sched,
172 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
178 do_delete (void *cls,
179 const struct GNUNET_SCHEDULER_TaskContext *tc)
181 struct CpsRunContext *crc = cls;
183 stored_bytes -= crc->esize;
186 GNUNET_DATASTORE_remove (datastore,
198 delete_value (void *cls,
199 const GNUNET_HashCode * key,
202 enum GNUNET_BLOCK_Type type,
205 struct GNUNET_TIME_Absolute
206 expiration, uint64_t uid)
208 struct CpsRunContext *crc = cls;
212 if (stored_bytes < MAX_SIZE)
214 crc->phase = RP_REPORT;
215 GNUNET_SCHEDULER_add_continuation (crc->sched,
218 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
221 GNUNET_SCHEDULER_add_with_priority (crc->sched,
222 GNUNET_SCHEDULER_PRIORITY_HIGH,
228 if (stored_bytes < MAX_SIZE)
230 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
235 memcpy (crc->data, data, size);
236 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
241 run_continuation (void *cls,
242 const struct GNUNET_SCHEDULER_TaskContext *tc)
244 struct CpsRunContext *crc = cls;
246 static GNUNET_HashCode key;
247 static char data[65536];
251 ok = (int) crc->phase;
255 memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
258 /* most content is 32k */
260 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
261 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
262 crc->size = size = size - (size & 7); /* always multiple of 8 */
263 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
264 memset (data, i, size);
266 memset (data, i - 255, size / 2);
268 GNUNET_DATASTORE_put (datastore,
274 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
276 GNUNET_TIME_relative_to_absolute
277 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
278 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
284 /* trim down below MAX_SIZE again */
285 GNUNET_DATASTORE_get_random (datastore,
295 "Stored %llu kB / %lluk ops / %llu ops/s\n",
296 stored_bytes / 1024, /* used size in k */
297 stored_ops / 1024, /* total operations (in k) */
298 1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).value));
301 GNUNET_SCHEDULER_add_continuation (crc->sched,
304 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
307 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
319 struct GNUNET_SCHEDULER_Handle *sched,
322 const struct GNUNET_CONFIGURATION_Handle *cfg)
324 struct CpsRunContext *crc;
326 datastore = GNUNET_DATASTORE_connect (cfg, sched);
327 start_time = GNUNET_TIME_absolute_get ();
328 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
332 GNUNET_SCHEDULER_add_continuation (crc->sched,
335 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
344 char *const argv[] = {
345 "perf-datastore-api",
353 struct GNUNET_GETOPT_CommandLineOption options[] = {
354 GNUNET_GETOPT_OPTION_END
357 GNUNET_snprintf (cfg_name,
359 "test_datastore_api_data_%s.conf",
361 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
362 "gnunet-service-arm",
366 "-c", cfg_name, NULL);
367 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
368 argv, "perf-datastore-api", "nohelp",
369 options, &run, NULL);
370 if (0 != PLIBC_KILL (pid, SIGTERM))
372 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
375 GNUNET_OS_process_wait(pid);
381 main (int argc, char *argv[])
387 /* determine name of plugin to use */
388 plugin_name = argv[0];
389 while (NULL != (pos = strstr(plugin_name, "_")))
392 GNUNET_snprintf (dir_name,
394 "/tmp/test-gnunet-datastore-%s",
396 GNUNET_DISK_directory_remove (dir_name);
397 GNUNET_log_setup ("perf-datastore-api",
406 fprintf (stderr, "\n");
408 GNUNET_DISK_directory_remove (dir_name);
412 /* end of perf_datastore_api.c */