add changelog
[oweals/gnunet.git] / src / namestore / perf_namestore_api_zone_iteration.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2018 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file namestore/perf_namestore_api_zone_iteration.c
22  * @brief testcase for zone iteration functionality: iterate all zones
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "namestore.h"
29 #include "gnunet_dnsparser_lib.h"
30
31 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
32
33 /**
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
36  * slow systems.
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
39
40 /**
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.
45  */
46 #define BENCHMARK_SIZE 1000
47
48 /**
49  * Maximum record size
50  */
51 #define MAX_REC_SIZE 500
52
53 /**
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
57  * more pronounced.
58  */
59 #define BLOCK_SIZE 100
60
61 static struct GNUNET_NAMESTORE_Handle *nsh;
62
63 static struct GNUNET_SCHEDULER_Task *timeout_task;
64
65 static struct GNUNET_SCHEDULER_Task *t;
66
67 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
68
69 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
70
71 static struct GNUNET_NAMESTORE_QueueEntry *qe;
72
73 static int res;
74
75 static unsigned int off;
76
77 static unsigned int left_until_next;
78
79 static uint8_t seen[1 + BENCHMARK_SIZE / 8];
80
81 static struct GNUNET_TIME_Absolute start;
82
83
84 /**
85  * Terminate everything
86  *
87  * @param cls NULL
88  */
89 static void
90 end (void *cls)
91 {
92   (void) cls;
93   if (NULL != qe)
94   {
95     GNUNET_NAMESTORE_cancel (qe);
96     qe = NULL;
97   }
98   if (NULL != zi)
99   {
100     GNUNET_NAMESTORE_zone_iteration_stop (zi);
101     zi = NULL;
102   }
103   if (NULL != nsh)
104   {
105     GNUNET_NAMESTORE_disconnect (nsh);
106     nsh = NULL;
107   }
108   if (NULL != t)
109   {
110     GNUNET_SCHEDULER_cancel (t);
111     t = NULL;
112   }
113   if (NULL != timeout_task)
114   {
115     GNUNET_SCHEDULER_cancel (timeout_task);
116     timeout_task = NULL;
117   }
118   if (NULL != privkey)
119   {
120     GNUNET_free (privkey);
121     privkey = NULL;
122   }
123 }
124
125
126 /**
127  * End with timeout. As this is a benchmark, we do not
128  * fail hard but return "skipped".
129  */
130 static void
131 timeout (void *cls)
132 {
133   (void) cls;
134   timeout_task = NULL;
135   GNUNET_SCHEDULER_shutdown ();
136   res = 77;
137 }
138
139
140 static struct GNUNET_GNSRECORD_Data *
141 create_record (unsigned int count)
142 {
143   struct GNUNET_GNSRECORD_Data *rd;
144
145   rd = GNUNET_malloc (count + sizeof(struct GNUNET_GNSRECORD_Data));
146   rd->expiration_time = GNUNET_TIME_relative_to_absolute (
147     GNUNET_TIME_UNIT_HOURS).abs_value_us;
148   rd->record_type = TEST_RECORD_TYPE;
149   rd->data_size = count;
150   rd->data = (void *) &rd[1];
151   rd->flags = 0;
152   memset (&rd[1],
153           'a',
154           count);
155   return rd;
156 }
157
158
159 static void
160 zone_end (void *cls)
161 {
162   struct GNUNET_TIME_Relative delay;
163
164   zi = NULL;
165   delay = GNUNET_TIME_absolute_get_duration (start);
166   fprintf (stdout,
167            "Iterating over %u records took %s\n",
168            off,
169            GNUNET_STRINGS_relative_time_to_string (delay,
170                                                    GNUNET_YES));
171   if (BENCHMARK_SIZE == off)
172   {
173     res = 0;
174   }
175   else
176   {
177     GNUNET_break (0);
178     res = 1;
179   }
180   GNUNET_SCHEDULER_shutdown ();
181 }
182
183
184 static void
185 fail_cb (void *cls)
186 {
187   zi = NULL;
188   res = 2;
189   GNUNET_break (0);
190   GNUNET_SCHEDULER_shutdown ();
191 }
192
193
194 static void
195 zone_proc (void *cls,
196            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
197            const char *label,
198            unsigned int rd_count,
199            const struct GNUNET_GNSRECORD_Data *rd)
200 {
201   struct GNUNET_GNSRECORD_Data *wrd;
202   unsigned int xoff;
203
204   GNUNET_assert (NULL != zone);
205   if (1 != sscanf (label,
206                    "l%u",
207                    &xoff))
208   {
209     res = 3;
210     GNUNET_break (0);
211     GNUNET_SCHEDULER_shutdown ();
212     return;
213   }
214   if ((xoff > BENCHMARK_SIZE) ||
215       (0 != (seen[xoff / 8] & (1U << (xoff % 8)))))
216   {
217     res = 3;
218     GNUNET_break (0);
219     GNUNET_SCHEDULER_shutdown ();
220     return;
221   }
222   seen[xoff / 8] |= (1U << (xoff % 8));
223   wrd = create_record (xoff % MAX_REC_SIZE);
224   if ((rd->record_type != wrd->record_type) ||
225       (rd->data_size != wrd->data_size) ||
226       (rd->flags != wrd->flags))
227   {
228     res = 4;
229     GNUNET_break (0);
230     GNUNET_SCHEDULER_shutdown ();
231     GNUNET_free (wrd);
232     return;
233   }
234   if (0 != memcmp (rd->data,
235                    wrd->data,
236                    wrd->data_size))
237   {
238     res = 4;
239     GNUNET_break (0);
240     GNUNET_SCHEDULER_shutdown ();
241     GNUNET_free (wrd);
242     return;
243   }
244   GNUNET_free (wrd);
245   if (0 != GNUNET_memcmp (zone,
246                           privkey))
247   {
248     res = 5;
249     GNUNET_break (0);
250     GNUNET_SCHEDULER_shutdown ();
251     return;
252   }
253   off++;
254   left_until_next--;
255   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256               "Obtained record %u, expecting %u more until asking for mor explicitly\n",
257               off,
258               left_until_next);
259   if (0 == left_until_next)
260   {
261     left_until_next = BLOCK_SIZE;
262     GNUNET_NAMESTORE_zone_iterator_next (zi,
263                                          left_until_next);
264   }
265 }
266
267
268 static void
269 publish_record (void *cls);
270
271
272 static void
273 put_cont (void *cls,
274           int32_t success,
275           const char *emsg)
276 {
277   (void) cls;
278   qe = NULL;
279   if (GNUNET_OK != success)
280   {
281     GNUNET_break (0);
282     GNUNET_SCHEDULER_shutdown ();
283     return;
284   }
285   t = GNUNET_SCHEDULER_add_now (&publish_record,
286                                 NULL);
287 }
288
289
290 static void
291 publish_record (void *cls)
292 {
293   struct GNUNET_GNSRECORD_Data *rd;
294   char *label;
295
296   (void) cls;
297   t = NULL;
298   if (BENCHMARK_SIZE == off)
299   {
300     struct GNUNET_TIME_Relative delay;
301
302     delay = GNUNET_TIME_absolute_get_duration (start);
303     fprintf (stdout,
304              "Inserting %u records took %s\n",
305              off,
306              GNUNET_STRINGS_relative_time_to_string (delay,
307                                                      GNUNET_YES));
308     start = GNUNET_TIME_absolute_get ();
309     off = 0;
310     left_until_next = 1;
311     zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
312                                                 NULL,
313                                                 &fail_cb,
314                                                 NULL,
315                                                 &zone_proc,
316                                                 NULL,
317                                                 &zone_end,
318                                                 NULL);
319     GNUNET_assert (NULL != zi);
320     return;
321   }
322   rd = create_record ((++off) % MAX_REC_SIZE);
323   GNUNET_asprintf (&label,
324                    "l%u",
325                    off);
326   qe = GNUNET_NAMESTORE_records_store (nsh,
327                                        privkey,
328                                        label,
329                                        1, rd,
330                                        &put_cont,
331                                        NULL);
332   GNUNET_free (label);
333   GNUNET_free (rd);
334 }
335
336
337 static void
338 run (void *cls,
339      const struct GNUNET_CONFIGURATION_Handle *cfg,
340      struct GNUNET_TESTING_Peer *peer)
341 {
342   GNUNET_SCHEDULER_add_shutdown (&end,
343                                  NULL);
344   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
345                                                &timeout,
346                                                NULL);
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,
353                                 NULL);
354 }
355
356
357 #include "test_common.c"
358
359
360 int
361 main (int argc,
362       char *argv[])
363 {
364   const char *plugin_name;
365   char *cfg_name;
366
367   SETUP_CFG (plugin_name, cfg_name);
368   res = 1;
369   if (0 !=
370       GNUNET_TESTING_peer_run ("perf-namestore-api-zone-iteration",
371                                cfg_name,
372                                &run,
373                                NULL))
374   {
375     res = 1;
376   }
377   GNUNET_DISK_purge_cfg_dir (cfg_name,
378                              "GNUNET_TEST_HOME");
379   GNUNET_free (cfg_name);
380   return res;
381 }
382
383
384 /* end of perf_namestore_api_zone_iteration.c */