2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 Christian Grothoff (and other contributing authors)
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 * Task scheduled for cleanup / termination of the process.
151 static struct GNUNET_SCHEDULER_Task * end;
154 * Selected level of verbosity.
156 static int verbosity;
160 * Task run in monitor mode when the user presses CTRL-C to abort.
161 * Stops monitoring activity.
164 * @param tc scheduler context
167 shutdown_task (void *cls,
168 const struct GNUNET_SCHEDULER_TaskContext *tc)
170 struct Iteration *icur;
171 struct Iteration *inext;
173 unsigned int iterations;
175 unsigned long long avg_duration;
178 float stddev_duration;
182 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
187 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
190 if (NULL != bl_handle )
192 GNUNET_TRANSPORT_blacklist_cancel (bl_handle);
197 GNUNET_ATS_connectivity_done (ats);
202 GNUNET_TRANSPORT_disconnect (handle);
207 FPRINTF (stdout, "\n");
210 * All time values in ms
212 * #messages;#messagesize;#avg_dur;#avg_rate;#duration_i0;#duration_i0;... */
216 /* First iteration to calculcate avg and stddev */
222 while (NULL != (icur = inext))
225 icur->rate = ((benchmark_count * benchmark_size) / 1024) /
226 ((float) icur->dur.rel_value_us / (1000 * 1000));
228 FPRINTF (stdout, _("%llu B in %llu ms == %.2f KB/s!\n"),
229 ((long long unsigned int) benchmark_count * benchmark_size),
230 ((long long unsigned int) icur->dur.rel_value_us / 1000),
233 avg_duration += icur->dur.rel_value_us / (1000);
234 avg_rate += icur->rate;
238 /* Calculate average rate */
239 avg_rate /= iterations;
240 /* Calculate average duration */
241 avg_duration /= iterations;
246 while (NULL != (icur = inext))
249 stddev_rate += ((icur->rate-avg_rate) *
250 (icur->rate-avg_rate));
251 stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration) *
252 ((icur->dur.rel_value_us / 1000) - avg_duration));
255 /* Calculate standard deviation rate */
256 stddev_rate = stddev_rate / iterations;
257 stddev_rate = sqrtf(stddev_rate);
259 /* Calculate standard deviation duration */
260 stddev_duration = stddev_duration / iterations;
261 stddev_duration = sqrtf(stddev_duration);
264 FPRINTF (stdout, _("%u;%u;%llu;%llu;%.2f;%.2f"),benchmark_count, benchmark_size,
265 avg_duration, (unsigned long long) stddev_duration, avg_rate, stddev_rate);
268 while (NULL != (icur = inext))
271 GNUNET_CONTAINER_DLL_remove (ihead, itail, icur);
273 FPRINTF (stdout, _(";%llu;%.2f"),
274 (long long unsigned int) (icur->dur.rel_value_us / 1000), icur->rate);
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"));
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
309 transmit_data (void *cls,
313 struct GNUNET_MessageHeader *m = buf;
316 if ((NULL == buf) || (0 == size))
324 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
325 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
326 m->size = ntohs (size);
327 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
328 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
330 if (itail->msgs_sent < benchmark_count)
332 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
333 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
342 if (itail->msgs_sent % 10 == 0 )
343 FPRINTF (stdout, _("."));
351 struct Iteration *icur;
356 benchmark_running = GNUNET_YES;
357 icur = GNUNET_new (struct Iteration);
358 GNUNET_CONTAINER_DLL_insert_tail (ihead, itail, icur);
359 icur->start = GNUNET_TIME_absolute_get();
363 _("\nStarting benchmark to `%s', starting to send %u messages in %u byte blocks\n"),
364 GNUNET_i2s (&pid), benchmark_count, benchmark_size);
366 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
367 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
378 static int it_count = 0;
381 itail->dur = GNUNET_TIME_absolute_get_duration (itail->start);
382 if (it_count == benchmark_iterations)
384 benchmark_running = GNUNET_NO;
386 GNUNET_SCHEDULER_cancel (end);
387 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
398 * Function called to notify transport users that another
399 * peer connected to us.
402 * @param peer the peer that connected
405 notify_connect (void *cls,
406 const struct GNUNET_PeerIdentity *peer)
408 if (0 != memcmp (&pid,
410 sizeof(struct GNUNET_PeerIdentity)))
413 _("Connected to different peer `%s'\n"),
420 _("Successfully connected to `%s'\n"),
427 * Function called to notify transport users that another
428 * peer disconnected from us.
431 * @param peer the peer that disconnected
434 notify_disconnect (void *cls,
435 const struct GNUNET_PeerIdentity *peer)
437 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
439 if (GNUNET_YES == benchmark_running)
441 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
449 * Function called by the transport for each received message.
452 * @param peer (claimed) identity of the other peer
453 * @param message the message
456 notify_receive (void *cls,
457 const struct GNUNET_PeerIdentity *peer,
458 const struct GNUNET_MessageHeader *message)
460 if (benchmark_receive)
462 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
466 _("Received %u bytes from %s\n"),
467 (unsigned int) ntohs (message->size),
475 blacklist_cb (void *cls,
476 const struct GNUNET_PeerIdentity *peer)
478 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
482 _("Denying connection to `%s'\n"),
484 return GNUNET_SYSERR;
493 * Function called with the result of the check if the 'transport'
494 * service is running.
496 * @param cls closure with our configuration
497 * @param result #GNUNET_YES if transport is running
500 testservice_task (void *cls, int result)
504 if (GNUNET_YES != result)
506 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
510 if (GNUNET_SERVER_MAX_MESSAGE_SIZE <= benchmark_size)
512 FPRINTF (stderr, _("Message size too big!\n"));
518 FPRINTF (stderr, _("No peer identity given\n"));
522 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
525 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
529 if (1 == benchmark_send)
533 _("Trying to send %u messages with size %u to peer `%s'\n"),
534 benchmark_count, benchmark_size, GNUNET_i2s (&pid));
536 else if (1 == benchmark_receive)
539 _("Trying to receive messages from peer `%s'\n"),
544 FPRINTF (stderr, _("No operation given\n"));
548 ats = GNUNET_ATS_connectivity_init (cfg);
551 FPRINTF (stderr, "%s", _("Failed to connect to ATS service\n"));
556 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
562 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
563 GNUNET_ATS_connectivity_done (ats);
569 bl_handle = GNUNET_TRANSPORT_blacklist (cfg,
572 ats_sh = GNUNET_ATS_connectivity_suggest (ats,
575 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
582 * Main function that will be run by the scheduler.
585 * @param args remaining command-line arguments
586 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
587 * @param mycfg configuration
593 const struct GNUNET_CONFIGURATION_Handle *mycfg)
595 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
596 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
597 &testservice_task, (void *) cfg);
602 main (int argc, char * const *argv)
605 benchmark_count = DEFAULT_MESSAGE_COUNT;
606 benchmark_size = DEFAULT_MESSAGE_SIZE;
607 benchmark_iterations = DEFAULT_ITERATION_COUNT;
608 benchmark_running = GNUNET_NO;
610 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
613 gettext_noop ("send data to peer"),
614 0, &GNUNET_GETOPT_set_one, &benchmark_send},
615 { 'r', "receive", NULL, gettext_noop
616 ("receive data from peer"), 0,
617 &GNUNET_GETOPT_set_one, &benchmark_receive},
618 { 'i', "iterations", NULL, gettext_noop
620 &GNUNET_GETOPT_set_uint, &benchmark_iterations},
621 { 'n', "number", NULL, gettext_noop
622 ("number of messages to send"), 1,
623 &GNUNET_GETOPT_set_uint, &benchmark_count},
624 { 'm', "messagesize", NULL, gettext_noop
625 ("message size to use"), 1,
626 &GNUNET_GETOPT_set_uint, &benchmark_size},
627 { 'p', "peer", "PEER",
628 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
630 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
631 GNUNET_GETOPT_OPTION_END
634 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
637 res = GNUNET_PROGRAM_run (argc, argv,
639 gettext_noop ("Direct access to transport service."),
642 GNUNET_free((void *) argv);
643 if (GNUNET_OK == res)
648 /* end of gnunet-transport-profiler.c */