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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file src/transport/gnunet-transport-profiler.c
23 * @brief Tool to help benchmark the transport subsystem.
24 * @author Christian Grothoff
25 * @author Nathan Evans
27 * This utility can be used to benchmark a transport mechanism for
31 #include "gnunet_util_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_ats_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_core_service.h"
40 struct Iteration *next;
41 struct Iteration *prev;
42 struct GNUNET_TIME_Absolute start;
43 struct GNUNET_TIME_Absolute end;
45 struct GNUNET_TIME_Relative dur;
47 /* Transmission rate for this iteration in KB/s */
50 unsigned int msgs_sent;
55 * Timeout for a connections
57 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
60 * Benchmarking block size in bye
62 #define DEFAULT_MESSAGE_SIZE 1024
65 * Benchmarking message count
67 #define DEFAULT_MESSAGE_COUNT 1024
70 * Benchmarking iteration count
72 #define DEFAULT_ITERATION_COUNT 1
77 static int benchmark_send;
82 static int benchmark_receive;
87 static unsigned int benchmark_count;
92 static unsigned int benchmark_iterations;
97 static unsigned int benchmark_size;
102 static unsigned int benchmark_running;
105 * Which peer should we connect to?
110 * Handle to transport service.
112 static struct GNUNET_TRANSPORT_CoreHandle *handle;
115 * Handle to ATS service.
117 static struct GNUNET_ATS_ConnectivityHandle *ats;
120 * Configuration handle
122 static struct GNUNET_CONFIGURATION_Handle *cfg;
127 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
129 static struct Iteration *ihead;
131 static struct Iteration *itail;
134 * Global return value (0 success).
139 * Handle for transmissions.
141 static struct GNUNET_MQ_Handle *mq;
143 static struct GNUNET_TRANSPORT_Blacklist *bl_handle;
146 * Identity of the peer we transmit to / connect to.
147 * (equivalent to 'cpid' string).
149 static struct GNUNET_PeerIdentity pid;
152 * Selected level of verbosity.
154 static unsigned int verbosity;
158 * Task run in monitor mode when the user presses CTRL-C to abort.
159 * Stops monitoring activity.
164 shutdown_task (void *cls)
166 struct Iteration *icur;
167 struct Iteration *inext;
169 unsigned int iterations;
171 unsigned long long avg_duration;
174 float stddev_duration;
178 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
181 if (NULL != bl_handle)
183 GNUNET_TRANSPORT_blacklist_cancel (bl_handle);
188 GNUNET_ATS_connectivity_done (ats);
193 GNUNET_TRANSPORT_core_disconnect (handle);
198 FPRINTF (stdout, "\n");
201 * All time values in ms
203 * #messages;#messagesize;#avg_dur;#avg_rate;#duration_i0;#duration_i0;... */
207 /* First iteration to calculcate avg and stddev */
213 while (NULL != (icur = inext))
216 icur->rate = ((benchmark_count * benchmark_size) / 1024) /
217 ((float) icur->dur.rel_value_us / (1000 * 1000));
219 FPRINTF (stdout, _("%llu B in %llu ms == %.2f KB/s!\n"),
220 ((long long unsigned int) benchmark_count * benchmark_size),
221 ((long long unsigned int) icur->dur.rel_value_us / 1000),
224 avg_duration += icur->dur.rel_value_us / (1000);
225 avg_rate += icur->rate;
229 iterations = 1; /* avoid division by zero */
230 /* Calculate average rate */
231 avg_rate /= iterations;
232 /* Calculate average duration */
233 avg_duration /= iterations;
238 while (NULL != (icur = inext))
241 stddev_rate += ((icur->rate-avg_rate) *
242 (icur->rate-avg_rate));
243 stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration) *
244 ((icur->dur.rel_value_us / 1000) - avg_duration));
247 /* Calculate standard deviation rate */
248 stddev_rate = stddev_rate / iterations;
249 stddev_rate = sqrtf(stddev_rate);
251 /* Calculate standard deviation duration */
252 stddev_duration = stddev_duration / iterations;
253 stddev_duration = sqrtf(stddev_duration);
257 "%u;%u;%llu;%llu;%.2f;%.2f",
261 (unsigned long long) stddev_duration,
266 while (NULL != (icur = inext))
269 GNUNET_CONTAINER_DLL_remove (ihead,
275 (long long unsigned int) (icur->dur.rel_value_us / 1000),
282 if (benchmark_receive)
284 duration = GNUNET_TIME_absolute_get_duration (start_time);
286 "Received %llu bytes/s (%llu bytes in %s)\n",
287 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
289 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
292 FPRINTF (stdout, "\n");
301 * Function called to notify a client about the socket
302 * begin ready to queue more data. @a buf will be
303 * NULL and @a size zero if the socket was closed for
304 * writing in the meantime.
307 * @param size number of bytes available in @a buf
308 * @param buf where the callee should write the message
309 * @return number of bytes written to @a buf
314 struct GNUNET_MQ_Envelope *env;
315 struct GNUNET_MessageHeader *m;
319 env = GNUNET_MQ_msg_extra (m,
321 GNUNET_MESSAGE_TYPE_DUMMY);
324 benchmark_size - sizeof(struct GNUNET_MessageHeader));
326 if (itail->msgs_sent < benchmark_count)
328 GNUNET_MQ_notify_sent (env,
338 if ( (verbosity > 0) &&
339 (0 == itail->msgs_sent % 10) )
340 FPRINTF (stdout, ".");
347 struct Iteration *icur;
350 if (! benchmark_send)
352 benchmark_running = GNUNET_YES;
353 icur = GNUNET_new (struct Iteration);
354 GNUNET_CONTAINER_DLL_insert_tail (ihead,
357 icur->start = GNUNET_TIME_absolute_get();
360 "\nStarting benchmark, starting to send %u messages in %u byte blocks\n",
370 static int it_count = 0;
373 itail->dur = GNUNET_TIME_absolute_get_duration (itail->start);
374 if (it_count == benchmark_iterations)
376 benchmark_running = GNUNET_NO;
377 GNUNET_SCHEDULER_shutdown ();
385 * Function called to notify transport users that another
386 * peer connected to us.
389 * @param peer the peer that connected
390 * @param m message queue for transmissions
394 notify_connect (void *cls,
395 const struct GNUNET_PeerIdentity *peer,
396 struct GNUNET_MQ_Handle *m)
398 if (0 != memcmp (&pid,
400 sizeof(struct GNUNET_PeerIdentity)))
403 "Connected to different peer `%s'\n",
410 "Successfully connected to `%s'\n",
419 * Function called to notify transport users that another
420 * peer disconnected from us.
423 * @param peer the peer that disconnected
424 * @param internal_cls NULL
427 notify_disconnect (void *cls,
428 const struct GNUNET_PeerIdentity *peer,
431 if (0 != memcmp (&pid,
433 sizeof(struct GNUNET_PeerIdentity)))
436 if (GNUNET_YES == benchmark_running)
439 "Disconnected from peer `%s' while benchmarking\n",
447 * Function called by the transport for each received message.
450 * @param message the message
454 check_dummy (void *cls,
455 const struct GNUNET_MessageHeader *message)
457 return GNUNET_OK; /* all messages are fine */
462 * Function called by the transport for each received message.
465 * @param message the message
468 handle_dummy (void *cls,
469 const struct GNUNET_MessageHeader *message)
471 if (! benchmark_receive)
475 "Received %u bytes\n",
476 (unsigned int) ntohs (message->size));
481 blacklist_cb (void *cls,
482 const struct GNUNET_PeerIdentity *peer)
484 if (0 != memcmp (&pid,
486 sizeof(struct GNUNET_PeerIdentity)))
490 "Denying connection to `%s'\n",
492 return GNUNET_SYSERR;
499 * Main function that will be run by the scheduler.
502 * @param args remaining command-line arguments
503 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
504 * @param mycfg configuration
510 const struct GNUNET_CONFIGURATION_Handle *mycfg)
512 struct GNUNET_MQ_MessageHandler handlers[] = {
513 GNUNET_MQ_hd_var_size (dummy,
514 GNUNET_MESSAGE_TYPE_DUMMY,
515 struct GNUNET_MessageHeader,
517 GNUNET_MQ_handler_end ()
520 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
523 if (GNUNET_MAX_MESSAGE_SIZE <= benchmark_size)
526 "Message size too big!\n");
533 "No peer identity given\n");
537 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
542 "Failed to parse peer identity `%s'\n",
546 if (1 == benchmark_send)
550 "Trying to send %u messages with size %u to peer `%s'\n",
551 benchmark_count, benchmark_size,
554 else if (1 == benchmark_receive)
557 "Trying to receive messages from peer `%s'\n",
563 "No operation given\n");
567 ats = GNUNET_ATS_connectivity_init (cfg);
571 "Failed to connect to ATS service\n");
576 handle = GNUNET_TRANSPORT_core_connect (cfg,
586 "Failed to connect to transport service\n");
587 GNUNET_ATS_connectivity_done (ats);
593 bl_handle = GNUNET_TRANSPORT_blacklist (cfg,
596 ats_sh = GNUNET_ATS_connectivity_suggest (ats,
599 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
605 main (int argc, char * const *argv)
608 benchmark_count = DEFAULT_MESSAGE_COUNT;
609 benchmark_size = DEFAULT_MESSAGE_SIZE;
610 benchmark_iterations = DEFAULT_ITERATION_COUNT;
611 benchmark_running = GNUNET_NO;
613 struct GNUNET_GETOPT_CommandLineOption options[] = {
615 GNUNET_GETOPT_option_flag ('s',
617 gettext_noop ("send data to peer"),
619 GNUNET_GETOPT_option_flag ('r',
621 gettext_noop ("receive data from peer"),
623 GNUNET_GETOPT_option_uint ('i',
626 gettext_noop ("iterations"),
627 &benchmark_iterations),
628 GNUNET_GETOPT_option_uint ('n',
631 gettext_noop ("number of messages to send"),
633 GNUNET_GETOPT_option_uint ('m',
636 gettext_noop ("message size to use"),
638 GNUNET_GETOPT_option_string ('p',
641 gettext_noop ("peer identity"),
643 GNUNET_GETOPT_option_verbose (&verbosity),
644 GNUNET_GETOPT_OPTION_END
647 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
650 res = GNUNET_PROGRAM_run (argc, argv,
652 gettext_noop ("Direct access to transport service."),
655 GNUNET_free((void *) argv);
656 if (GNUNET_OK == res)
661 /* end of gnunet-transport-profiler.c */