2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2011, 2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
35 #include "gnunet_util_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_datastore_service.h"
38 #include "gnunet_testing_lib.h"
42 * How long until we give up on transmitting the message?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
47 * Target datastore size (in bytes).
49 #define MAX_SIZE (1024LL * 1024 * 4)
52 * Report progress outside of major reports? Should probably be #GNUNET_YES if
55 #define REPORT_ID GNUNET_YES
58 * Number of put operations equivalent to 1/3rd of #MAX_SIZE
60 #define PUT_10 MAX_SIZE / 32 / 1024 / 3
63 * Total number of iterations (each iteration doing
64 * PUT_10 put operations); we report full status every
65 * 10 iterations. Abort with CTRL-C.
70 * Total number of iterations to do to go beyond the quota.
71 * The quota is set to 10 MB or 2.5 times #MAX_SIZE,
72 * so we got 16 times #MAX_SIZE to be sure to hit it a LOT.
74 #define QUOTA_PUTS (MAX_SIZE / 32 / 1024 * 16LL)
78 * Number of bytes stored in the datastore in total.
80 static unsigned long long stored_bytes;
83 * Number of entries stored in the datastore in total.
85 static unsigned long long stored_entries;
88 * Number of database operations performed. Inserting
89 * counts as one operation, deleting as two (as deletion
90 * requires selecting a value for deletion first).
92 static unsigned long long stored_ops;
95 * Start time of the benchmark.
97 static struct GNUNET_TIME_Absolute start_time;
100 * Database backend we use.
102 static const char *plugin_name;
105 * Handle to the datastore.
107 static struct GNUNET_DATASTORE_Handle *datastore;
110 * Value we return from #main().
115 * Which phase of the process are we in?
120 * We are done (shutting down normally).
125 * We are adding new entries to the datastore.
130 * We are deleting entries from the datastore.
135 * We are putting as much as we can to see how the database performs
136 * when it reaches the quota and has to auto-delete (see #3903).
141 * We are generating a report.
146 * Execution failed with some kind of error.
153 * Closure we give to all of the functions executing the
154 * benchmark. Could right now be global, but this allows
155 * us to theoretically run multiple clients "in parallel".
160 * Execution phase we are in.
165 * Size of the value we are currently storing (during #RP_PUT).
170 * Current iteration counter, we are done with the benchmark
171 * once it hits #ITERATIONS.
176 * Counts the number of items put in the current phase.
177 * Once it hits #PUT_10, we progress to the #RP_CUT phase
178 * or are done if @e i reaches #ITERATIONS.
185 * Main state machine. Executes the next step of the benchmark
186 * depending on the current state.
188 * @param cls the `struct CpsRunContext`
191 run_continuation (void *cls);
195 * Continuation called to notify client about result of the insertion
196 * operation. Checks for errors, updates our iteration counters and
197 * continues execution with #run_continuation().
199 * @param cls the `struct CpsRunContext`
200 * @param success #GNUNET_SYSERR on failure
201 * @param min_expiration minimum expiration time required for content to be stored
202 * by the datacache at this time, zero for unknown
203 * @param msg NULL on success, otherwise an error message
206 check_success (void *cls,
208 struct GNUNET_TIME_Absolute min_expiration,
211 struct CpsRunContext *crc = cls;
214 FPRINTF (stderr, "%s", (GNUNET_OK == success) ? "I" : "i");
216 if (GNUNET_OK != success)
218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219 "Check success failed: `%s'\n",
221 crc->phase = RP_ERROR;
222 GNUNET_SCHEDULER_add_now (&run_continuation,
226 stored_bytes += crc->size;
233 if (crc->j >= PUT_10)
237 if (crc->i == ITERATIONS)
238 crc->phase = RP_PUT_QUOTA;
244 if (crc->j >= QUOTA_PUTS)
247 crc->phase = RP_DONE;
253 GNUNET_SCHEDULER_add_now (&run_continuation,
259 * Continuation called to notify client about result of the
260 * deletion operation. Checks for errors and continues
261 * execution with #run_continuation().
263 * @param cls the `struct CpsRunContext`
264 * @param success #GNUNET_SYSERR on failure
265 * @param min_expiration minimum expiration time required for content to be stored
266 * by the datacache at this time, zero for unknown
267 * @param msg NULL on success, otherwise an error message
270 remove_next (void *cls,
272 struct GNUNET_TIME_Absolute min_expiration,
275 struct CpsRunContext *crc = cls;
277 if (GNUNET_OK != success)
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280 "remove_next failed: `%s'\n",
282 crc->phase = RP_ERROR;
283 GNUNET_SCHEDULER_add_now (&run_continuation,
288 FPRINTF (stderr, "%s", "D");
290 GNUNET_assert (GNUNET_OK == success);
291 GNUNET_SCHEDULER_add_now (&run_continuation,
297 * We have selected a value for deletion, trigger removal.
299 * @param cls the `struct CpsRunContext`
300 * @param key key for the content
301 * @param size number of bytes in data
302 * @param data content stored
303 * @param type type of the content
304 * @param priority priority of the content
305 * @param anonymity anonymity-level for the content
306 * @param replication replication-level for the content
307 * @param expiration expiration time for the content
308 * @param uid unique identifier for the datum;
309 * maybe 0 if no unique identifier is available
312 delete_value (void *cls,
313 const struct GNUNET_HashCode *key,
316 enum GNUNET_BLOCK_Type type,
319 uint32_t replication,
320 struct GNUNET_TIME_Absolute expiration,
323 struct CpsRunContext *crc = cls;
325 GNUNET_assert (NULL != key);
327 stored_bytes -= size;
330 if (stored_bytes < MAX_SIZE)
332 GNUNET_assert (NULL !=
333 GNUNET_DATASTORE_remove (datastore,
342 * Main state machine. Executes the next step of the benchmark
343 * depending on the current state.
345 * @param cls the `struct CpsRunContext`
348 run_continuation (void *cls)
350 struct CpsRunContext *crc = cls;
352 static struct GNUNET_HashCode key;
353 static char data[65536];
356 ok = (int) crc->phase;
362 sizeof (struct GNUNET_HashCode));
363 /* most content is 32k */
366 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
367 16)) /* but some of it is less! */
368 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
370 crc->size = size = size - (size & 7); /* always multiple of 8 */
371 GNUNET_CRYPTO_hash (&key,
372 sizeof (struct GNUNET_HashCode),
379 (int) (crc->j - 255),
382 GNUNET_assert (NULL !=
383 GNUNET_DATASTORE_put (datastore,
389 GNUNET_CRYPTO_random_u32
390 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
393 GNUNET_TIME_relative_to_absolute
394 (GNUNET_TIME_relative_multiply
395 (GNUNET_TIME_UNIT_SECONDS,
396 GNUNET_CRYPTO_random_u32
397 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
400 &check_success, crc));
403 /* trim down below MAX_SIZE again */
404 GNUNET_assert (NULL !=
405 GNUNET_DATASTORE_get_for_replication (datastore,
415 "Stored %llu kB / %lluk ops / %llu ops/s\n",
416 stored_bytes / 1024, /* used size in k */
417 stored_ops / 1024, /* total operations (in k) */
418 1000LL * 1000LL * stored_ops / (1 +
419 GNUNET_TIME_absolute_get_duration
420 (start_time).rel_value_us));
423 GNUNET_SCHEDULER_add_now (&run_continuation,
429 sizeof (struct GNUNET_HashCode));
430 /* most content is 32k */
433 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
434 16)) /* but some of it is less! */
435 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
437 crc->size = size = size - (size & 7); /* always multiple of 8 */
438 GNUNET_CRYPTO_hash (&key,
439 sizeof (struct GNUNET_HashCode),
446 (int) (crc->j - 255),
449 GNUNET_assert (NULL !=
450 GNUNET_DATASTORE_put (datastore,
451 0, /* reservation ID */
455 crc->j + 1, /* type */
456 GNUNET_CRYPTO_random_u32
457 (GNUNET_CRYPTO_QUALITY_WEAK,
459 crc->j, /* anonymity */
461 GNUNET_TIME_relative_to_absolute
462 (GNUNET_TIME_relative_multiply
463 (GNUNET_TIME_UNIT_SECONDS,
464 GNUNET_CRYPTO_random_u32
465 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
468 &check_success, crc));
472 GNUNET_snprintf (gstr,
476 if ((crc->i == ITERATIONS) && (stored_ops > 0))
479 "PUT operation duration",
480 GNUNET_TIME_absolute_get_duration (start_time).rel_value_us / 1000LL /
484 "\nPUT performance: %s for %llu operations\n",
485 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start_time),
489 "PUT performance: %llu ms/operation\n",
490 GNUNET_TIME_absolute_get_duration (start_time).rel_value_us / 1000LL /
493 GNUNET_DATASTORE_disconnect (datastore,
499 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
510 * Function called with the result of the initial PUT operation. If
511 * the PUT succeeded, we start the actual benchmark loop, otherwise we
512 * bail out with an error.
516 * @param success #GNUNET_SYSERR on failure
517 * @param min_expiration minimum expiration time required for content to be stored
518 * by the datacache at this time, zero for unknown
519 * @param msg NULL on success, otherwise an error message
522 run_tests (void *cls,
524 struct GNUNET_TIME_Absolute min_expiration,
527 struct CpsRunContext *crc = cls;
529 if (success != GNUNET_YES)
532 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
534 GNUNET_DATASTORE_disconnect (datastore,
539 GNUNET_SCHEDULER_add_now (&run_continuation,
545 * Beginning of the actual execution of the benchmark.
546 * Performs a first test operation (PUT) to verify that
547 * the plugin works at all.
550 * @param cfg configuration to use
551 * @param peer peer handle (unused)
555 const struct GNUNET_CONFIGURATION_Handle *cfg,
556 struct GNUNET_TESTING_Peer *peer)
558 struct CpsRunContext *crc;
559 static struct GNUNET_HashCode zkey;
561 datastore = GNUNET_DATASTORE_connect (cfg);
562 start_time = GNUNET_TIME_absolute_get ();
563 crc = GNUNET_new (struct CpsRunContext);
566 GNUNET_DATASTORE_put (datastore,
570 GNUNET_BLOCK_TYPE_TEST,
572 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
578 "Test 'put' operation failed.\n");
586 * Entry point into the test. Determines which configuration / plugin
587 * we are running with based on the name of the binary and starts
590 * @param argc should be 1
591 * @param argv used to determine plugin / configuration name.
592 * @return 0 on success
600 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
601 GNUNET_snprintf (cfg_name,
603 "test_datastore_api_data_%s.conf",
606 GNUNET_TESTING_peer_run ("perf-gnunet-datastore",
611 FPRINTF (stderr, "%s", "\n");
615 /* end of perf_datastore_api.c */