aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gnunet-gns-benchmark.c
blob: 126f790c5c9b2e6586533293e8dc628186e70fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
     This file is part of GNUnet
     Copyright (C) 2018 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.
*/
/**
 * @file src/gns/gnunet-gns-benchmark.c
 * @brief issue many queries to GNS and compute performance statistics
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gnunet_util_lib.h>
#include <gnunet_gnsrecord_lib.h>
#include <gnunet_gns_service.h>


/**
 * How long do we wait at least between requests by default?
 */
#define DEF_REQUEST_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1)

/**
 * How long do we wait until we consider a request failed by default?
 */
#define DEF_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)


/**
 * We distinguish between different categories of
 * requests, for which we track statistics separately.
 * However, this process does not change how it acts
 * based on the category.
 */
enum RequestCategory
{
  RC_SHARED = 0,
  RC_PRIVATE = 1,
  /**
   * Must be last and match number of categories.
   */
  RC_MAX = 2
};


/**
 * Request we should make.  We keep this struct in memory per request,
 * thus optimizing it is crucial for the overall memory consumption of
 * the zone importer.
 */
struct Request
{

  /**
   * Active requests are kept in a DLL.
   */
  struct Request *next;

  /**
   * Active requests are kept in a DLL.
   */
  struct Request *prev;

  /**
   * Socket used to make the request, NULL if not active.
   */
  struct GNUNET_GNS_LookupWithTldRequest *lr;

  /**
   * Hostname we are resolving, allocated at the end of
   * this struct (optimizing memory consumption by reducing
   * total number of allocations).
   */
  const char *hostname;

  /**
   * While we are fetching the record, the value is set to the
   * starting time of the GNS operation.
   */
  struct GNUNET_TIME_Absolute op_start_time;

  /**
   * Observed latency, set once we got a reply.
   */
  struct GNUNET_TIME_Relative latency;

  /**
   * Category of the request.
   */
  enum RequestCategory cat;

};


/**
 * GNS handle.
 */
static struct GNUNET_GNS_Handle *gns;

/**
 * Number of lookups we performed overall per category.
 */
static unsigned int lookups[RC_MAX];

/**
 * Number of replies we got per category.
 */
static unsigned int replies[RC_MAX];

/**
 * Number of replies we got per category.
 */
static unsigned int failures[RC_MAX];

/**
 * Sum of the observed latencies of successful queries,
 * per category.
 */
static struct GNUNET_TIME_Relative latency_sum[RC_MAX];

/**
 * Active requests are kept in a DLL.
 */
static struct Request *act_head;

/**
 * Active requests are kept in a DLL.
 */
static struct Request *act_tail;

/**
 * Completed successful requests are kept in a DLL.
 */
static struct Request *succ_head;

/**
 * Completed successful requests are kept in a DLL.
 */
static struct Request *succ_tail;

/**
 * Yet to be started requests are kept in a DLL.
 */
static struct Request *todo_head;

/**
 * Yet to be started requests are kept in a DLL.
 */
static struct Request *todo_tail;

/**
 * Main task.
 */
static struct GNUNET_SCHEDULER_Task *t;

/**
 * Delay between requests.
 */
static struct GNUNET_TIME_Relative request_delay;

/**
 * Timeout for requests.
 */
static struct GNUNET_TIME_Relative timeout;

/**
 * Number of requests we have concurrently active.
 */
static unsigned int active_cnt;

/**
 * Look for GNS2DNS records specifically?
 */
static int g2d;

/**
 * Free @a req and data structures reachable from it.
 *
 * @param req request to free
 */
static void
free_request (struct Request *req)
{
  if (NULL != req->lr)
    GNUNET_GNS_lookup_with_tld_cancel (req->lr);
  GNUNET_free (req);
}


/**
 * Function called with the result of a GNS resolution.
 *
 * @param cls closure with the `struct Request`
 * @param gns_tld #GNUNET_YES if GNS lookup was attempted
 * @param rd_count number of records in @a rd
 * @param rd the records in reply
 */
static void
process_result (void *cls,
		int gns_tld,
		uint32_t rd_count,
		const struct GNUNET_GNSRECORD_Data *rd)
{
  struct Request *req = cls;

  (void) gns_tld;
  (void) rd_count;
  (void) rd;
  active_cnt--;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Got response for request `%s'\n",
              req->hostname);
  req->lr = NULL;
  req->latency = GNUNET_TIME_absolute_get_duration (req->op_start_time);
  GNUNET_CONTAINER_DLL_remove (act_head,
			       act_tail,
			       req);
  GNUNET_CONTAINER_DLL_insert (succ_head,
			       succ_tail,
			       req);
  replies[req->cat]++;
  latency_sum[req->cat]
    = GNUNET_TIME_relative_add (latency_sum[req->cat],
				req->latency);
}


/**
 * Process request from the queue.
 *
 * @param cls NULL
 */
static void
process_queue (void *cls)
{
  struct Request *req;
  struct GNUNET_TIME_Relative duration;

  (void) cls;
  t = NULL;
  /* check for expired requests */
  while (NULL != (req = act_head))
  {
    duration = GNUNET_TIME_absolute_get_duration (req->op_start_time);
    if (duration.rel_value_us < timeout.rel_value_us)
      break;
    GNUNET_CONTAINER_DLL_remove (act_head,
				 act_tail,
				 req);
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Failing request `%s' due to timeout\n",
                req->hostname);
    failures[req->cat]++;
    active_cnt--;
    free_request (req);
  }
  if (NULL == (req = todo_head))
  {
    struct GNUNET_TIME_Absolute at;

    if (NULL == (req = act_head))
    {
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
    at = GNUNET_TIME_absolute_add (req->op_start_time,
				   timeout);
    t = GNUNET_SCHEDULER_add_at (at,
				 &process_queue,
				 NULL);
    return;
  }
  GNUNET_CONTAINER_DLL_remove (todo_head,
			       todo_tail,
			       req);
  GNUNET_CONTAINER_DLL_insert_tail (act_head,
				    act_tail,
				    req);
  lookups[req->cat]++;
  active_cnt++;
  req->op_start_time = GNUNET_TIME_absolute_get ();
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Starting request `%s' (%u in parallel)\n",
              req->hostname,
              active_cnt);
  req->lr = GNUNET_GNS_lookup_with_tld (gns,
					req->hostname,
					g2d
                                        ? GNUNET_GNSRECORD_TYPE_GNS2DNS
                                        : GNUNET_GNSRECORD_TYPE_ANY,
					GNUNET_GNS_LO_DEFAULT,
					&process_result,
					req);
  t = GNUNET_SCHEDULER_add_delayed (request_delay,
                                    &process_queue,
                                    NULL);
}


/**
 * Compare two requests by latency for qsort().
 *
 * @param c1 pointer to `struct Request *`
 * @param c2 pointer to `struct Request *`
 * @return -1 if c1<c2, 1 if c1>c2, 0 if c1==c2.
 */
static int
compare_req (const void *c1,
	     const void *c2)
{
  const struct Request *r1 = *(void **) c1;
  const struct Request *r2 = *(void **) c2;

  if (r1->latency.rel_value_us < r2->latency.rel_value_us)
    return -1;
  if (r1->latency.rel_value_us > r2->latency.rel_value_us)
    return 1;
  return 0;
}


/**
 * Output statistics, then clean up and terminate the process.
 *
 * @param cls NULL
 */
static void
do_shutdown (void *cls)
{
  struct Request *req;
  struct Request **ra[RC_MAX];
  unsigned int rp[RC_MAX];

  (void) cls;
  for (enum RequestCategory rc = 0;rc < RC_MAX;rc++)
  {
    ra[rc] = GNUNET_new_array (replies[rc],
			       struct Request *);
    rp[rc] = 0;
  }
  for (req = succ_head;NULL != req; req = req->next)
  {
    GNUNET_assert (rp[req->cat] < replies[req->cat]);
    ra[req->cat][rp[req->cat]++] = req;
  }
  for (enum RequestCategory rc = 0;rc < RC_MAX;rc++)
  {
    unsigned int off;

    fprintf (stdout,
	     "Category %u\n",
	     rc);
    fprintf (stdout,
	     "\tlookups: %u replies: %u failures: %u\n",
	     lookups[rc],
	     replies[rc],
	     failures[rc]);
    if (0 == rp[rc])
      continue;
    qsort (ra[rc],
	   rp[rc],
	   sizeof (struct Request *),
	   &compare_req);
    latency_sum[rc] = GNUNET_TIME_relative_divide (latency_sum[rc],
                                                   replies[rc]);
    fprintf (stdout,
	     "\taverage: %s\n",
	     GNUNET_STRINGS_relative_time_to_string (latency_sum[rc],
						     GNUNET_YES));
    off = rp[rc] * 50 / 100;
    fprintf (stdout,
	     "\tmedian(50): %s\n",
	     GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
						     GNUNET_YES));
    off = rp[rc] * 75 / 100;
    fprintf (stdout,
	     "\tquantile(75): %s\n",
	     GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
						     GNUNET_YES));
    off = rp[rc] * 90 / 100;
    fprintf (stdout,
	     "\tquantile(90): %s\n",
	     GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
						     GNUNET_YES));
    off = rp[rc] * 99 / 100;
    fprintf (stdout,
	     "\tquantile(99): %s\n",
	     GNUNET_STRINGS_relative_time_to_string (ra[rc][off]->latency,
						     GNUNET_YES));
    GNUNET_free (ra[rc]);
  }
  if (NULL != t)
  {
    GNUNET_SCHEDULER_cancel (t);
    t = NULL;
  }
  while (NULL != (req = act_head))
  {
    GNUNET_CONTAINER_DLL_remove (act_head,
				 act_tail,
				 req);
    free_request (req);
  }
  while (NULL != (req = succ_head))
  {
    GNUNET_CONTAINER_DLL_remove (succ_head,
				 succ_tail,
				 req);
    free_request (req);
  }
  while (NULL != (req = todo_head))
  {
    GNUNET_CONTAINER_DLL_remove (todo_head,
				 todo_tail,
				 req);
    free_request (req);
  }
  if (NULL != gns)
  {
    GNUNET_GNS_disconnect (gns);
    gns = NULL;
  }
}


/**
 * Add @a hostname to the list of requests to be made.
 *
 * @param hostname name to resolve
 * @param cat category of the @a hostname
 */
static void
queue (const char *hostname,
       enum RequestCategory cat)
{
  struct Request *req;
  const char *dot;
  size_t hlen;

  dot = strchr (hostname,
                (unsigned char) '.');
  if (NULL == dot)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Refusing invalid hostname `%s' (lacks '.')\n",
                hostname);
    return;
  }
  hlen = strlen (hostname) + 1;
  req = GNUNET_malloc (sizeof (struct Request) + hlen);
  req->cat = cat;
  req->hostname = (char *) &req[1];
  GNUNET_memcpy (&req[1],
                 hostname,
                 hlen);
  GNUNET_CONTAINER_DLL_insert (todo_head,
			       todo_tail,
			       req);
}


/**
 * Begin processing hostnames from stdin.
 *
 * @param cls NULL
 */
static void
process_stdin (void *cls)
{
  static struct GNUNET_TIME_Absolute last;
  static uint64_t idot;
  unsigned int cat;
  char hn[256];
  char in[270];

  (void) cls;
  t = NULL;
  while (NULL !=
         fgets (in,
                sizeof (in),
                stdin))
  {
    if (strlen(in) > 0)
      hn[strlen(in)-1] = '\0'; /* eat newline */
    if ( (2 != sscanf (in,
		       "%u %255s",
		       &cat,
		       hn)) ||
	 (cat >= RC_MAX) )
    {
      fprintf (stderr,
	       "Malformed input line `%s', skipping\n",
	       in);
      continue;
    }
    if (0 == idot)
      last = GNUNET_TIME_absolute_get ();
    idot++;
    if (0 == idot % 100000)
    {
      struct GNUNET_TIME_Relative delta;

      delta = GNUNET_TIME_absolute_get_duration (last);
      last = GNUNET_TIME_absolute_get ();
      fprintf (stderr,
	       "Read 100000 domain names in %s\n",
	       GNUNET_STRINGS_relative_time_to_string (delta,
						       GNUNET_YES));
    }
    queue (hn,
	   (enum RequestCategory) cat);
  }
  fprintf (stderr,
           "Done reading %llu domain names\n",
           (unsigned long long) idot);
  t = GNUNET_SCHEDULER_add_now (&process_queue,
				NULL);
}


/**
 * Process requests from the queue, then if the queue is
 * not empty, try again.
 *
 * @param cls NULL
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  (void) cls;
  (void) args;
  (void) cfgfile;
  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
                                 NULL);
  gns = GNUNET_GNS_connect (cfg);
  if (NULL == gns)
  {
    GNUNET_break (0);
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  t = GNUNET_SCHEDULER_add_now (&process_stdin,
				NULL);
}


/**
 * Call with list of names with numeric category to query.
 *
 * @param argc unused
 * @param argv unused
 * @return 0 on success
 */
int
main (int argc,
      char *const*argv)
{
  int ret = 0;
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_relative_time ('d',
                                        "delay",
                                        "RELATIVETIME",
                                        gettext_noop ("how long to wait between queries"),
                                        &request_delay),
    GNUNET_GETOPT_option_relative_time ('t',
                                        "timeout",
                                        "RELATIVETIME",
                                        gettext_noop ("how long to wait for an answer"),
                                        &timeout),
    GNUNET_GETOPT_option_flag ('2',
                               "g2d",
                               gettext_noop ("look for GNS2DNS records instead of ANY"),
                               &g2d),
    GNUNET_GETOPT_OPTION_END
  };

  if (GNUNET_OK !=
      GNUNET_STRINGS_get_utf8_args (argc, argv,
				    &argc, &argv))
    return 2;
  timeout = DEF_TIMEOUT;
  request_delay = DEF_REQUEST_DELAY;
  if (GNUNET_OK !=
      GNUNET_PROGRAM_run (argc,
			  argv,
			  "gnunet-gns-benchmark",
			  "resolve GNS names and measure performance",
			  options,
			  &run,
			  NULL))
    ret = 1;
  GNUNET_free ((void*) argv);
  return ret;
}

/* end of gnunet-gns-benchmark.c */