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 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"
41 #define VERBOSE GNUNET_NO
44 * How long until we give up on transmitting the message?
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
48 static const char *plugin_name;
50 static struct GNUNET_DATASTORE_Handle *datastore;
53 * Target datastore size (in bytes).
55 #define MAX_SIZE 1024LL * 1024 * 4
58 * Report progress outside of major reports? Should probably be GNUNET_YES if
61 #define REPORT_ID GNUNET_YES
64 * Number of put operations equivalent to 1/3rd of MAX_SIZE
66 #define PUT_10 MAX_SIZE / 32 / 1024 / 3
69 * Total number of iterations (each iteration doing
70 * PUT_10 put operations); we report full status every
71 * 10 iterations. Abort with CTRL-C.
76 static unsigned long long stored_bytes;
78 static unsigned long long stored_entries;
80 static unsigned long long stored_ops;
82 static struct GNUNET_TIME_Absolute start_time;
98 const struct GNUNET_CONFIGURATION_Handle *cfg;
101 unsigned long long size;
108 run_continuation (void *cls,
109 const struct GNUNET_SCHEDULER_TaskContext *tc);
115 check_success (void *cls,
119 struct CpsRunContext *crc = cls;
121 if (GNUNET_OK != success)
123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124 "Check success failed: `%s'\n", msg);
125 crc->phase = RP_ERROR;
126 GNUNET_SCHEDULER_add_now (&run_continuation,
131 fprintf (stderr, "I");
133 stored_bytes += crc->size;
137 if (crc->j >= PUT_10)
141 if (crc->i == ITERATIONS)
142 crc->phase = RP_DONE;
146 GNUNET_SCHEDULER_add_continuation (&run_continuation,
148 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
153 * Continuation called to notify client about result of the
157 * @param success GNUNET_SYSERR on failure
158 * @param msg NULL on success, otherwise an error message
161 remove_next(void *cls,
165 struct CpsRunContext *crc = cls;
167 if (GNUNET_OK != success)
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 "remove_next failed: `%s'\n", msg);
171 crc->phase = RP_ERROR;
172 GNUNET_SCHEDULER_add_now (&run_continuation,
177 fprintf (stderr, "D");
179 GNUNET_assert (GNUNET_OK == success);
180 GNUNET_SCHEDULER_add_now (&run_continuation,
186 delete_value (void *cls,
187 const GNUNET_HashCode *key,
190 enum GNUNET_BLOCK_Type type,
193 struct GNUNET_TIME_Absolute
194 expiration, uint64_t uid)
196 struct CpsRunContext *crc = cls;
198 GNUNET_assert (NULL != key);
200 stored_bytes -= size;
203 if (stored_bytes < MAX_SIZE)
205 GNUNET_assert (NULL !=
206 GNUNET_DATASTORE_remove (datastore,
217 run_continuation (void *cls,
218 const struct GNUNET_SCHEDULER_TaskContext *tc)
220 struct CpsRunContext *crc = cls;
222 static GNUNET_HashCode key;
223 static char data[65536];
228 ok = (int) crc->phase;
232 memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
235 /* most content is 32k */
237 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
238 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
239 crc->size = size = size - (size & 7); /* always multiple of 8 */
240 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
241 memset (data, i, size);
243 memset (data, i - 255, size / 2);
245 GNUNET_assert (NULL !=
246 GNUNET_DATASTORE_put (datastore,
252 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
254 GNUNET_TIME_relative_to_absolute
255 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
256 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
262 /* trim down below MAX_SIZE again */
263 GNUNET_assert (NULL !=
264 GNUNET_DATASTORE_get_for_replication (datastore,
274 "Stored %llu kB / %lluk ops / %llu ops/s\n",
275 stored_bytes / 1024, /* used size in k */
276 stored_ops / 1024, /* total operations (in k) */
277 1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value));
280 GNUNET_SCHEDULER_add_continuation (&run_continuation,
282 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
285 GNUNET_snprintf (gstr, sizeof (gstr),
286 "PUT operations in %s-datastore",
288 if (crc->i == ITERATIONS)
289 GAUGER ("DATASTORE", gstr, 1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value), "op/s");
290 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
295 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
306 run_tests (void *cls,
310 struct CpsRunContext *crc = cls;
312 if (success != GNUNET_YES)
315 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
320 GNUNET_SCHEDULER_add_continuation (&run_continuation,
322 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
330 const struct GNUNET_CONFIGURATION_Handle *cfg)
332 struct CpsRunContext *crc;
333 static GNUNET_HashCode zkey;
335 datastore = GNUNET_DATASTORE_connect (cfg);
336 start_time = GNUNET_TIME_absolute_get ();
337 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
341 GNUNET_DATASTORE_put (datastore, 0,
343 GNUNET_BLOCK_TYPE_TEST,
344 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
345 0, 1, GNUNET_TIME_UNIT_MINUTES,
349 "Test 'put' operation failed.\n");
359 struct GNUNET_OS_Process *proc;
361 char *const argv[] = {
362 "perf-datastore-api",
370 struct GNUNET_GETOPT_CommandLineOption options[] = {
371 GNUNET_GETOPT_OPTION_END
374 GNUNET_snprintf (cfg_name,
376 "test_datastore_api_data_%s.conf",
378 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
379 "gnunet-service-arm",
383 "-c", cfg_name, NULL);
384 GNUNET_assert (NULL != proc);
385 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
386 argv, "perf-datastore-api", "nohelp",
387 options, &run, NULL);
388 sleep (1); /* give datastore chance to process 'DROP' */
389 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
391 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
394 GNUNET_OS_process_wait (proc);
395 GNUNET_OS_process_close (proc);
402 main (int argc, char *argv[])
409 /* determine name of plugin to use */
410 plugin_name = argv[0];
411 while (NULL != (pos = strstr(plugin_name, "_")))
413 if (NULL != (pos = strstr(plugin_name, ".")))
416 pos = (char *) plugin_name;
418 GNUNET_snprintf (dir_name,
420 "/tmp/test-gnunet-datastore-%s",
422 GNUNET_DISK_directory_remove (dir_name);
423 GNUNET_log_setup ("perf-datastore-api",
431 if (pos != plugin_name)
434 fprintf (stderr, "\n");
436 GNUNET_DISK_directory_remove (dir_name);
440 /* end of perf_datastore_api.c */