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);
122 * End with timeout. As this is a benchmark, we do not
123 * fail hard but return "skipped".
130 GNUNET_SCHEDULER_shutdown ();
135 static struct GNUNET_GNSRECORD_Data *
136 create_record (unsigned int count)
138 struct GNUNET_GNSRECORD_Data *rd;
140 rd = GNUNET_malloc (count + sizeof(struct GNUNET_GNSRECORD_Data));
141 rd->expiration_time = GNUNET_TIME_relative_to_absolute (
142 GNUNET_TIME_UNIT_HOURS).abs_value_us;
143 rd->record_type = TEST_RECORD_TYPE;
144 rd->data_size = count;
145 rd->data = (void *) &rd[1];
157 struct GNUNET_TIME_Relative delay;
160 delay = GNUNET_TIME_absolute_get_duration (start);
162 "Iterating over %u records took %s\n",
164 GNUNET_STRINGS_relative_time_to_string (delay,
166 if (BENCHMARK_SIZE == off)
175 GNUNET_SCHEDULER_shutdown ();
185 GNUNET_SCHEDULER_shutdown ();
190 zone_proc (void *cls,
191 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
193 unsigned int rd_count,
194 const struct GNUNET_GNSRECORD_Data *rd)
196 struct GNUNET_GNSRECORD_Data *wrd;
199 GNUNET_assert (NULL != zone);
200 if (1 != sscanf (label,
206 GNUNET_SCHEDULER_shutdown ();
209 if ((xoff > BENCHMARK_SIZE) ||
210 (0 != (seen[xoff / 8] & (1U << (xoff % 8)))))
214 GNUNET_SCHEDULER_shutdown ();
217 seen[xoff / 8] |= (1U << (xoff % 8));
218 wrd = create_record (xoff % MAX_REC_SIZE);
219 if ((rd->record_type != wrd->record_type) ||
220 (rd->data_size != wrd->data_size) ||
221 (rd->flags != wrd->flags))
225 GNUNET_SCHEDULER_shutdown ();
229 if (0 != memcmp (rd->data,
235 GNUNET_SCHEDULER_shutdown ();
240 if (0 != GNUNET_memcmp (zone,
245 GNUNET_SCHEDULER_shutdown ();
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Obtained record %u, expecting %u more until asking for mor explicitly\n",
254 if (0 == left_until_next)
256 left_until_next = BLOCK_SIZE;
257 GNUNET_NAMESTORE_zone_iterator_next (zi,
264 publish_record (void *cls);
274 if (GNUNET_OK != success)
277 GNUNET_SCHEDULER_shutdown ();
280 t = GNUNET_SCHEDULER_add_now (&publish_record,
286 publish_record (void *cls)
288 struct GNUNET_GNSRECORD_Data *rd;
293 if (BENCHMARK_SIZE == off)
295 struct GNUNET_TIME_Relative delay;
297 delay = GNUNET_TIME_absolute_get_duration (start);
299 "Inserting %u records took %s\n",
301 GNUNET_STRINGS_relative_time_to_string (delay,
303 start = GNUNET_TIME_absolute_get ();
306 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
314 GNUNET_assert (NULL != zi);
317 rd = create_record ((++off) % MAX_REC_SIZE);
318 GNUNET_asprintf (&label,
321 qe = GNUNET_NAMESTORE_records_store (nsh,
334 const struct GNUNET_CONFIGURATION_Handle *cfg,
335 struct GNUNET_TESTING_Peer *peer)
337 GNUNET_SCHEDULER_add_shutdown (&end,
339 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
342 nsh = GNUNET_NAMESTORE_connect (cfg);
343 GNUNET_assert (NULL != nsh);
344 GNUNET_CRYPTO_ecdsa_key_create (&privkey);
345 start = GNUNET_TIME_absolute_get ();
346 t = GNUNET_SCHEDULER_add_now (&publish_record,
351 #include "test_common.c"
358 const char *plugin_name;
361 SETUP_CFG (plugin_name, cfg_name);
364 GNUNET_TESTING_peer_run ("perf-namestore-api-zone-iteration",
371 GNUNET_DISK_purge_cfg_dir (cfg_name,
373 GNUNET_free (cfg_name);
378 /* end of perf_namestore_api_zone_iteration.c */