2 This file is part of GNUnet.
3 Copyright (C) 2013, 2018 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 namestore/perf_namestore_api_zone_iteration.c
22 * @brief testcase for zone iteration functionality: iterate all zones
23 * @author Christian Grothoff
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "namestore.h"
29 #include "gnunet_dnsparser_lib.h"
31 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
34 * A #BENCHMARK_SIZE of 1000 takes less than a minute on a reasonably
35 * modern system, so 30 minutes should be OK even for very, very
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
41 * The runtime of the benchmark is expected to be linear
42 * for the iteration phase with a *good* database. The FLAT
43 * database uses a quadratic retrieval algorithm,
44 * hence it should be quadratic in the size.
46 #define BENCHMARK_SIZE 1000
51 #define MAX_REC_SIZE 500
54 * How big are the blocks we fetch? Note that the first block is
55 * always just 1 record set per current API. Smaller block
56 * sizes will make quadratic iteration-by-offset penalties
59 #define BLOCK_SIZE 100
61 static struct GNUNET_NAMESTORE_Handle *nsh;
63 static struct GNUNET_SCHEDULER_Task *timeout_task;
65 static struct GNUNET_SCHEDULER_Task *t;
67 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
69 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
71 static struct GNUNET_NAMESTORE_QueueEntry *qe;
75 static unsigned int off;
77 static unsigned int left_until_next;
79 static uint8_t seen[1 + BENCHMARK_SIZE / 8];
81 static struct GNUNET_TIME_Absolute start;
85 * Terminate everything
95 GNUNET_NAMESTORE_cancel (qe);
100 GNUNET_NAMESTORE_zone_iteration_stop (zi);
105 GNUNET_NAMESTORE_disconnect (nsh);
110 GNUNET_SCHEDULER_cancel (t);
113 if (NULL != timeout_task)
115 GNUNET_SCHEDULER_cancel (timeout_task);
120 GNUNET_free (privkey);
127 * End with timeout. As this is a benchmark, we do not
128 * fail hard but return "skipped".
135 GNUNET_SCHEDULER_shutdown ();
140 static struct GNUNET_GNSRECORD_Data *
141 create_record (unsigned int count)
143 struct GNUNET_GNSRECORD_Data *rd;
145 rd = GNUNET_malloc (count + sizeof (struct GNUNET_GNSRECORD_Data));
146 rd->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
147 rd->record_type = TEST_RECORD_TYPE;
148 rd->data_size = count;
149 rd->data = (void *) &rd[1];
161 struct GNUNET_TIME_Relative delay;
164 delay = GNUNET_TIME_absolute_get_duration (start);
166 "Iterating over %u records took %s\n",
168 GNUNET_STRINGS_relative_time_to_string (delay,
170 if (BENCHMARK_SIZE == off)
179 GNUNET_SCHEDULER_shutdown ();
189 GNUNET_SCHEDULER_shutdown ();
194 zone_proc (void *cls,
195 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
197 unsigned int rd_count,
198 const struct GNUNET_GNSRECORD_Data *rd)
200 struct GNUNET_GNSRECORD_Data *wrd;
203 GNUNET_assert (NULL != zone);
204 if (1 != sscanf (label,
210 GNUNET_SCHEDULER_shutdown ();
213 if ( (xoff > BENCHMARK_SIZE) ||
214 (0 != (seen[xoff / 8] & (1U << (xoff % 8)))) )
218 GNUNET_SCHEDULER_shutdown ();
221 seen[xoff / 8] |= (1U << (xoff % 8));
222 wrd = create_record (xoff % MAX_REC_SIZE);
223 if ( (rd->record_type != wrd->record_type) ||
224 (rd->data_size != wrd->data_size) ||
225 (rd->flags != wrd->flags) )
229 GNUNET_SCHEDULER_shutdown ();
233 if (0 != memcmp (rd->data,
239 GNUNET_SCHEDULER_shutdown ();
244 if (0 != memcmp (zone,
246 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
250 GNUNET_SCHEDULER_shutdown ();
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Obtained record %u, expecting %u more until asking for mor explicitly\n",
259 if (0 == left_until_next)
261 left_until_next = BLOCK_SIZE;
262 GNUNET_NAMESTORE_zone_iterator_next (zi,
269 publish_record (void *cls);
279 if (GNUNET_OK != success)
282 GNUNET_SCHEDULER_shutdown ();
285 t = GNUNET_SCHEDULER_add_now (&publish_record,
291 publish_record (void *cls)
293 struct GNUNET_GNSRECORD_Data *rd;
298 if (BENCHMARK_SIZE == off)
300 struct GNUNET_TIME_Relative delay;
302 delay = GNUNET_TIME_absolute_get_duration (start);
304 "Inserting %u records took %s\n",
306 GNUNET_STRINGS_relative_time_to_string (delay,
308 start = GNUNET_TIME_absolute_get ();
311 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
319 GNUNET_assert (NULL != zi);
322 rd = create_record ((++off) % MAX_REC_SIZE);
323 GNUNET_asprintf (&label,
326 qe = GNUNET_NAMESTORE_records_store (nsh,
339 const struct GNUNET_CONFIGURATION_Handle *cfg,
340 struct GNUNET_TESTING_Peer *peer)
342 GNUNET_SCHEDULER_add_shutdown (&end,
344 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
347 nsh = GNUNET_NAMESTORE_connect (cfg);
348 GNUNET_assert (NULL != nsh);
349 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
350 GNUNET_assert (NULL != privkey);
351 start = GNUNET_TIME_absolute_get ();
352 t = GNUNET_SCHEDULER_add_now (&publish_record,
357 #include "test_common.c"
364 const char *plugin_name;
367 SETUP_CFG (plugin_name, cfg_name);
370 GNUNET_TESTING_peer_run ("perf-namestore-api-zone-iteration",
377 GNUNET_DISK_purge_cfg_dir (cfg_name,
379 GNUNET_free (cfg_name);
384 /* end of perf_namestore_api_zone_iteration.c */