update clang-format
[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 (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];
150   rd->flags = 0;
151   memset (&rd[1],
152           'a',
153           count);
154   return rd;
155 }
156
157
158 static void
159 zone_end (void *cls)
160 {
161   struct GNUNET_TIME_Relative delay;
162
163   zi = NULL;
164   delay = GNUNET_TIME_absolute_get_duration (start);
165   fprintf (stdout,
166            "Iterating over %u records took %s\n",
167            off,
168            GNUNET_STRINGS_relative_time_to_string (delay,
169                                                    GNUNET_YES));
170   if (BENCHMARK_SIZE == off)
171   {
172     res = 0;
173   }
174   else
175   {
176     GNUNET_break (0);
177     res = 1;
178   }
179   GNUNET_SCHEDULER_shutdown ();
180 }
181
182
183 static void
184 fail_cb (void *cls)
185 {
186   zi = NULL;
187   res = 2;
188   GNUNET_break (0);
189   GNUNET_SCHEDULER_shutdown ();
190 }
191
192
193 static void
194 zone_proc (void *cls,
195            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
196            const char *label,
197            unsigned int rd_count,
198            const struct GNUNET_GNSRECORD_Data *rd)
199 {
200   struct GNUNET_GNSRECORD_Data *wrd;
201   unsigned int xoff;
202
203   GNUNET_assert (NULL != zone);
204   if (1 != sscanf (label,
205                    "l%u",
206                    &xoff))
207   {
208     res = 3;
209     GNUNET_break (0);
210     GNUNET_SCHEDULER_shutdown ();
211     return;
212   }
213   if ( (xoff > BENCHMARK_SIZE) ||
214        (0 != (seen[xoff / 8] & (1U << (xoff % 8)))) )
215   {
216     res = 3;
217     GNUNET_break (0);
218     GNUNET_SCHEDULER_shutdown ();
219     return;
220   }
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) )
226   {
227     res = 4;
228     GNUNET_break (0);
229     GNUNET_SCHEDULER_shutdown ();
230     GNUNET_free (wrd);
231     return;
232   }
233   if (0 != memcmp (rd->data,
234                    wrd->data,
235                    wrd->data_size))
236   {
237     res = 4;
238     GNUNET_break (0);
239     GNUNET_SCHEDULER_shutdown ();
240     GNUNET_free (wrd);
241     return;
242   }
243   GNUNET_free (wrd);
244   if (0 != memcmp (zone,
245                    privkey,
246                    sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
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 */