2 This file is part of GNUnet.
3 Copyright (C) 2011-2016 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.
17 * @file src/transport/gnunet-transport-profiler.c
18 * @brief Tool to help benchmark the transport subsystem.
19 * @author Christian Grothoff
20 * @author Nathan Evans
22 * This utility can be used to benchmark a transport mechanism for
26 #include "gnunet_util_lib.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_transport_core_service.h"
35 struct Iteration *next;
36 struct Iteration *prev;
37 struct GNUNET_TIME_Absolute start;
38 struct GNUNET_TIME_Absolute end;
40 struct GNUNET_TIME_Relative dur;
42 /* Transmission rate for this iteration in KB/s */
45 unsigned int msgs_sent;
50 * Timeout for a connections
52 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
55 * Benchmarking block size in bye
57 #define DEFAULT_MESSAGE_SIZE 1024
60 * Benchmarking message count
62 #define DEFAULT_MESSAGE_COUNT 1024
65 * Benchmarking iteration count
67 #define DEFAULT_ITERATION_COUNT 1
72 static int benchmark_send;
77 static int benchmark_receive;
82 static unsigned int benchmark_count;
87 static unsigned int benchmark_iterations;
92 static unsigned int benchmark_size;
97 static unsigned int benchmark_running;
100 * Which peer should we connect to?
105 * Handle to transport service.
107 static struct GNUNET_TRANSPORT_CoreHandle *handle;
110 * Handle to ATS service.
112 static struct GNUNET_ATS_ConnectivityHandle *ats;
115 * Configuration handle
117 static struct GNUNET_CONFIGURATION_Handle *cfg;
122 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
124 static struct Iteration *ihead;
126 static struct Iteration *itail;
129 * Global return value (0 success).
134 * Handle for transmissions.
136 static struct GNUNET_MQ_Handle *mq;
138 static struct GNUNET_TRANSPORT_Blacklist *bl_handle;
141 * Identity of the peer we transmit to / connect to.
142 * (equivalent to 'cpid' string).
144 static struct GNUNET_PeerIdentity pid;
147 * Selected level of verbosity.
149 static unsigned int verbosity;
153 * Task run in monitor mode when the user presses CTRL-C to abort.
154 * Stops monitoring activity.
159 shutdown_task (void *cls)
161 struct Iteration *icur;
162 struct Iteration *inext;
164 unsigned int iterations;
166 unsigned long long avg_duration;
169 float stddev_duration;
173 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
176 if (NULL != bl_handle)
178 GNUNET_TRANSPORT_blacklist_cancel (bl_handle);
183 GNUNET_ATS_connectivity_done (ats);
188 GNUNET_TRANSPORT_core_disconnect (handle);
193 FPRINTF (stdout, "\n");
196 * All time values in ms
198 * #messages;#messagesize;#avg_dur;#avg_rate;#duration_i0;#duration_i0;... */
202 /* First iteration to calculcate avg and stddev */
208 while (NULL != (icur = inext))
211 icur->rate = ((benchmark_count * benchmark_size) / 1024) /
212 ((float) icur->dur.rel_value_us / (1000 * 1000));
214 FPRINTF (stdout, _("%llu B in %llu ms == %.2f KB/s!\n"),
215 ((long long unsigned int) benchmark_count * benchmark_size),
216 ((long long unsigned int) icur->dur.rel_value_us / 1000),
219 avg_duration += icur->dur.rel_value_us / (1000);
220 avg_rate += icur->rate;
224 iterations = 1; /* avoid division by zero */
225 /* Calculate average rate */
226 avg_rate /= iterations;
227 /* Calculate average duration */
228 avg_duration /= iterations;
233 while (NULL != (icur = inext))
236 stddev_rate += ((icur->rate-avg_rate) *
237 (icur->rate-avg_rate));
238 stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration) *
239 ((icur->dur.rel_value_us / 1000) - avg_duration));
242 /* Calculate standard deviation rate */
243 stddev_rate = stddev_rate / iterations;
244 stddev_rate = sqrtf(stddev_rate);
246 /* Calculate standard deviation duration */
247 stddev_duration = stddev_duration / iterations;
248 stddev_duration = sqrtf(stddev_duration);
252 "%u;%u;%llu;%llu;%.2f;%.2f",
256 (unsigned long long) stddev_duration,
261 while (NULL != (icur = inext))
264 GNUNET_CONTAINER_DLL_remove (ihead,
270 (long long unsigned int) (icur->dur.rel_value_us / 1000),
277 if (benchmark_receive)
279 duration = GNUNET_TIME_absolute_get_duration (start_time);
281 "Received %llu bytes/s (%llu bytes in %s)\n",
282 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
284 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
287 FPRINTF (stdout, "\n");
296 * Function called to notify a client about the socket
297 * begin ready to queue more data. @a buf will be
298 * NULL and @a size zero if the socket was closed for
299 * writing in the meantime.
302 * @param size number of bytes available in @a buf
303 * @param buf where the callee should write the message
304 * @return number of bytes written to @a buf
309 struct GNUNET_MQ_Envelope *env;
310 struct GNUNET_MessageHeader *m;
314 env = GNUNET_MQ_msg_extra (m,
316 GNUNET_MESSAGE_TYPE_DUMMY);
319 benchmark_size - sizeof(struct GNUNET_MessageHeader));
321 if (itail->msgs_sent < benchmark_count)
323 GNUNET_MQ_notify_sent (env,
333 if ( (verbosity > 0) &&
334 (0 == itail->msgs_sent % 10) )
335 FPRINTF (stdout, ".");
342 struct Iteration *icur;
345 if (! benchmark_send)
347 benchmark_running = GNUNET_YES;
348 icur = GNUNET_new (struct Iteration);
349 GNUNET_CONTAINER_DLL_insert_tail (ihead,
352 icur->start = GNUNET_TIME_absolute_get();
355 "\nStarting benchmark, starting to send %u messages in %u byte blocks\n",
365 static int it_count = 0;
368 itail->dur = GNUNET_TIME_absolute_get_duration (itail->start);
369 if (it_count == benchmark_iterations)
371 benchmark_running = GNUNET_NO;
372 GNUNET_SCHEDULER_shutdown ();
380 * Function called to notify transport users that another
381 * peer connected to us.
384 * @param peer the peer that connected
385 * @param m message queue for transmissions
389 notify_connect (void *cls,
390 const struct GNUNET_PeerIdentity *peer,
391 struct GNUNET_MQ_Handle *m)
393 if (0 != memcmp (&pid,
395 sizeof(struct GNUNET_PeerIdentity)))
398 "Connected to different peer `%s'\n",
405 "Successfully connected to `%s'\n",
414 * Function called to notify transport users that another
415 * peer disconnected from us.
418 * @param peer the peer that disconnected
419 * @param internal_cls NULL
422 notify_disconnect (void *cls,
423 const struct GNUNET_PeerIdentity *peer,
426 if (0 != memcmp (&pid,
428 sizeof(struct GNUNET_PeerIdentity)))
431 if (GNUNET_YES == benchmark_running)
434 "Disconnected from peer `%s' while benchmarking\n",
442 * Function called by the transport for each received message.
445 * @param message the message
449 check_dummy (void *cls,
450 const struct GNUNET_MessageHeader *message)
452 return GNUNET_OK; /* all messages are fine */
457 * Function called by the transport for each received message.
460 * @param message the message
463 handle_dummy (void *cls,
464 const struct GNUNET_MessageHeader *message)
466 if (! benchmark_receive)
470 "Received %u bytes\n",
471 (unsigned int) ntohs (message->size));
476 blacklist_cb (void *cls,
477 const struct GNUNET_PeerIdentity *peer)
479 if (0 != memcmp (&pid,
481 sizeof(struct GNUNET_PeerIdentity)))
485 "Denying connection to `%s'\n",
487 return GNUNET_SYSERR;
494 * Main function that will be run by the scheduler.
497 * @param args remaining command-line arguments
498 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
499 * @param mycfg configuration
505 const struct GNUNET_CONFIGURATION_Handle *mycfg)
507 struct GNUNET_MQ_MessageHandler handlers[] = {
508 GNUNET_MQ_hd_var_size (dummy,
509 GNUNET_MESSAGE_TYPE_DUMMY,
510 struct GNUNET_MessageHeader,
512 GNUNET_MQ_handler_end ()
515 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
518 if (GNUNET_MAX_MESSAGE_SIZE <= benchmark_size)
521 "Message size too big!\n");
528 "No peer identity given\n");
532 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
537 "Failed to parse peer identity `%s'\n",
541 if (1 == benchmark_send)
545 "Trying to send %u messages with size %u to peer `%s'\n",
546 benchmark_count, benchmark_size,
549 else if (1 == benchmark_receive)
552 "Trying to receive messages from peer `%s'\n",
558 "No operation given\n");
562 ats = GNUNET_ATS_connectivity_init (cfg);
566 "Failed to connect to ATS service\n");
571 handle = GNUNET_TRANSPORT_core_connect (cfg,
581 "Failed to connect to transport service\n");
582 GNUNET_ATS_connectivity_done (ats);
588 bl_handle = GNUNET_TRANSPORT_blacklist (cfg,
591 ats_sh = GNUNET_ATS_connectivity_suggest (ats,
594 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
600 main (int argc, char * const *argv)
603 benchmark_count = DEFAULT_MESSAGE_COUNT;
604 benchmark_size = DEFAULT_MESSAGE_SIZE;
605 benchmark_iterations = DEFAULT_ITERATION_COUNT;
606 benchmark_running = GNUNET_NO;
608 struct GNUNET_GETOPT_CommandLineOption options[] = {
610 GNUNET_GETOPT_option_flag ('s',
612 gettext_noop ("send data to peer"),
614 GNUNET_GETOPT_option_flag ('r',
616 gettext_noop ("receive data from peer"),
618 GNUNET_GETOPT_option_uint ('i',
621 gettext_noop ("iterations"),
622 &benchmark_iterations),
623 GNUNET_GETOPT_option_uint ('n',
626 gettext_noop ("number of messages to send"),
628 GNUNET_GETOPT_option_uint ('m',
631 gettext_noop ("message size to use"),
633 GNUNET_GETOPT_option_string ('p',
636 gettext_noop ("peer identity"),
638 GNUNET_GETOPT_option_verbose (&verbosity),
639 GNUNET_GETOPT_OPTION_END
642 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
645 res = GNUNET_PROGRAM_run (argc, argv,
647 gettext_noop ("Direct access to transport service."),
650 GNUNET_free((void *) argv);
651 if (GNUNET_OK == res)
656 /* end of gnunet-transport-profiler.c */