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/>.
19 * @file src/gns/gnunet-gns-benchmark.c
20 * @brief issue many queries to GNS and compute performance statistics
21 * @author Christian Grothoff
24 #include <gnunet_util_lib.h>
25 #include <gnunet_gnsrecord_lib.h>
26 #include <gnunet_gns_service.h>
30 * How long do we wait at least between requests by default?
32 #define DEF_REQUEST_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1)
35 * How long do we wait until we consider a request failed by default?
37 #define DEF_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
41 * We distinguish between different categories of
42 * requests, for which we track statistics separately.
43 * However, this process does not change how it acts
44 * based on the category.
51 * Must be last and match number of categories.
58 * Request we should make. We keep this struct in memory per request,
59 * thus optimizing it is crucial for the overall memory consumption of
66 * Active requests are kept in a DLL.
71 * Active requests are kept in a DLL.
76 * Socket used to make the request, NULL if not active.
78 struct GNUNET_GNS_LookupWithTldRequest *lr;
81 * Hostname we are resolving, allocated at the end of
82 * this struct (optimizing memory consumption by reducing
83 * total number of allocations).
88 * While we are fetching the record, the value is set to the
89 * starting time of the GNS operation.
91 struct GNUNET_TIME_Absolute op_start_time;
94 * Observed latency, set once we got a reply.
96 struct GNUNET_TIME_Relative latency;
99 * Category of the request.
101 enum RequestCategory cat;
109 static struct GNUNET_GNS_Handle *gns;
112 * Number of lookups we performed overall per category.
114 static unsigned int lookups[RC_MAX];
117 * Number of replies we got per category.
119 static unsigned int replies[RC_MAX];
122 * Number of replies we got per category.
124 static unsigned int failures[RC_MAX];
127 * Sum of the observed latencies of successful queries,
130 static struct GNUNET_TIME_Relative latency_sum[RC_MAX];
133 * Active requests are kept in a DLL.
135 static struct Request *act_head;
138 * Active requests are kept in a DLL.
140 static struct Request *act_tail;
143 * Completed successful requests are kept in a DLL.
145 static struct Request *succ_head;
148 * Completed successful requests are kept in a DLL.
150 static struct Request *succ_tail;
153 * Yet to be started requests are kept in a DLL.
155 static struct Request *todo_head;
158 * Yet to be started requests are kept in a DLL.
160 static struct Request *todo_tail;
165 static struct GNUNET_SCHEDULER_Task *t;
168 * Delay between requests.
170 static struct GNUNET_TIME_Relative request_delay;
173 * Timeout for requests.
175 static struct GNUNET_TIME_Relative timeout;
178 * Number of requests we have concurrently active.
180 static unsigned int active_cnt;
183 * Look for GNS2DNS records specifically?
188 * Free @a req and data structures reachable from it.
190 * @param req request to free
193 free_request (struct Request *req)
196 GNUNET_GNS_lookup_with_tld_cancel (req->lr);
202 * Function called with the result of a GNS resolution.
204 * @param cls closure with the `struct Request`
205 * @param gns_tld #GNUNET_YES if GNS lookup was attempted
206 * @param rd_count number of records in @a rd
207 * @param rd the records in reply
210 process_result (void *cls,
213 const struct GNUNET_GNSRECORD_Data *rd)
215 struct Request *req = cls;
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Got response for request `%s'\n",
225 req->latency = GNUNET_TIME_absolute_get_duration (req->op_start_time);
226 GNUNET_CONTAINER_DLL_remove (act_head,
229 GNUNET_CONTAINER_DLL_insert (succ_head,
233 latency_sum[req->cat]
234 = GNUNET_TIME_relative_add (latency_sum[req->cat],
240 * Process request from the queue.
245 process_queue (void *cls)
248 struct GNUNET_TIME_Relative duration;
252 /* check for expired requests */
253 while (NULL != (req = act_head))
255 duration = GNUNET_TIME_absolute_get_duration (req->op_start_time);
256 if (duration.rel_value_us < timeout.rel_value_us)
258 GNUNET_CONTAINER_DLL_remove (act_head,
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Failing request `%s' due to timeout\n",
264 failures[req->cat]++;
268 if (NULL == (req = todo_head))
270 struct GNUNET_TIME_Absolute at;
272 if (NULL == (req = act_head))
274 GNUNET_SCHEDULER_shutdown ();
277 at = GNUNET_TIME_absolute_add (req->op_start_time,
279 t = GNUNET_SCHEDULER_add_at (at,
284 GNUNET_CONTAINER_DLL_remove (todo_head,
287 GNUNET_CONTAINER_DLL_insert_tail (act_head,
292 req->op_start_time = GNUNET_TIME_absolute_get ();
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Starting request `%s' (%u in parallel)\n",
297 req->lr = GNUNET_GNS_lookup_with_tld (gns,
300 ? GNUNET_GNSRECORD_TYPE_GNS2DNS
301 : GNUNET_GNSRECORD_TYPE_ANY,
302 GNUNET_GNS_LO_DEFAULT,
305 t = GNUNET_SCHEDULER_add_delayed (request_delay,
312 * Compare two requests by latency for qsort().
314 * @param c1 pointer to `struct Request *`
315 * @param c2 pointer to `struct Request *`
316 * @return -1 if c1<c2, 1 if c1>c2, 0 if c1==c2.
319 compare_req (const void *c1,
322 const struct Request *r1 = *(void **) c1;
323 const struct Request *r2 = *(void **) c2;
325 if (r1->latency.rel_value_us < r2->latency.rel_value_us)
327 if (r1->latency.rel_value_us > r2->latency.rel_value_us)
334 * Output statistics, then clean up and terminate the process.
339 do_shutdown (void *cls)
342 struct Request **ra[RC_MAX];
343 unsigned int rp[RC_MAX];
346 for (enum RequestCategory rc = 0;rc < RC_MAX;rc++)
348 ra[rc] = GNUNET_new_array (replies[rc],
352 for (req = succ_head;NULL != req; req = req->next)
354 GNUNET_assert (rp[req->cat] < replies[req->cat]);
355 ra[req->cat][rp[req->cat]++] = req;
357 for (enum RequestCategory rc = 0;rc < RC_MAX;rc++)
365 "\tlookups: %u replies: %u failures: %u\n",
373 sizeof (struct Request *),
375 latency_sum[rc] = GNUNET_TIME_relative_divide (latency_sum[rc],
379 GNUNET_STRINGS_relative_time_to_string (latency_sum[rc],
381 off = rp[rc] * 50 / 100;
383 "\tmedian(50): %s\n",
384 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
386 off = rp[rc] * 75 / 100;
388 "\tquantile(75): %s\n",
389 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
391 off = rp[rc] * 90 / 100;
393 "\tquantile(90): %s\n",
394 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
396 off = rp[rc] * 99 / 100;
398 "\tquantile(99): %s\n",
399 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
401 GNUNET_free (ra[rc]);
405 GNUNET_SCHEDULER_cancel (t);
408 while (NULL != (req = act_head))
410 GNUNET_CONTAINER_DLL_remove (act_head,
415 while (NULL != (req = succ_head))
417 GNUNET_CONTAINER_DLL_remove (succ_head,
422 while (NULL != (req = todo_head))
424 GNUNET_CONTAINER_DLL_remove (todo_head,
431 GNUNET_GNS_disconnect (gns);
438 * Add @a hostname to the list of requests to be made.
440 * @param hostname name to resolve
441 * @param cat category of the @a hostname
444 queue (const char *hostname,
445 enum RequestCategory cat)
451 dot = strchr (hostname,
452 (unsigned char) '.');
455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
456 "Refusing invalid hostname `%s' (lacks '.')\n",
460 hlen = strlen (hostname) + 1;
461 req = GNUNET_malloc (sizeof (struct Request) + hlen);
463 req->hostname = (char *) &req[1];
464 GNUNET_memcpy (&req[1],
467 GNUNET_CONTAINER_DLL_insert (todo_head,
474 * Begin processing hostnames from stdin.
479 process_stdin (void *cls)
481 static struct GNUNET_TIME_Absolute last;
482 static uint64_t idot;
495 hn[strlen(in)-1] = '\0'; /* eat newline */
496 if ( (2 != sscanf (in,
503 "Malformed input line `%s', skipping\n",
508 last = GNUNET_TIME_absolute_get ();
510 if (0 == idot % 100000)
512 struct GNUNET_TIME_Relative delta;
514 delta = GNUNET_TIME_absolute_get_duration (last);
515 last = GNUNET_TIME_absolute_get ();
517 "Read 100000 domain names in %s\n",
518 GNUNET_STRINGS_relative_time_to_string (delta,
522 (enum RequestCategory) cat);
525 "Done reading %llu domain names\n",
526 (unsigned long long) idot);
527 t = GNUNET_SCHEDULER_add_now (&process_queue,
533 * Process requests from the queue, then if the queue is
534 * not empty, try again.
537 * @param args remaining command-line arguments
538 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
539 * @param cfg configuration
545 const struct GNUNET_CONFIGURATION_Handle *cfg)
550 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
552 gns = GNUNET_GNS_connect (cfg);
556 GNUNET_SCHEDULER_shutdown ();
559 t = GNUNET_SCHEDULER_add_now (&process_stdin,
565 * Call with list of names with numeric category to query.
569 * @return 0 on success
576 struct GNUNET_GETOPT_CommandLineOption options[] = {
577 GNUNET_GETOPT_option_relative_time ('d',
580 gettext_noop ("how long to wait between queries"),
582 GNUNET_GETOPT_option_relative_time ('t',
585 gettext_noop ("how long to wait for an answer"),
587 GNUNET_GETOPT_option_flag ('2',
589 gettext_noop ("look for GNS2DNS records instead of ANY"),
591 GNUNET_GETOPT_OPTION_END
595 GNUNET_STRINGS_get_utf8_args (argc, argv,
598 timeout = DEF_TIMEOUT;
599 request_delay = DEF_REQUEST_DELAY;
601 GNUNET_PROGRAM_run (argc,
603 "gnunet-gns-benchmark",
604 "resolve GNS names and measure performance",
609 GNUNET_free ((void*) argv);
613 /* end of gnunet-gns-benchmark.c */