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"
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 \
56 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
59 * Benchmarking block size in bye
61 #define DEFAULT_MESSAGE_SIZE 1024
64 * Benchmarking message count
66 #define DEFAULT_MESSAGE_COUNT 1024
69 * Benchmarking iteration count
71 #define DEFAULT_ITERATION_COUNT 1
76 static int benchmark_send;
81 static int benchmark_receive;
86 static unsigned int benchmark_count;
91 static unsigned int benchmark_iterations;
96 static unsigned int benchmark_size;
101 static unsigned int benchmark_running;
104 * Which peer should we connect to?
109 * Handle to transport service.
111 static struct GNUNET_TRANSPORT_CoreHandle *handle;
114 * Handle to ATS service.
116 static struct GNUNET_ATS_ConnectivityHandle *ats;
119 * Configuration handle
121 static struct GNUNET_CONFIGURATION_Handle *cfg;
126 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
128 static struct Iteration *ihead;
130 static struct Iteration *itail;
133 * Global return value (0 success).
138 * Handle for transmissions.
140 static struct GNUNET_MQ_Handle *mq;
142 static struct GNUNET_TRANSPORT_Blacklist *bl_handle;
145 * Identity of the peer we transmit to / connect to.
146 * (equivalent to 'cpid' string).
148 static struct GNUNET_PeerIdentity pid;
151 * Selected level of verbosity.
153 static unsigned int verbosity;
157 * Task run in monitor mode when the user presses CTRL-C to abort.
158 * Stops monitoring activity.
163 shutdown_task(void *cls)
165 struct Iteration *icur;
166 struct Iteration *inext;
168 unsigned int iterations;
170 unsigned long long avg_duration;
173 float stddev_duration;
177 GNUNET_ATS_connectivity_suggest_cancel(ats_sh);
180 if (NULL != bl_handle)
182 GNUNET_TRANSPORT_blacklist_cancel(bl_handle);
187 GNUNET_ATS_connectivity_done(ats);
192 GNUNET_TRANSPORT_core_disconnect(handle);
197 fprintf(stdout, "\n");
200 * All time values in ms
202 * #messages;#messagesize;#avg_dur;#avg_rate;#duration_i0;#duration_i0;... */
206 /* First iteration to calculcate avg and stddev */
212 while (NULL != (icur = inext))
215 icur->rate = ((benchmark_count * benchmark_size) / 1024) /
216 ((float)icur->dur.rel_value_us / (1000 * 1000));
219 _("%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) * (icur->rate - avg_rate));
242 stddev_duration += (((icur->dur.rel_value_us / 1000) - avg_duration) *
243 ((icur->dur.rel_value_us / 1000) - avg_duration));
245 /* Calculate standard deviation rate */
246 stddev_rate = stddev_rate / iterations;
247 stddev_rate = sqrtf(stddev_rate);
249 /* Calculate standard deviation duration */
250 stddev_duration = stddev_duration / iterations;
251 stddev_duration = sqrtf(stddev_duration);
255 "%u;%u;%llu;%llu;%.2f;%.2f",
259 (unsigned long long)stddev_duration,
264 while (NULL != (icur = inext))
267 GNUNET_CONTAINER_DLL_remove(ihead, itail, icur);
271 (long long unsigned int)(icur->dur.rel_value_us / 1000),
278 if (benchmark_receive)
280 duration = GNUNET_TIME_absolute_get_duration(start_time);
282 "Received %llu bytes/s (%llu bytes in %s)\n",
283 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
285 GNUNET_STRINGS_relative_time_to_string(duration, GNUNET_YES));
288 fprintf(stdout, "\n");
297 * Function called to notify a client about the socket
298 * begin ready to queue more data. @a buf will be
299 * NULL and @a size zero if the socket was closed for
300 * writing in the meantime.
303 * @param size number of bytes available in @a buf
304 * @param buf where the callee should write the message
305 * @return number of bytes written to @a buf
310 struct GNUNET_MQ_Envelope *env;
311 struct GNUNET_MessageHeader *m;
315 env = GNUNET_MQ_msg_extra(m, benchmark_size, GNUNET_MESSAGE_TYPE_DUMMY);
316 memset(&m[1], 52, benchmark_size - sizeof(struct GNUNET_MessageHeader));
318 if (itail->msgs_sent < benchmark_count)
320 GNUNET_MQ_notify_sent(env, &send_msg, NULL);
326 GNUNET_MQ_send(mq, env);
327 if ((verbosity > 0) && (0 == itail->msgs_sent % 10))
328 fprintf(stdout, ".");
335 struct Iteration *icur;
340 benchmark_running = GNUNET_YES;
341 icur = GNUNET_new(struct Iteration);
342 GNUNET_CONTAINER_DLL_insert_tail(ihead, itail, icur);
343 icur->start = GNUNET_TIME_absolute_get();
347 "\nStarting benchmark, starting to send %u messages in %u byte blocks\n",
357 static int it_count = 0;
360 itail->dur = GNUNET_TIME_absolute_get_duration(itail->start);
361 if (it_count == benchmark_iterations)
363 benchmark_running = GNUNET_NO;
364 GNUNET_SCHEDULER_shutdown();
372 * Function called to notify transport users that another
373 * peer connected to us.
376 * @param peer the peer that connected
377 * @param m message queue for transmissions
381 notify_connect(void *cls,
382 const struct GNUNET_PeerIdentity *peer,
383 struct GNUNET_MQ_Handle *m)
385 if (0 != memcmp(&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
387 fprintf(stdout, "Connected to different peer `%s'\n", GNUNET_i2s(&pid));
392 fprintf(stdout, "Successfully connected to `%s'\n", GNUNET_i2s(&pid));
400 * Function called to notify transport users that another
401 * peer disconnected from us.
404 * @param peer the peer that disconnected
405 * @param internal_cls NULL
408 notify_disconnect(void *cls,
409 const struct GNUNET_PeerIdentity *peer,
412 if (0 != memcmp(&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
415 if (GNUNET_YES == benchmark_running)
418 "Disconnected from peer `%s' while benchmarking\n",
426 * Function called by the transport for each received message.
429 * @param message the message
433 check_dummy(void *cls, const struct GNUNET_MessageHeader *message)
435 return GNUNET_OK; /* all messages are fine */
440 * Function called by the transport for each received message.
443 * @param message the message
446 handle_dummy(void *cls, const struct GNUNET_MessageHeader *message)
448 if (!benchmark_receive)
452 "Received %u bytes\n",
453 (unsigned int)ntohs(message->size));
458 blacklist_cb(void *cls, const struct GNUNET_PeerIdentity *peer)
460 if (0 != memcmp(&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
463 fprintf(stdout, "Denying connection to `%s'\n", GNUNET_i2s(peer));
464 return GNUNET_SYSERR;
471 * Main function that will be run by the scheduler.
474 * @param args remaining command-line arguments
475 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
476 * @param mycfg configuration
482 const struct GNUNET_CONFIGURATION_Handle *mycfg)
484 struct GNUNET_MQ_MessageHandler handlers[] =
485 { GNUNET_MQ_hd_var_size(dummy,
486 GNUNET_MESSAGE_TYPE_DUMMY,
487 struct GNUNET_MessageHeader,
489 GNUNET_MQ_handler_end() };
491 cfg = (struct GNUNET_CONFIGURATION_Handle *)mycfg;
494 if (GNUNET_MAX_MESSAGE_SIZE <= benchmark_size)
496 fprintf(stderr, "Message size too big!\n");
502 fprintf(stderr, "No peer identity given\n");
505 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string(cpid,
509 fprintf(stderr, "Failed to parse peer identity `%s'\n", cpid);
512 if (1 == benchmark_send)
516 "Trying to send %u messages with size %u to peer `%s'\n",
521 else if (1 == benchmark_receive)
524 "Trying to receive messages from peer `%s'\n",
529 fprintf(stderr, "No operation given\n");
533 ats = GNUNET_ATS_connectivity_init(cfg);
536 fprintf(stderr, "Failed to connect to ATS service\n");
541 handle = GNUNET_TRANSPORT_core_connect(cfg,
550 fprintf(stderr, "Failed to connect to transport service\n");
551 GNUNET_ATS_connectivity_done(ats);
557 bl_handle = GNUNET_TRANSPORT_blacklist(cfg, &blacklist_cb, NULL);
558 ats_sh = GNUNET_ATS_connectivity_suggest(ats, &pid, 1);
559 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
564 main(int argc, char *const *argv)
568 benchmark_count = DEFAULT_MESSAGE_COUNT;
569 benchmark_size = DEFAULT_MESSAGE_SIZE;
570 benchmark_iterations = DEFAULT_ITERATION_COUNT;
571 benchmark_running = GNUNET_NO;
573 struct GNUNET_GETOPT_CommandLineOption options[] = {
574 GNUNET_GETOPT_option_flag('s',
576 gettext_noop("send data to peer"),
578 GNUNET_GETOPT_option_flag('r',
580 gettext_noop("receive data from peer"),
582 GNUNET_GETOPT_option_uint('i',
585 gettext_noop("iterations"),
586 &benchmark_iterations),
587 GNUNET_GETOPT_option_uint('n',
590 gettext_noop("number of messages to send"),
592 GNUNET_GETOPT_option_uint('m',
595 gettext_noop("message size to use"),
597 GNUNET_GETOPT_option_string('p',
600 gettext_noop("peer identity"),
602 GNUNET_GETOPT_option_verbose(&verbosity),
603 GNUNET_GETOPT_OPTION_END
606 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
610 GNUNET_PROGRAM_run(argc,
613 gettext_noop("Direct access to transport service."),
617 GNUNET_free((void *)argv);
618 if (GNUNET_OK == res)
623 /* end of gnunet-transport-profiler.c */