gnunet-gns-benchmark.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2018 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file src/gns/gnunet-gns-benchmark.c
  18. * @brief issue many queries to GNS and compute performance statistics
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include <gnunet_util_lib.h>
  23. #include <gnunet_gnsrecord_lib.h>
  24. #include <gnunet_gns_service.h>
  25. /**
  26. * How long do we wait at least between requests by default?
  27. */
  28. #define DEF_REQUEST_DELAY GNUNET_TIME_relative_multiply ( \
  29. GNUNET_TIME_UNIT_MILLISECONDS, 1)
  30. /**
  31. * How long do we wait until we consider a request failed by default?
  32. */
  33. #define DEF_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
  34. /**
  35. * We distinguish between different categories of
  36. * requests, for which we track statistics separately.
  37. * However, this process does not change how it acts
  38. * based on the category.
  39. */
  40. enum RequestCategory
  41. {
  42. RC_SHARED = 0,
  43. RC_PRIVATE = 1,
  44. /**
  45. * Must be last and match number of categories.
  46. */
  47. RC_MAX = 2
  48. };
  49. /**
  50. * Request we should make. We keep this struct in memory per request,
  51. * thus optimizing it is crucial for the overall memory consumption of
  52. * the zone importer.
  53. */
  54. struct Request
  55. {
  56. /**
  57. * Active requests are kept in a DLL.
  58. */
  59. struct Request *next;
  60. /**
  61. * Active requests are kept in a DLL.
  62. */
  63. struct Request *prev;
  64. /**
  65. * Socket used to make the request, NULL if not active.
  66. */
  67. struct GNUNET_GNS_LookupWithTldRequest *lr;
  68. /**
  69. * Hostname we are resolving, allocated at the end of
  70. * this struct (optimizing memory consumption by reducing
  71. * total number of allocations).
  72. */
  73. const char *hostname;
  74. /**
  75. * While we are fetching the record, the value is set to the
  76. * starting time of the GNS operation.
  77. */
  78. struct GNUNET_TIME_Absolute op_start_time;
  79. /**
  80. * Observed latency, set once we got a reply.
  81. */
  82. struct GNUNET_TIME_Relative latency;
  83. /**
  84. * Category of the request.
  85. */
  86. enum RequestCategory cat;
  87. };
  88. /**
  89. * GNS handle.
  90. */
  91. static struct GNUNET_GNS_Handle *gns;
  92. /**
  93. * Number of lookups we performed overall per category.
  94. */
  95. static unsigned int lookups[RC_MAX];
  96. /**
  97. * Number of replies we got per category.
  98. */
  99. static unsigned int replies[RC_MAX];
  100. /**
  101. * Number of replies we got per category.
  102. */
  103. static unsigned int failures[RC_MAX];
  104. /**
  105. * Sum of the observed latencies of successful queries,
  106. * per category.
  107. */
  108. static struct GNUNET_TIME_Relative latency_sum[RC_MAX];
  109. /**
  110. * Active requests are kept in a DLL.
  111. */
  112. static struct Request *act_head;
  113. /**
  114. * Active requests are kept in a DLL.
  115. */
  116. static struct Request *act_tail;
  117. /**
  118. * Completed successful requests are kept in a DLL.
  119. */
  120. static struct Request *succ_head;
  121. /**
  122. * Completed successful requests are kept in a DLL.
  123. */
  124. static struct Request *succ_tail;
  125. /**
  126. * Yet to be started requests are kept in a DLL.
  127. */
  128. static struct Request *todo_head;
  129. /**
  130. * Yet to be started requests are kept in a DLL.
  131. */
  132. static struct Request *todo_tail;
  133. /**
  134. * Main task.
  135. */
  136. static struct GNUNET_SCHEDULER_Task *t;
  137. /**
  138. * Delay between requests.
  139. */
  140. static struct GNUNET_TIME_Relative request_delay;
  141. /**
  142. * Timeout for requests.
  143. */
  144. static struct GNUNET_TIME_Relative timeout;
  145. /**
  146. * Number of requests we have concurrently active.
  147. */
  148. static unsigned int active_cnt;
  149. /**
  150. * Look for GNS2DNS records specifically?
  151. */
  152. static int g2d;
  153. /**
  154. * Free @a req and data structures reachable from it.
  155. *
  156. * @param req request to free
  157. */
  158. static void
  159. free_request (struct Request *req)
  160. {
  161. if (NULL != req->lr)
  162. GNUNET_GNS_lookup_with_tld_cancel (req->lr);
  163. GNUNET_free (req);
  164. }
  165. /**
  166. * Function called with the result of a GNS resolution.
  167. *
  168. * @param cls closure with the `struct Request`
  169. * @param gns_tld #GNUNET_YES if GNS lookup was attempted
  170. * @param rd_count number of records in @a rd
  171. * @param rd the records in reply
  172. */
  173. static void
  174. process_result (void *cls,
  175. int gns_tld,
  176. uint32_t rd_count,
  177. const struct GNUNET_GNSRECORD_Data *rd)
  178. {
  179. struct Request *req = cls;
  180. (void) gns_tld;
  181. (void) rd_count;
  182. (void) rd;
  183. active_cnt--;
  184. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  185. "Got response for request `%s'\n",
  186. req->hostname);
  187. req->lr = NULL;
  188. req->latency = GNUNET_TIME_absolute_get_duration (req->op_start_time);
  189. GNUNET_CONTAINER_DLL_remove (act_head,
  190. act_tail,
  191. req);
  192. GNUNET_CONTAINER_DLL_insert (succ_head,
  193. succ_tail,
  194. req);
  195. replies[req->cat]++;
  196. latency_sum[req->cat]
  197. = GNUNET_TIME_relative_add (latency_sum[req->cat],
  198. req->latency);
  199. }
  200. /**
  201. * Process request from the queue.
  202. *
  203. * @param cls NULL
  204. */
  205. static void
  206. process_queue (void *cls)
  207. {
  208. struct Request *req;
  209. struct GNUNET_TIME_Relative duration;
  210. (void) cls;
  211. t = NULL;
  212. /* check for expired requests */
  213. while (NULL != (req = act_head))
  214. {
  215. duration = GNUNET_TIME_absolute_get_duration (req->op_start_time);
  216. if (duration.rel_value_us < timeout.rel_value_us)
  217. break;
  218. GNUNET_CONTAINER_DLL_remove (act_head,
  219. act_tail,
  220. req);
  221. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  222. "Failing request `%s' due to timeout\n",
  223. req->hostname);
  224. failures[req->cat]++;
  225. active_cnt--;
  226. free_request (req);
  227. }
  228. if (NULL == (req = todo_head))
  229. {
  230. struct GNUNET_TIME_Absolute at;
  231. if (NULL == (req = act_head))
  232. {
  233. GNUNET_SCHEDULER_shutdown ();
  234. return;
  235. }
  236. at = GNUNET_TIME_absolute_add (req->op_start_time,
  237. timeout);
  238. t = GNUNET_SCHEDULER_add_at (at,
  239. &process_queue,
  240. NULL);
  241. return;
  242. }
  243. GNUNET_CONTAINER_DLL_remove (todo_head,
  244. todo_tail,
  245. req);
  246. GNUNET_CONTAINER_DLL_insert_tail (act_head,
  247. act_tail,
  248. req);
  249. lookups[req->cat]++;
  250. active_cnt++;
  251. req->op_start_time = GNUNET_TIME_absolute_get ();
  252. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  253. "Starting request `%s' (%u in parallel)\n",
  254. req->hostname,
  255. active_cnt);
  256. req->lr = GNUNET_GNS_lookup_with_tld (gns,
  257. req->hostname,
  258. g2d
  259. ? GNUNET_GNSRECORD_TYPE_GNS2DNS
  260. : GNUNET_GNSRECORD_TYPE_ANY,
  261. GNUNET_GNS_LO_DEFAULT,
  262. &process_result,
  263. req);
  264. t = GNUNET_SCHEDULER_add_delayed (request_delay,
  265. &process_queue,
  266. NULL);
  267. }
  268. /**
  269. * Compare two requests by latency for qsort().
  270. *
  271. * @param c1 pointer to `struct Request *`
  272. * @param c2 pointer to `struct Request *`
  273. * @return -1 if c1<c2, 1 if c1>c2, 0 if c1==c2.
  274. */
  275. static int
  276. compare_req (const void *c1,
  277. const void *c2)
  278. {
  279. const struct Request *r1 = *(void **) c1;
  280. const struct Request *r2 = *(void **) c2;
  281. if (r1->latency.rel_value_us < r2->latency.rel_value_us)
  282. return -1;
  283. if (r1->latency.rel_value_us > r2->latency.rel_value_us)
  284. return 1;
  285. return 0;
  286. }
  287. /**
  288. * Output statistics, then clean up and terminate the process.
  289. *
  290. * @param cls NULL
  291. */
  292. static void
  293. do_shutdown (void *cls)
  294. {
  295. struct Request *req;
  296. struct Request **ra[RC_MAX];
  297. unsigned int rp[RC_MAX];
  298. (void) cls;
  299. for (enum RequestCategory rc = 0; rc < RC_MAX; rc++)
  300. {
  301. ra[rc] = GNUNET_new_array (replies[rc],
  302. struct Request *);
  303. rp[rc] = 0;
  304. }
  305. for (req = succ_head; NULL != req; req = req->next)
  306. {
  307. GNUNET_assert (rp[req->cat] < replies[req->cat]);
  308. ra[req->cat][rp[req->cat]++] = req;
  309. }
  310. for (enum RequestCategory rc = 0; rc < RC_MAX; rc++)
  311. {
  312. unsigned int off;
  313. fprintf (stdout,
  314. "Category %u\n",
  315. rc);
  316. fprintf (stdout,
  317. "\tlookups: %u replies: %u failures: %u\n",
  318. lookups[rc],
  319. replies[rc],
  320. failures[rc]);
  321. if (0 == rp[rc])
  322. continue;
  323. qsort (ra[rc],
  324. rp[rc],
  325. sizeof(struct Request *),
  326. &compare_req);
  327. latency_sum[rc] = GNUNET_TIME_relative_divide (latency_sum[rc],
  328. replies[rc]);
  329. fprintf (stdout,
  330. "\taverage: %s\n",
  331. GNUNET_STRINGS_relative_time_to_string (latency_sum[rc],
  332. GNUNET_YES));
  333. off = rp[rc] * 50 / 100;
  334. fprintf (stdout,
  335. "\tmedian(50): %s\n",
  336. GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
  337. GNUNET_YES));
  338. off = rp[rc] * 75 / 100;
  339. fprintf (stdout,
  340. "\tquantile(75): %s\n",
  341. GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
  342. GNUNET_YES));
  343. off = rp[rc] * 90 / 100;
  344. fprintf (stdout,
  345. "\tquantile(90): %s\n",
  346. GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
  347. GNUNET_YES));
  348. off = rp[rc] * 99 / 100;
  349. fprintf (stdout,
  350. "\tquantile(99): %s\n",
  351. GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
  352. GNUNET_YES));
  353. GNUNET_free (ra[rc]);
  354. }
  355. if (NULL != t)
  356. {
  357. GNUNET_SCHEDULER_cancel (t);
  358. t = NULL;
  359. }
  360. while (NULL != (req = act_head))
  361. {
  362. GNUNET_CONTAINER_DLL_remove (act_head,
  363. act_tail,
  364. req);
  365. free_request (req);
  366. }
  367. while (NULL != (req = succ_head))
  368. {
  369. GNUNET_CONTAINER_DLL_remove (succ_head,
  370. succ_tail,
  371. req);
  372. free_request (req);
  373. }
  374. while (NULL != (req = todo_head))
  375. {
  376. GNUNET_CONTAINER_DLL_remove (todo_head,
  377. todo_tail,
  378. req);
  379. free_request (req);
  380. }
  381. if (NULL != gns)
  382. {
  383. GNUNET_GNS_disconnect (gns);
  384. gns = NULL;
  385. }
  386. }
  387. /**
  388. * Add @a hostname to the list of requests to be made.
  389. *
  390. * @param hostname name to resolve
  391. * @param cat category of the @a hostname
  392. */
  393. static void
  394. queue (const char *hostname,
  395. enum RequestCategory cat)
  396. {
  397. struct Request *req;
  398. const char *dot;
  399. size_t hlen;
  400. dot = strchr (hostname,
  401. (unsigned char) '.');
  402. if (NULL == dot)
  403. {
  404. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  405. "Refusing invalid hostname `%s' (lacks '.')\n",
  406. hostname);
  407. return;
  408. }
  409. hlen = strlen (hostname) + 1;
  410. req = GNUNET_malloc (sizeof(struct Request) + hlen);
  411. req->cat = cat;
  412. req->hostname = (char *) &req[1];
  413. GNUNET_memcpy (&req[1],
  414. hostname,
  415. hlen);
  416. GNUNET_CONTAINER_DLL_insert (todo_head,
  417. todo_tail,
  418. req);
  419. }
  420. /**
  421. * Begin processing hostnames from stdin.
  422. *
  423. * @param cls NULL
  424. */
  425. static void
  426. process_stdin (void *cls)
  427. {
  428. static struct GNUNET_TIME_Absolute last;
  429. static uint64_t idot;
  430. unsigned int cat;
  431. char hn[256];
  432. char in[270];
  433. (void) cls;
  434. t = NULL;
  435. while (NULL !=
  436. fgets (in,
  437. sizeof(in),
  438. stdin))
  439. {
  440. if (strlen (in) > 0)
  441. hn[strlen (in) - 1] = '\0'; /* eat newline */
  442. if ((2 != sscanf (in,
  443. "%u %255s",
  444. &cat,
  445. hn)) ||
  446. (cat >= RC_MAX))
  447. {
  448. fprintf (stderr,
  449. "Malformed input line `%s', skipping\n",
  450. in);
  451. continue;
  452. }
  453. if (0 == idot)
  454. last = GNUNET_TIME_absolute_get ();
  455. idot++;
  456. if (0 == idot % 100000)
  457. {
  458. struct GNUNET_TIME_Relative delta;
  459. delta = GNUNET_TIME_absolute_get_duration (last);
  460. last = GNUNET_TIME_absolute_get ();
  461. fprintf (stderr,
  462. "Read 100000 domain names in %s\n",
  463. GNUNET_STRINGS_relative_time_to_string (delta,
  464. GNUNET_YES));
  465. }
  466. queue (hn,
  467. (enum RequestCategory) cat);
  468. }
  469. fprintf (stderr,
  470. "Done reading %llu domain names\n",
  471. (unsigned long long) idot);
  472. t = GNUNET_SCHEDULER_add_now (&process_queue,
  473. NULL);
  474. }
  475. /**
  476. * Process requests from the queue, then if the queue is
  477. * not empty, try again.
  478. *
  479. * @param cls NULL
  480. * @param args remaining command-line arguments
  481. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  482. * @param cfg configuration
  483. */
  484. static void
  485. run (void *cls,
  486. char *const *args,
  487. const char *cfgfile,
  488. const struct GNUNET_CONFIGURATION_Handle *cfg)
  489. {
  490. (void) cls;
  491. (void) args;
  492. (void) cfgfile;
  493. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  494. NULL);
  495. gns = GNUNET_GNS_connect (cfg);
  496. if (NULL == gns)
  497. {
  498. GNUNET_break (0);
  499. GNUNET_SCHEDULER_shutdown ();
  500. return;
  501. }
  502. t = GNUNET_SCHEDULER_add_now (&process_stdin,
  503. NULL);
  504. }
  505. /**
  506. * Call with list of names with numeric category to query.
  507. *
  508. * @param argc unused
  509. * @param argv unused
  510. * @return 0 on success
  511. */
  512. int
  513. main (int argc,
  514. char *const*argv)
  515. {
  516. int ret = 0;
  517. struct GNUNET_GETOPT_CommandLineOption options[] = {
  518. GNUNET_GETOPT_option_relative_time ('d',
  519. "delay",
  520. "RELATIVETIME",
  521. gettext_noop (
  522. "how long to wait between queries"),
  523. &request_delay),
  524. GNUNET_GETOPT_option_relative_time ('t',
  525. "timeout",
  526. "RELATIVETIME",
  527. gettext_noop (
  528. "how long to wait for an answer"),
  529. &timeout),
  530. GNUNET_GETOPT_option_flag ('2',
  531. "g2d",
  532. gettext_noop (
  533. "look for GNS2DNS records instead of ANY"),
  534. &g2d),
  535. GNUNET_GETOPT_OPTION_END
  536. };
  537. if (GNUNET_OK !=
  538. GNUNET_STRINGS_get_utf8_args (argc, argv,
  539. &argc, &argv))
  540. return 2;
  541. timeout = DEF_TIMEOUT;
  542. request_delay = DEF_REQUEST_DELAY;
  543. if (GNUNET_OK !=
  544. GNUNET_PROGRAM_run (argc,
  545. argv,
  546. "gnunet-gns-benchmark",
  547. "resolve GNS names and measure performance",
  548. options,
  549. &run,
  550. NULL))
  551. ret = 1;
  552. GNUNET_free_nz ((void *) argv);
  553. return ret;
  554. }
  555. /* end of gnunet-gns-benchmark.c */