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"
39 struct Iteration *next;
40 struct Iteration *prev;
41 struct GNUNET_TIME_Absolute start;
42 struct GNUNET_TIME_Absolute end;
44 struct GNUNET_TIME_Relative dur;
46 /* Transmission rate for this iteration in KB/s */
49 unsigned int msgs_sent;
54 * Timeout for a connections
56 #define CONNECT_TIMEOUT \
57 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));
220 _ ("%llu B in %llu ms == %.2f KB/s!\n"),
221 ((long long unsigned int) benchmark_count * benchmark_size),
222 ((long long unsigned int) icur->dur.rel_value_us / 1000),
225 avg_duration += icur->dur.rel_value_us / (1000);
226 avg_rate += icur->rate;
230 iterations = 1; /* avoid division by zero */
231 /* Calculate average rate */
232 avg_rate /= iterations;
233 /* Calculate average duration */
234 avg_duration /= iterations;
239 while (NULL != (icur = inext))
242 stddev_rate += ((icur->rate - avg_rate) * (icur->rate - avg_rate));
243 stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration)
244 * ((icur->dur.rel_value_us / 1000) - avg_duration));
246 /* Calculate standard deviation rate */
247 stddev_rate = stddev_rate / iterations;
248 stddev_rate = sqrtf (stddev_rate);
250 /* Calculate standard deviation duration */
251 stddev_duration = stddev_duration / iterations;
252 stddev_duration = sqrtf (stddev_duration);
256 "%u;%u;%llu;%llu;%.2f;%.2f",
260 (unsigned long long) stddev_duration,
265 while (NULL != (icur = inext))
268 GNUNET_CONTAINER_DLL_remove (ihead, itail, icur);
272 (long long unsigned int) (icur->dur.rel_value_us / 1000),
279 if (benchmark_receive)
281 duration = GNUNET_TIME_absolute_get_duration (start_time);
283 "Received %llu bytes/s (%llu bytes in %s)\n",
284 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
286 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
289 fprintf (stdout, "\n");
298 * Function called to notify a client about the socket
299 * begin ready to queue more data. @a buf will be
300 * NULL and @a size zero if the socket was closed for
301 * writing in the meantime.
304 * @param size number of bytes available in @a buf
305 * @param buf where the callee should write the message
306 * @return number of bytes written to @a buf
311 struct GNUNET_MQ_Envelope *env;
312 struct GNUNET_MessageHeader *m;
316 env = GNUNET_MQ_msg_extra (m, benchmark_size, GNUNET_MESSAGE_TYPE_DUMMY);
317 memset (&m[1], 52, benchmark_size - sizeof(struct GNUNET_MessageHeader));
319 if (itail->msgs_sent < benchmark_count)
321 GNUNET_MQ_notify_sent (env, &send_msg, NULL);
327 GNUNET_MQ_send (mq, env);
328 if ((verbosity > 0) && (0 == itail->msgs_sent % 10))
329 fprintf (stdout, ".");
336 struct Iteration *icur;
339 if (! benchmark_send)
341 benchmark_running = GNUNET_YES;
342 icur = GNUNET_new (struct Iteration);
343 GNUNET_CONTAINER_DLL_insert_tail (ihead, itail, icur);
344 icur->start = GNUNET_TIME_absolute_get ();
348 "\nStarting benchmark, starting to send %u messages in %u byte blocks\n",
358 static int it_count = 0;
361 itail->dur = GNUNET_TIME_absolute_get_duration (itail->start);
362 if (it_count == benchmark_iterations)
364 benchmark_running = GNUNET_NO;
365 GNUNET_SCHEDULER_shutdown ();
373 * Function called to notify transport users that another
374 * peer connected to us.
377 * @param peer the peer that connected
378 * @param m message queue for transmissions
382 notify_connect (void *cls,
383 const struct GNUNET_PeerIdentity *peer,
384 struct GNUNET_MQ_Handle *m)
386 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
388 fprintf (stdout, "Connected to different peer `%s'\n", GNUNET_i2s (&pid));
393 fprintf (stdout, "Successfully connected to `%s'\n", GNUNET_i2s (&pid));
401 * Function called to notify transport users that another
402 * peer disconnected from us.
405 * @param peer the peer that disconnected
406 * @param internal_cls NULL
409 notify_disconnect (void *cls,
410 const struct GNUNET_PeerIdentity *peer,
413 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
416 if (GNUNET_YES == benchmark_running)
419 "Disconnected from peer `%s' while benchmarking\n",
427 * Function called by the transport for each received message.
430 * @param message the message
434 check_dummy (void *cls, const struct GNUNET_MessageHeader *message)
436 return GNUNET_OK; /* all messages are fine */
441 * Function called by the transport for each received message.
444 * @param message the message
447 handle_dummy (void *cls, const struct GNUNET_MessageHeader *message)
449 if (! benchmark_receive)
453 "Received %u bytes\n",
454 (unsigned int) ntohs (message->size));
459 blacklist_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
461 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
464 fprintf (stdout, "Denying connection to `%s'\n", GNUNET_i2s (peer));
465 return GNUNET_SYSERR;
472 * Main function that will be run by the scheduler.
475 * @param args remaining command-line arguments
476 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
477 * @param mycfg configuration
483 const struct GNUNET_CONFIGURATION_Handle *mycfg)
485 struct GNUNET_MQ_MessageHandler handlers[] =
486 { GNUNET_MQ_hd_var_size (dummy,
487 GNUNET_MESSAGE_TYPE_DUMMY,
488 struct GNUNET_MessageHeader,
490 GNUNET_MQ_handler_end () };
492 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
495 if (GNUNET_MAX_MESSAGE_SIZE <= benchmark_size)
497 fprintf (stderr, "Message size too big!\n");
503 fprintf (stderr, "No peer identity given\n");
506 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
510 fprintf (stderr, "Failed to parse peer identity `%s'\n", cpid);
513 if (1 == benchmark_send)
517 "Trying to send %u messages with size %u to peer `%s'\n",
522 else if (1 == benchmark_receive)
525 "Trying to receive messages from peer `%s'\n",
530 fprintf (stderr, "No operation given\n");
534 ats = GNUNET_ATS_connectivity_init (cfg);
537 fprintf (stderr, "Failed to connect to ATS service\n");
542 handle = GNUNET_TRANSPORT_core_connect (cfg,
551 fprintf (stderr, "Failed to connect to transport service\n");
552 GNUNET_ATS_connectivity_done (ats);
558 bl_handle = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_cb, NULL);
559 ats_sh = GNUNET_ATS_connectivity_suggest (ats, &pid, 1);
560 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
565 main (int argc, char *const *argv)
569 benchmark_count = DEFAULT_MESSAGE_COUNT;
570 benchmark_size = DEFAULT_MESSAGE_SIZE;
571 benchmark_iterations = DEFAULT_ITERATION_COUNT;
572 benchmark_running = GNUNET_NO;
574 struct GNUNET_GETOPT_CommandLineOption options[] = {
575 GNUNET_GETOPT_option_flag ('s',
577 gettext_noop ("send data to peer"),
579 GNUNET_GETOPT_option_flag ('r',
581 gettext_noop ("receive data from peer"),
583 GNUNET_GETOPT_option_uint ('i',
586 gettext_noop ("iterations"),
587 &benchmark_iterations),
588 GNUNET_GETOPT_option_uint ('n',
591 gettext_noop ("number of messages to send"),
593 GNUNET_GETOPT_option_uint ('m',
596 gettext_noop ("message size to use"),
598 GNUNET_GETOPT_option_string ('p',
601 gettext_noop ("peer identity"),
603 GNUNET_GETOPT_option_verbose (&verbosity),
604 GNUNET_GETOPT_OPTION_END
607 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
611 GNUNET_PROGRAM_run (argc,
614 gettext_noop ("Direct access to transport service."),
618 GNUNET_free ((void *) argv);
619 if (GNUNET_OK == res)
625 /* end of gnunet-transport-profiler.c */