2 This file is part of GNUnet
3 Copyright (C) 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 src/gns/gnunet-gns-benchmark.c
22 * @brief issue many queries to GNS and compute performance statistics
23 * @author Christian Grothoff
26 #include <gnunet_util_lib.h>
27 #include <gnunet_gnsrecord_lib.h>
28 #include <gnunet_gns_service.h>
32 * How long do we wait at least between requests by default?
34 #define DEF_REQUEST_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1)
37 * How long do we wait until we consider a request failed by default?
39 #define DEF_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
43 * We distinguish between different categories of
44 * requests, for which we track statistics separately.
45 * However, this process does not change how it acts
46 * based on the category.
53 * Must be last and match number of categories.
60 * Request we should make. We keep this struct in memory per request,
61 * thus optimizing it is crucial for the overall memory consumption of
68 * Active requests are kept in a DLL.
73 * Active requests are kept in a DLL.
78 * Socket used to make the request, NULL if not active.
80 struct GNUNET_GNS_LookupWithTldRequest *lr;
83 * Hostname we are resolving, allocated at the end of
84 * this struct (optimizing memory consumption by reducing
85 * total number of allocations).
90 * While we are fetching the record, the value is set to the
91 * starting time of the GNS operation.
93 struct GNUNET_TIME_Absolute op_start_time;
96 * Observed latency, set once we got a reply.
98 struct GNUNET_TIME_Relative latency;
101 * Category of the request.
103 enum RequestCategory cat;
111 static struct GNUNET_GNS_Handle *gns;
114 * Number of lookups we performed overall per category.
116 static unsigned int lookups[RC_MAX];
119 * Number of replies we got per category.
121 static unsigned int replies[RC_MAX];
124 * Number of replies we got per category.
126 static unsigned int failures[RC_MAX];
129 * Sum of the observed latencies of successful queries,
132 static struct GNUNET_TIME_Relative latency_sum[RC_MAX];
135 * Active requests are kept in a DLL.
137 static struct Request *act_head;
140 * Active requests are kept in a DLL.
142 static struct Request *act_tail;
145 * Completed successful requests are kept in a DLL.
147 static struct Request *succ_head;
150 * Completed successful requests are kept in a DLL.
152 static struct Request *succ_tail;
155 * Yet to be started requests are kept in a DLL.
157 static struct Request *todo_head;
160 * Yet to be started requests are kept in a DLL.
162 static struct Request *todo_tail;
167 static struct GNUNET_SCHEDULER_Task *t;
170 * Delay between requests.
172 static struct GNUNET_TIME_Relative request_delay;
175 * Timeout for requests.
177 static struct GNUNET_TIME_Relative timeout;
180 * Number of requests we have concurrently active.
182 static unsigned int active_cnt;
185 * Look for GNS2DNS records specifically?
190 * Free @a req and data structures reachable from it.
192 * @param req request to free
195 free_request (struct Request *req)
198 GNUNET_GNS_lookup_with_tld_cancel (req->lr);
204 * Function called with the result of a GNS resolution.
206 * @param cls closure with the `struct Request`
207 * @param gns_tld #GNUNET_YES if GNS lookup was attempted
208 * @param rd_count number of records in @a rd
209 * @param rd the records in reply
212 process_result (void *cls,
215 const struct GNUNET_GNSRECORD_Data *rd)
217 struct Request *req = cls;
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Got response for request `%s'\n",
227 req->latency = GNUNET_TIME_absolute_get_duration (req->op_start_time);
228 GNUNET_CONTAINER_DLL_remove (act_head,
231 GNUNET_CONTAINER_DLL_insert (succ_head,
235 latency_sum[req->cat]
236 = GNUNET_TIME_relative_add (latency_sum[req->cat],
242 * Process request from the queue.
247 process_queue (void *cls)
250 struct GNUNET_TIME_Relative duration;
254 /* check for expired requests */
255 while (NULL != (req = act_head))
257 duration = GNUNET_TIME_absolute_get_duration (req->op_start_time);
258 if (duration.rel_value_us < timeout.rel_value_us)
260 GNUNET_CONTAINER_DLL_remove (act_head,
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Failing request `%s' due to timeout\n",
266 failures[req->cat]++;
270 if (NULL == (req = todo_head))
272 struct GNUNET_TIME_Absolute at;
274 if (NULL == (req = act_head))
276 GNUNET_SCHEDULER_shutdown ();
279 at = GNUNET_TIME_absolute_add (req->op_start_time,
281 t = GNUNET_SCHEDULER_add_at (at,
286 GNUNET_CONTAINER_DLL_remove (todo_head,
289 GNUNET_CONTAINER_DLL_insert_tail (act_head,
294 req->op_start_time = GNUNET_TIME_absolute_get ();
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Starting request `%s' (%u in parallel)\n",
299 req->lr = GNUNET_GNS_lookup_with_tld (gns,
302 ? GNUNET_GNSRECORD_TYPE_GNS2DNS
303 : GNUNET_GNSRECORD_TYPE_ANY,
304 GNUNET_GNS_LO_DEFAULT,
307 t = GNUNET_SCHEDULER_add_delayed (request_delay,
314 * Compare two requests by latency for qsort().
316 * @param c1 pointer to `struct Request *`
317 * @param c2 pointer to `struct Request *`
318 * @return -1 if c1<c2, 1 if c1>c2, 0 if c1==c2.
321 compare_req (const void *c1,
324 const struct Request *r1 = *(void **) c1;
325 const struct Request *r2 = *(void **) c2;
327 if (r1->latency.rel_value_us < r2->latency.rel_value_us)
329 if (r1->latency.rel_value_us > r2->latency.rel_value_us)
336 * Output statistics, then clean up and terminate the process.
341 do_shutdown (void *cls)
344 struct Request **ra[RC_MAX];
345 unsigned int rp[RC_MAX];
348 for (enum RequestCategory rc = 0;rc < RC_MAX;rc++)
350 ra[rc] = GNUNET_new_array (replies[rc],
354 for (req = succ_head;NULL != req; req = req->next)
356 GNUNET_assert (rp[req->cat] < replies[req->cat]);
357 ra[req->cat][rp[req->cat]++] = req;
359 for (enum RequestCategory rc = 0;rc < RC_MAX;rc++)
367 "\tlookups: %u replies: %u failures: %u\n",
375 sizeof (struct Request *),
377 latency_sum[rc] = GNUNET_TIME_relative_divide (latency_sum[rc],
381 GNUNET_STRINGS_relative_time_to_string (latency_sum[rc],
383 off = rp[rc] * 50 / 100;
385 "\tmedian(50): %s\n",
386 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
388 off = rp[rc] * 75 / 100;
390 "\tquantile(75): %s\n",
391 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
393 off = rp[rc] * 90 / 100;
395 "\tquantile(90): %s\n",
396 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
398 off = rp[rc] * 99 / 100;
400 "\tquantile(99): %s\n",
401 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
403 GNUNET_free (ra[rc]);
407 GNUNET_SCHEDULER_cancel (t);
410 while (NULL != (req = act_head))
412 GNUNET_CONTAINER_DLL_remove (act_head,
417 while (NULL != (req = succ_head))
419 GNUNET_CONTAINER_DLL_remove (succ_head,
424 while (NULL != (req = todo_head))
426 GNUNET_CONTAINER_DLL_remove (todo_head,
433 GNUNET_GNS_disconnect (gns);
440 * Add @a hostname to the list of requests to be made.
442 * @param hostname name to resolve
443 * @param cat category of the @a hostname
446 queue (const char *hostname,
447 enum RequestCategory cat)
453 dot = strchr (hostname,
454 (unsigned char) '.');
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 "Refusing invalid hostname `%s' (lacks '.')\n",
462 hlen = strlen (hostname) + 1;
463 req = GNUNET_malloc (sizeof (struct Request) + hlen);
465 req->hostname = (char *) &req[1];
466 GNUNET_memcpy (&req[1],
469 GNUNET_CONTAINER_DLL_insert (todo_head,
476 * Begin processing hostnames from stdin.
481 process_stdin (void *cls)
483 static struct GNUNET_TIME_Absolute last;
484 static uint64_t idot;
497 hn[strlen(in)-1] = '\0'; /* eat newline */
498 if ( (2 != sscanf (in,
505 "Malformed input line `%s', skipping\n",
510 last = GNUNET_TIME_absolute_get ();
512 if (0 == idot % 100000)
514 struct GNUNET_TIME_Relative delta;
516 delta = GNUNET_TIME_absolute_get_duration (last);
517 last = GNUNET_TIME_absolute_get ();
519 "Read 100000 domain names in %s\n",
520 GNUNET_STRINGS_relative_time_to_string (delta,
524 (enum RequestCategory) cat);
527 "Done reading %llu domain names\n",
528 (unsigned long long) idot);
529 t = GNUNET_SCHEDULER_add_now (&process_queue,
535 * Process requests from the queue, then if the queue is
536 * not empty, try again.
539 * @param args remaining command-line arguments
540 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
541 * @param cfg configuration
547 const struct GNUNET_CONFIGURATION_Handle *cfg)
552 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
554 gns = GNUNET_GNS_connect (cfg);
558 GNUNET_SCHEDULER_shutdown ();
561 t = GNUNET_SCHEDULER_add_now (&process_stdin,
567 * Call with list of names with numeric category to query.
571 * @return 0 on success
578 struct GNUNET_GETOPT_CommandLineOption options[] = {
579 GNUNET_GETOPT_option_relative_time ('d',
582 gettext_noop ("how long to wait between queries"),
584 GNUNET_GETOPT_option_relative_time ('t',
587 gettext_noop ("how long to wait for an answer"),
589 GNUNET_GETOPT_option_flag ('2',
591 gettext_noop ("look for GNS2DNS records instead of ANY"),
593 GNUNET_GETOPT_OPTION_END
597 GNUNET_STRINGS_get_utf8_args (argc, argv,
600 timeout = DEF_TIMEOUT;
601 request_delay = DEF_REQUEST_DELAY;
603 GNUNET_PROGRAM_run (argc,
605 "gnunet-gns-benchmark",
606 "resolve GNS names and measure performance",
611 GNUNET_free ((void*) argv);
615 /* end of gnunet-gns-benchmark.c */