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 iterations = 1; /* avoid division by zero */
232 /* Calculate average rate */
233 avg_rate /= iterations;
234 /* Calculate average duration */
235 avg_duration /= iterations;
240 while (NULL != (icur = inext))
243 stddev_rate += ((icur->rate-avg_rate) *
244 (icur->rate-avg_rate));
245 stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration) *
246 ((icur->dur.rel_value_us / 1000) - avg_duration));
249 /* Calculate standard deviation rate */
250 stddev_rate = stddev_rate / iterations;
251 stddev_rate = sqrtf(stddev_rate);
253 /* Calculate standard deviation duration */
254 stddev_duration = stddev_duration / iterations;
255 stddev_duration = sqrtf(stddev_duration);
259 "%u;%u;%llu;%llu;%.2f;%.2f",
263 (unsigned long long) stddev_duration,
268 while (NULL != (icur = inext))
271 GNUNET_CONTAINER_DLL_remove (ihead,
277 (long long unsigned int) (icur->dur.rel_value_us / 1000),
284 if (benchmark_receive)
286 duration = GNUNET_TIME_absolute_get_duration (start_time);
288 "Received %llu bytes/s (%llu bytes in %s)\n",
289 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
291 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
294 FPRINTF (stdout, "\n");
302 * Function called to notify a client about the socket
303 * begin ready to queue more data. @a buf will be
304 * NULL and @a size zero if the socket was closed for
305 * writing in the meantime.
308 * @param size number of bytes available in @a buf
309 * @param buf where the callee should write the message
310 * @return number of bytes written to @a buf
313 transmit_data (void *cls,
317 struct GNUNET_MessageHeader *m = buf;
320 if ((NULL == buf) || (0 == size))
328 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
329 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
330 m->size = ntohs (size);
331 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
332 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
334 if (itail->msgs_sent < benchmark_count)
336 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
337 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
346 if (itail->msgs_sent % 10 == 0 )
347 FPRINTF (stdout, ".");
355 struct Iteration *icur;
360 benchmark_running = GNUNET_YES;
361 icur = GNUNET_new (struct Iteration);
362 GNUNET_CONTAINER_DLL_insert_tail (ihead, itail, icur);
363 icur->start = GNUNET_TIME_absolute_get();
367 "\nStarting benchmark to `%s', starting to send %u messages in %u byte blocks\n",
372 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
373 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
384 static int it_count = 0;
387 itail->dur = GNUNET_TIME_absolute_get_duration (itail->start);
388 if (it_count == benchmark_iterations)
390 benchmark_running = GNUNET_NO;
391 GNUNET_SCHEDULER_shutdown ();
402 * Function called to notify transport users that another
403 * peer connected to us.
406 * @param peer the peer that connected
409 notify_connect (void *cls,
410 const struct GNUNET_PeerIdentity *peer)
412 if (0 != memcmp (&pid,
414 sizeof(struct GNUNET_PeerIdentity)))
417 "Connected to different peer `%s'\n",
424 "Successfully connected to `%s'\n",
431 * Function called to notify transport users that another
432 * peer disconnected from us.
435 * @param peer the peer that disconnected
438 notify_disconnect (void *cls,
439 const struct GNUNET_PeerIdentity *peer)
441 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
443 if (GNUNET_YES == benchmark_running)
446 "Disconnected from peer `%s' while benchmarking\n",
454 * Function called by the transport for each received message.
457 * @param peer (claimed) identity of the other peer
458 * @param message the message
461 notify_receive (void *cls,
462 const struct GNUNET_PeerIdentity *peer,
463 const struct GNUNET_MessageHeader *message)
465 if (benchmark_receive)
467 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
471 "Received %u bytes from %s\n",
472 (unsigned int) ntohs (message->size),
480 blacklist_cb (void *cls,
481 const struct GNUNET_PeerIdentity *peer)
483 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
487 "Denying connection to `%s'\n",
489 return GNUNET_SYSERR;
498 * Function called with the result of the check if the 'transport'
499 * service is running.
501 * @param cls closure with our configuration
502 * @param result #GNUNET_YES if transport is running
505 testservice_task (void *cls, int result)
509 if (GNUNET_YES != result)
512 "TRANSPORT service is not running\n");
516 if (GNUNET_SERVER_MAX_MESSAGE_SIZE <= benchmark_size)
519 "Message size too big!\n");
526 "No peer identity given\n");
530 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
534 "Failed to parse peer identity `%s'\n",
539 if (1 == benchmark_send)
543 "Trying to send %u messages with size %u to peer `%s'\n",
544 benchmark_count, benchmark_size,
547 else if (1 == benchmark_receive)
550 "Trying to receive messages from peer `%s'\n",
556 "No operation given\n");
560 ats = GNUNET_ATS_connectivity_init (cfg);
564 "Failed to connect to ATS service\n");
569 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
576 "Failed to connect to transport service\n");
577 GNUNET_ATS_connectivity_done (ats);
583 bl_handle = GNUNET_TRANSPORT_blacklist (cfg,
586 ats_sh = GNUNET_ATS_connectivity_suggest (ats,
589 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
595 * Main function that will be run by the scheduler.
598 * @param args remaining command-line arguments
599 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
600 * @param mycfg configuration
606 const struct GNUNET_CONFIGURATION_Handle *mycfg)
608 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
609 GNUNET_CLIENT_service_test ("transport",
611 GNUNET_TIME_UNIT_SECONDS,
618 main (int argc, char * const *argv)
621 benchmark_count = DEFAULT_MESSAGE_COUNT;
622 benchmark_size = DEFAULT_MESSAGE_SIZE;
623 benchmark_iterations = DEFAULT_ITERATION_COUNT;
624 benchmark_running = GNUNET_NO;
626 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
629 gettext_noop ("send data to peer"),
630 0, &GNUNET_GETOPT_set_one, &benchmark_send},
631 { 'r', "receive", NULL, gettext_noop
632 ("receive data from peer"), 0,
633 &GNUNET_GETOPT_set_one, &benchmark_receive},
634 { 'i', "iterations", NULL, gettext_noop
636 &GNUNET_GETOPT_set_uint, &benchmark_iterations},
637 { 'n', "number", NULL, gettext_noop
638 ("number of messages to send"), 1,
639 &GNUNET_GETOPT_set_uint, &benchmark_count},
640 { 'm', "messagesize", NULL, gettext_noop
641 ("message size to use"), 1,
642 &GNUNET_GETOPT_set_uint, &benchmark_size},
643 { 'p', "peer", "PEER",
644 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
646 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
647 GNUNET_GETOPT_OPTION_END
650 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
653 res = GNUNET_PROGRAM_run (argc, argv,
655 gettext_noop ("Direct access to transport service."),
658 GNUNET_free((void *) argv);
659 if (GNUNET_OK == res)
664 /* end of gnunet-transport-profiler.c */