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.
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/>.
20 * @file src/transport/gnunet-transport-profiler.c
21 * @brief Tool to help benchmark the transport subsystem.
22 * @author Christian Grothoff
23 * @author Nathan Evans
25 * This utility can be used to benchmark a transport mechanism for
29 #include "gnunet_util_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_transport_core_service.h"
38 struct Iteration *next;
39 struct Iteration *prev;
40 struct GNUNET_TIME_Absolute start;
41 struct GNUNET_TIME_Absolute end;
43 struct GNUNET_TIME_Relative dur;
45 /* Transmission rate for this iteration in KB/s */
48 unsigned int msgs_sent;
53 * Timeout for a connections
55 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
58 * Benchmarking block size in bye
60 #define DEFAULT_MESSAGE_SIZE 1024
63 * Benchmarking message count
65 #define DEFAULT_MESSAGE_COUNT 1024
68 * Benchmarking iteration count
70 #define DEFAULT_ITERATION_COUNT 1
75 static int benchmark_send;
80 static int benchmark_receive;
85 static unsigned int benchmark_count;
90 static unsigned int benchmark_iterations;
95 static unsigned int benchmark_size;
100 static unsigned int benchmark_running;
103 * Which peer should we connect to?
108 * Handle to transport service.
110 static struct GNUNET_TRANSPORT_CoreHandle *handle;
113 * Handle to ATS service.
115 static struct GNUNET_ATS_ConnectivityHandle *ats;
118 * Configuration handle
120 static struct GNUNET_CONFIGURATION_Handle *cfg;
125 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
127 static struct Iteration *ihead;
129 static struct Iteration *itail;
132 * Global return value (0 success).
137 * Handle for transmissions.
139 static struct GNUNET_MQ_Handle *mq;
141 static struct GNUNET_TRANSPORT_Blacklist *bl_handle;
144 * Identity of the peer we transmit to / connect to.
145 * (equivalent to 'cpid' string).
147 static struct GNUNET_PeerIdentity pid;
150 * Selected level of verbosity.
152 static unsigned int verbosity;
156 * Task run in monitor mode when the user presses CTRL-C to abort.
157 * Stops monitoring activity.
162 shutdown_task (void *cls)
164 struct Iteration *icur;
165 struct Iteration *inext;
167 unsigned int iterations;
169 unsigned long long avg_duration;
172 float stddev_duration;
176 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
179 if (NULL != bl_handle)
181 GNUNET_TRANSPORT_blacklist_cancel (bl_handle);
186 GNUNET_ATS_connectivity_done (ats);
191 GNUNET_TRANSPORT_core_disconnect (handle);
196 FPRINTF (stdout, "\n");
199 * All time values in ms
201 * #messages;#messagesize;#avg_dur;#avg_rate;#duration_i0;#duration_i0;... */
205 /* First iteration to calculcate avg and stddev */
211 while (NULL != (icur = inext))
214 icur->rate = ((benchmark_count * benchmark_size) / 1024) /
215 ((float) icur->dur.rel_value_us / (1000 * 1000));
217 FPRINTF (stdout, _("%llu B in %llu ms == %.2f KB/s!\n"),
218 ((long long unsigned int) benchmark_count * benchmark_size),
219 ((long long unsigned int) icur->dur.rel_value_us / 1000),
222 avg_duration += icur->dur.rel_value_us / (1000);
223 avg_rate += icur->rate;
227 iterations = 1; /* avoid division by zero */
228 /* Calculate average rate */
229 avg_rate /= iterations;
230 /* Calculate average duration */
231 avg_duration /= iterations;
236 while (NULL != (icur = inext))
239 stddev_rate += ((icur->rate-avg_rate) *
240 (icur->rate-avg_rate));
241 stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration) *
242 ((icur->dur.rel_value_us / 1000) - avg_duration));
245 /* Calculate standard deviation rate */
246 stddev_rate = stddev_rate / iterations;
247 stddev_rate = sqrtf(stddev_rate);
249 /* Calculate standard deviation duration */
250 stddev_duration = stddev_duration / iterations;
251 stddev_duration = sqrtf(stddev_duration);
255 "%u;%u;%llu;%llu;%.2f;%.2f",
259 (unsigned long long) stddev_duration,
264 while (NULL != (icur = inext))
267 GNUNET_CONTAINER_DLL_remove (ihead,
273 (long long unsigned int) (icur->dur.rel_value_us / 1000),
280 if (benchmark_receive)
282 duration = GNUNET_TIME_absolute_get_duration (start_time);
284 "Received %llu bytes/s (%llu bytes in %s)\n",
285 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
287 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
290 FPRINTF (stdout, "\n");
299 * Function called to notify a client about the socket
300 * begin ready to queue more data. @a buf will be
301 * NULL and @a size zero if the socket was closed for
302 * writing in the meantime.
305 * @param size number of bytes available in @a buf
306 * @param buf where the callee should write the message
307 * @return number of bytes written to @a buf
312 struct GNUNET_MQ_Envelope *env;
313 struct GNUNET_MessageHeader *m;
317 env = GNUNET_MQ_msg_extra (m,
319 GNUNET_MESSAGE_TYPE_DUMMY);
322 benchmark_size - sizeof(struct GNUNET_MessageHeader));
324 if (itail->msgs_sent < benchmark_count)
326 GNUNET_MQ_notify_sent (env,
336 if ( (verbosity > 0) &&
337 (0 == itail->msgs_sent % 10) )
338 FPRINTF (stdout, ".");
345 struct Iteration *icur;
348 if (! benchmark_send)
350 benchmark_running = GNUNET_YES;
351 icur = GNUNET_new (struct Iteration);
352 GNUNET_CONTAINER_DLL_insert_tail (ihead,
355 icur->start = GNUNET_TIME_absolute_get();
358 "\nStarting benchmark, starting to send %u messages in %u byte blocks\n",
368 static int it_count = 0;
371 itail->dur = GNUNET_TIME_absolute_get_duration (itail->start);
372 if (it_count == benchmark_iterations)
374 benchmark_running = GNUNET_NO;
375 GNUNET_SCHEDULER_shutdown ();
383 * Function called to notify transport users that another
384 * peer connected to us.
387 * @param peer the peer that connected
388 * @param m message queue for transmissions
392 notify_connect (void *cls,
393 const struct GNUNET_PeerIdentity *peer,
394 struct GNUNET_MQ_Handle *m)
396 if (0 != memcmp (&pid,
398 sizeof(struct GNUNET_PeerIdentity)))
401 "Connected to different peer `%s'\n",
408 "Successfully connected to `%s'\n",
417 * Function called to notify transport users that another
418 * peer disconnected from us.
421 * @param peer the peer that disconnected
422 * @param internal_cls NULL
425 notify_disconnect (void *cls,
426 const struct GNUNET_PeerIdentity *peer,
429 if (0 != memcmp (&pid,
431 sizeof(struct GNUNET_PeerIdentity)))
434 if (GNUNET_YES == benchmark_running)
437 "Disconnected from peer `%s' while benchmarking\n",
445 * Function called by the transport for each received message.
448 * @param message the message
452 check_dummy (void *cls,
453 const struct GNUNET_MessageHeader *message)
455 return GNUNET_OK; /* all messages are fine */
460 * Function called by the transport for each received message.
463 * @param message the message
466 handle_dummy (void *cls,
467 const struct GNUNET_MessageHeader *message)
469 if (! benchmark_receive)
473 "Received %u bytes\n",
474 (unsigned int) ntohs (message->size));
479 blacklist_cb (void *cls,
480 const struct GNUNET_PeerIdentity *peer)
482 if (0 != memcmp (&pid,
484 sizeof(struct GNUNET_PeerIdentity)))
488 "Denying connection to `%s'\n",
490 return GNUNET_SYSERR;
497 * Main function that will be run by the scheduler.
500 * @param args remaining command-line arguments
501 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
502 * @param mycfg configuration
508 const struct GNUNET_CONFIGURATION_Handle *mycfg)
510 struct GNUNET_MQ_MessageHandler handlers[] = {
511 GNUNET_MQ_hd_var_size (dummy,
512 GNUNET_MESSAGE_TYPE_DUMMY,
513 struct GNUNET_MessageHeader,
515 GNUNET_MQ_handler_end ()
518 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
521 if (GNUNET_MAX_MESSAGE_SIZE <= benchmark_size)
524 "Message size too big!\n");
531 "No peer identity given\n");
535 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
540 "Failed to parse peer identity `%s'\n",
544 if (1 == benchmark_send)
548 "Trying to send %u messages with size %u to peer `%s'\n",
549 benchmark_count, benchmark_size,
552 else if (1 == benchmark_receive)
555 "Trying to receive messages from peer `%s'\n",
561 "No operation given\n");
565 ats = GNUNET_ATS_connectivity_init (cfg);
569 "Failed to connect to ATS service\n");
574 handle = GNUNET_TRANSPORT_core_connect (cfg,
584 "Failed to connect to transport service\n");
585 GNUNET_ATS_connectivity_done (ats);
591 bl_handle = GNUNET_TRANSPORT_blacklist (cfg,
594 ats_sh = GNUNET_ATS_connectivity_suggest (ats,
597 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
603 main (int argc, char * const *argv)
606 benchmark_count = DEFAULT_MESSAGE_COUNT;
607 benchmark_size = DEFAULT_MESSAGE_SIZE;
608 benchmark_iterations = DEFAULT_ITERATION_COUNT;
609 benchmark_running = GNUNET_NO;
611 struct GNUNET_GETOPT_CommandLineOption options[] = {
613 GNUNET_GETOPT_option_flag ('s',
615 gettext_noop ("send data to peer"),
617 GNUNET_GETOPT_option_flag ('r',
619 gettext_noop ("receive data from peer"),
621 GNUNET_GETOPT_option_uint ('i',
624 gettext_noop ("iterations"),
625 &benchmark_iterations),
626 GNUNET_GETOPT_option_uint ('n',
629 gettext_noop ("number of messages to send"),
631 GNUNET_GETOPT_option_uint ('m',
634 gettext_noop ("message size to use"),
636 GNUNET_GETOPT_option_string ('p',
639 gettext_noop ("peer identity"),
641 GNUNET_GETOPT_option_verbose (&verbosity),
642 GNUNET_GETOPT_OPTION_END
645 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
648 res = GNUNET_PROGRAM_run (argc, argv,
650 gettext_noop ("Direct access to transport service."),
653 GNUNET_free((void *) argv);
654 if (GNUNET_OK == res)
659 /* end of gnunet-transport-profiler.c */