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 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.
16 * @file src/gns/gnunet-gns-benchmark.c
17 * @brief issue many queries to GNS and compute performance statistics
18 * @author Christian Grothoff
21 #include <gnunet_util_lib.h>
22 #include <gnunet_gnsrecord_lib.h>
23 #include <gnunet_gns_service.h>
27 * How long do we wait at least between requests by default?
29 #define DEF_REQUEST_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1)
32 * How long do we wait until we consider a request failed by default?
34 #define DEF_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
38 * We distinguish between different categories of
39 * requests, for which we track statistics separately.
40 * However, this process does not change how it acts
41 * based on the category.
48 * Must be last and match number of categories.
55 * Request we should make. We keep this struct in memory per request,
56 * thus optimizing it is crucial for the overall memory consumption of
63 * Active requests are kept in a DLL.
68 * Active requests are kept in a DLL.
73 * Socket used to make the request, NULL if not active.
75 struct GNUNET_GNS_LookupWithTldRequest *lr;
78 * Hostname we are resolving, allocated at the end of
79 * this struct (optimizing memory consumption by reducing
80 * total number of allocations).
85 * While we are fetching the record, the value is set to the
86 * starting time of the GNS operation.
88 struct GNUNET_TIME_Absolute op_start_time;
91 * Observed latency, set once we got a reply.
93 struct GNUNET_TIME_Relative latency;
96 * Category of the request.
98 enum RequestCategory cat;
106 static struct GNUNET_GNS_Handle *gns;
109 * Number of lookups we performed overall per category.
111 static unsigned int lookups[RC_MAX];
114 * Number of replies we got per category.
116 static unsigned int replies[RC_MAX];
119 * Number of replies we got per category.
121 static unsigned int failures[RC_MAX];
124 * Sum of the observed latencies of successful queries,
127 static struct GNUNET_TIME_Relative latency_sum[RC_MAX];
130 * Active requests are kept in a DLL.
132 static struct Request *act_head;
135 * Active requests are kept in a DLL.
137 static struct Request *act_tail;
140 * Completed successful requests are kept in a DLL.
142 static struct Request *succ_head;
145 * Completed successful requests are kept in a DLL.
147 static struct Request *succ_tail;
150 * Yet to be started requests are kept in a DLL.
152 static struct Request *todo_head;
155 * Yet to be started requests are kept in a DLL.
157 static struct Request *todo_tail;
162 static struct GNUNET_SCHEDULER_Task *t;
165 * Delay between requests.
167 static struct GNUNET_TIME_Relative request_delay;
170 * Timeout for requests.
172 static struct GNUNET_TIME_Relative timeout;
175 * Number of requests we have concurrently active.
177 static unsigned int active_cnt;
180 * Look for GNS2DNS records specifically?
185 * Free @a req and data structures reachable from it.
187 * @param req request to free
190 free_request (struct Request *req)
193 GNUNET_GNS_lookup_with_tld_cancel (req->lr);
199 * Function called with the result of a GNS resolution.
201 * @param cls closure with the `struct Request`
202 * @param gns_tld #GNUNET_YES if GNS lookup was attempted
203 * @param rd_count number of records in @a rd
204 * @param rd the records in reply
207 process_result (void *cls,
210 const struct GNUNET_GNSRECORD_Data *rd)
212 struct Request *req = cls;
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Got response for request `%s'\n",
222 req->latency = GNUNET_TIME_absolute_get_duration (req->op_start_time);
223 GNUNET_CONTAINER_DLL_remove (act_head,
226 GNUNET_CONTAINER_DLL_insert (succ_head,
230 latency_sum[req->cat]
231 = GNUNET_TIME_relative_add (latency_sum[req->cat],
237 * Process request from the queue.
242 process_queue (void *cls)
245 struct GNUNET_TIME_Relative duration;
249 /* check for expired requests */
250 while (NULL != (req = act_head))
252 duration = GNUNET_TIME_absolute_get_duration (req->op_start_time);
253 if (duration.rel_value_us < timeout.rel_value_us)
255 GNUNET_CONTAINER_DLL_remove (act_head,
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Failing request `%s' due to timeout\n",
261 failures[req->cat]++;
265 if (NULL == (req = todo_head))
267 struct GNUNET_TIME_Absolute at;
269 if (NULL == (req = act_head))
271 GNUNET_SCHEDULER_shutdown ();
274 at = GNUNET_TIME_absolute_add (req->op_start_time,
276 t = GNUNET_SCHEDULER_add_at (at,
281 GNUNET_CONTAINER_DLL_remove (todo_head,
284 GNUNET_CONTAINER_DLL_insert_tail (act_head,
289 req->op_start_time = GNUNET_TIME_absolute_get ();
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Starting request `%s' (%u in parallel)\n",
294 req->lr = GNUNET_GNS_lookup_with_tld (gns,
297 ? GNUNET_GNSRECORD_TYPE_GNS2DNS
298 : GNUNET_GNSRECORD_TYPE_ANY,
299 GNUNET_GNS_LO_DEFAULT,
302 t = GNUNET_SCHEDULER_add_delayed (request_delay,
309 * Compare two requests by latency for qsort().
311 * @param c1 pointer to `struct Request *`
312 * @param c2 pointer to `struct Request *`
313 * @return -1 if c1<c2, 1 if c1>c2, 0 if c1==c2.
316 compare_req (const void *c1,
319 const struct Request *r1 = *(void **) c1;
320 const struct Request *r2 = *(void **) c2;
322 if (r1->latency.rel_value_us < r2->latency.rel_value_us)
324 if (r1->latency.rel_value_us > r2->latency.rel_value_us)
331 * Output statistics, then clean up and terminate the process.
336 do_shutdown (void *cls)
339 struct Request **ra[RC_MAX];
340 unsigned int rp[RC_MAX];
343 for (enum RequestCategory rc = 0;rc < RC_MAX;rc++)
345 ra[rc] = GNUNET_new_array (replies[rc],
349 for (req = succ_head;NULL != req; req = req->next)
351 GNUNET_assert (rp[req->cat] < replies[req->cat]);
352 ra[req->cat][rp[req->cat]++] = req;
354 for (enum RequestCategory rc = 0;rc < RC_MAX;rc++)
362 "\tlookups: %u replies: %u failures: %u\n",
370 sizeof (struct Request *),
372 latency_sum[rc] = GNUNET_TIME_relative_divide (latency_sum[rc],
376 GNUNET_STRINGS_relative_time_to_string (latency_sum[rc],
378 off = rp[rc] * 50 / 100;
380 "\tmedian(50): %s\n",
381 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
383 off = rp[rc] * 75 / 100;
385 "\tquantile(75): %s\n",
386 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
388 off = rp[rc] * 90 / 100;
390 "\tquantile(90): %s\n",
391 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
393 off = rp[rc] * 99 / 100;
395 "\tquantile(99): %s\n",
396 GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
398 GNUNET_free (ra[rc]);
402 GNUNET_SCHEDULER_cancel (t);
405 while (NULL != (req = act_head))
407 GNUNET_CONTAINER_DLL_remove (act_head,
412 while (NULL != (req = succ_head))
414 GNUNET_CONTAINER_DLL_remove (succ_head,
419 while (NULL != (req = todo_head))
421 GNUNET_CONTAINER_DLL_remove (todo_head,
428 GNUNET_GNS_disconnect (gns);
435 * Add @a hostname to the list of requests to be made.
437 * @param hostname name to resolve
438 * @param cat category of the @a hostname
441 queue (const char *hostname,
442 enum RequestCategory cat)
448 dot = strchr (hostname,
449 (unsigned char) '.');
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 "Refusing invalid hostname `%s' (lacks '.')\n",
457 hlen = strlen (hostname) + 1;
458 req = GNUNET_malloc (sizeof (struct Request) + hlen);
460 req->hostname = (char *) &req[1];
461 GNUNET_memcpy (&req[1],
464 GNUNET_CONTAINER_DLL_insert (todo_head,
471 * Begin processing hostnames from stdin.
476 process_stdin (void *cls)
478 static struct GNUNET_TIME_Absolute last;
479 static uint64_t idot;
492 hn[strlen(in)-1] = '\0'; /* eat newline */
493 if ( (2 != sscanf (in,
500 "Malformed input line `%s', skipping\n",
505 last = GNUNET_TIME_absolute_get ();
507 if (0 == idot % 100000)
509 struct GNUNET_TIME_Relative delta;
511 delta = GNUNET_TIME_absolute_get_duration (last);
512 last = GNUNET_TIME_absolute_get ();
514 "Read 100000 domain names in %s\n",
515 GNUNET_STRINGS_relative_time_to_string (delta,
519 (enum RequestCategory) cat);
522 "Done reading %llu domain names\n",
523 (unsigned long long) idot);
524 t = GNUNET_SCHEDULER_add_now (&process_queue,
530 * Process requests from the queue, then if the queue is
531 * not empty, try again.
534 * @param args remaining command-line arguments
535 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
536 * @param cfg configuration
542 const struct GNUNET_CONFIGURATION_Handle *cfg)
547 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
549 gns = GNUNET_GNS_connect (cfg);
553 GNUNET_SCHEDULER_shutdown ();
556 t = GNUNET_SCHEDULER_add_now (&process_stdin,
562 * Call with list of names with numeric category to query.
566 * @return 0 on success
573 struct GNUNET_GETOPT_CommandLineOption options[] = {
574 GNUNET_GETOPT_option_relative_time ('d',
577 gettext_noop ("how long to wait between queries"),
579 GNUNET_GETOPT_option_relative_time ('t',
582 gettext_noop ("how long to wait for an answer"),
584 GNUNET_GETOPT_option_flag ('2',
586 gettext_noop ("look for GNS2DNS records instead of ANY"),
588 GNUNET_GETOPT_OPTION_END
592 GNUNET_STRINGS_get_utf8_args (argc, argv,
595 timeout = DEF_TIMEOUT;
596 request_delay = DEF_REQUEST_DELAY;
598 GNUNET_PROGRAM_run (argc,
600 "gnunet-gns-benchmark",
601 "resolve GNS names and measure performance",
606 GNUNET_free ((void*) argv);
610 /* end of gnunet-gns-benchmark.c */