2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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"
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_Handle *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).
136 * Handle for current transmission request.
138 static struct GNUNET_TRANSPORT_TransmitHandle *th;
140 static struct GNUNET_TRANSPORT_Blacklist *bl_handle;
143 * Identity of the peer we transmit to / connect to.
144 * (equivalent to 'cpid' string).
146 static struct GNUNET_PeerIdentity pid;
149 * Selected level of verbosity.
151 static int verbosity;
155 * Task run in monitor mode when the user presses CTRL-C to abort.
156 * Stops monitoring activity.
161 shutdown_task (void *cls)
163 struct Iteration *icur;
164 struct Iteration *inext;
166 unsigned int iterations;
168 unsigned long long avg_duration;
171 float stddev_duration;
175 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
180 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
183 if (NULL != bl_handle )
185 GNUNET_TRANSPORT_blacklist_cancel (bl_handle);
190 GNUNET_ATS_connectivity_done (ats);
195 GNUNET_TRANSPORT_disconnect (handle);
200 FPRINTF (stdout, "\n");
203 * All time values in ms
205 * #messages;#messagesize;#avg_dur;#avg_rate;#duration_i0;#duration_i0;... */
209 /* First iteration to calculcate avg and stddev */
215 while (NULL != (icur = inext))
218 icur->rate = ((benchmark_count * benchmark_size) / 1024) /
219 ((float) icur->dur.rel_value_us / (1000 * 1000));
221 FPRINTF (stdout, _("%llu B in %llu ms == %.2f KB/s!\n"),
222 ((long long unsigned int) benchmark_count * benchmark_size),
223 ((long long unsigned int) icur->dur.rel_value_us / 1000),
226 avg_duration += icur->dur.rel_value_us / (1000);
227 avg_rate += icur->rate;
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) *
243 (icur->rate-avg_rate));
244 stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration) *
245 ((icur->dur.rel_value_us / 1000) - avg_duration));
248 /* Calculate standard deviation rate */
249 stddev_rate = stddev_rate / iterations;
250 stddev_rate = sqrtf(stddev_rate);
252 /* Calculate standard deviation duration */
253 stddev_duration = stddev_duration / iterations;
254 stddev_duration = sqrtf(stddev_duration);
257 FPRINTF (stdout, _("%u;%u;%llu;%llu;%.2f;%.2f"),benchmark_count, benchmark_size,
258 avg_duration, (unsigned long long) stddev_duration, avg_rate, stddev_rate);
261 while (NULL != (icur = inext))
264 GNUNET_CONTAINER_DLL_remove (ihead, itail, icur);
266 FPRINTF (stdout, _(";%llu;%.2f"),
267 (long long unsigned int) (icur->dur.rel_value_us / 1000), icur->rate);
273 if (benchmark_receive)
275 duration = GNUNET_TIME_absolute_get_duration (start_time);
277 _("Received %llu bytes/s (%llu bytes in %s)\n"),
278 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
280 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
283 FPRINTF (stdout, _("\n"));
291 * Function called to notify a client about the socket
292 * begin ready to queue more data. @a buf will be
293 * NULL and @a size zero if the socket was closed for
294 * writing in the meantime.
297 * @param size number of bytes available in @a buf
298 * @param buf where the callee should write the message
299 * @return number of bytes written to @a buf
302 transmit_data (void *cls,
306 struct GNUNET_MessageHeader *m = buf;
309 if ((NULL == buf) || (0 == size))
317 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
318 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
319 m->size = ntohs (size);
320 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
321 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
323 if (itail->msgs_sent < benchmark_count)
325 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
326 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
335 if (itail->msgs_sent % 10 == 0 )
336 FPRINTF (stdout, _("."));
344 struct Iteration *icur;
349 benchmark_running = GNUNET_YES;
350 icur = GNUNET_new (struct Iteration);
351 GNUNET_CONTAINER_DLL_insert_tail (ihead, itail, icur);
352 icur->start = GNUNET_TIME_absolute_get();
356 _("\nStarting benchmark to `%s', starting to send %u messages in %u byte blocks\n"),
357 GNUNET_i2s (&pid), benchmark_count, benchmark_size);
359 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
360 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
371 static int it_count = 0;
374 itail->dur = GNUNET_TIME_absolute_get_duration (itail->start);
375 if (it_count == benchmark_iterations)
377 benchmark_running = GNUNET_NO;
378 GNUNET_SCHEDULER_shutdown ();
389 * Function called to notify transport users that another
390 * peer connected to us.
393 * @param peer the peer that connected
396 notify_connect (void *cls,
397 const struct GNUNET_PeerIdentity *peer)
399 if (0 != memcmp (&pid,
401 sizeof(struct GNUNET_PeerIdentity)))
404 _("Connected to different peer `%s'\n"),
411 _("Successfully connected to `%s'\n"),
418 * Function called to notify transport users that another
419 * peer disconnected from us.
422 * @param peer the peer that disconnected
425 notify_disconnect (void *cls,
426 const struct GNUNET_PeerIdentity *peer)
428 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
430 if (GNUNET_YES == benchmark_running)
432 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
440 * Function called by the transport for each received message.
443 * @param peer (claimed) identity of the other peer
444 * @param message the message
447 notify_receive (void *cls,
448 const struct GNUNET_PeerIdentity *peer,
449 const struct GNUNET_MessageHeader *message)
451 if (benchmark_receive)
453 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
457 _("Received %u bytes from %s\n"),
458 (unsigned int) ntohs (message->size),
466 blacklist_cb (void *cls,
467 const struct GNUNET_PeerIdentity *peer)
469 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
473 _("Denying connection to `%s'\n"),
475 return GNUNET_SYSERR;
484 * Function called with the result of the check if the 'transport'
485 * service is running.
487 * @param cls closure with our configuration
488 * @param result #GNUNET_YES if transport is running
491 testservice_task (void *cls, int result)
495 if (GNUNET_YES != result)
497 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
501 if (GNUNET_SERVER_MAX_MESSAGE_SIZE <= benchmark_size)
503 FPRINTF (stderr, _("Message size too big!\n"));
509 FPRINTF (stderr, _("No peer identity given\n"));
513 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
516 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
520 if (1 == benchmark_send)
524 _("Trying to send %u messages with size %u to peer `%s'\n"),
525 benchmark_count, benchmark_size, GNUNET_i2s (&pid));
527 else if (1 == benchmark_receive)
530 _("Trying to receive messages from peer `%s'\n"),
535 FPRINTF (stderr, _("No operation given\n"));
539 ats = GNUNET_ATS_connectivity_init (cfg);
542 FPRINTF (stderr, "%s", _("Failed to connect to ATS service\n"));
547 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
553 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
554 GNUNET_ATS_connectivity_done (ats);
560 bl_handle = GNUNET_TRANSPORT_blacklist (cfg,
563 ats_sh = GNUNET_ATS_connectivity_suggest (ats,
566 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
572 * Main function that will be run by the scheduler.
575 * @param args remaining command-line arguments
576 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
577 * @param mycfg configuration
583 const struct GNUNET_CONFIGURATION_Handle *mycfg)
585 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
586 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
587 &testservice_task, (void *) cfg);
592 main (int argc, char * const *argv)
595 benchmark_count = DEFAULT_MESSAGE_COUNT;
596 benchmark_size = DEFAULT_MESSAGE_SIZE;
597 benchmark_iterations = DEFAULT_ITERATION_COUNT;
598 benchmark_running = GNUNET_NO;
600 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
603 gettext_noop ("send data to peer"),
604 0, &GNUNET_GETOPT_set_one, &benchmark_send},
605 { 'r', "receive", NULL, gettext_noop
606 ("receive data from peer"), 0,
607 &GNUNET_GETOPT_set_one, &benchmark_receive},
608 { 'i', "iterations", NULL, gettext_noop
610 &GNUNET_GETOPT_set_uint, &benchmark_iterations},
611 { 'n', "number", NULL, gettext_noop
612 ("number of messages to send"), 1,
613 &GNUNET_GETOPT_set_uint, &benchmark_count},
614 { 'm', "messagesize", NULL, gettext_noop
615 ("message size to use"), 1,
616 &GNUNET_GETOPT_set_uint, &benchmark_size},
617 { 'p', "peer", "PEER",
618 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
620 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
621 GNUNET_GETOPT_OPTION_END
624 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
627 res = GNUNET_PROGRAM_run (argc, argv,
629 gettext_noop ("Direct access to transport service."),
632 GNUNET_free((void *) argv);
633 if (GNUNET_OK == res)
638 /* end of gnunet-transport-profiler.c */