2 This file is part of GNUnet.
3 Copyright (C) 2010-2013 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.
21 * @file ats/perf_ats.c
22 * @brief ats benchmark: start peers and modify preferences, monitor change over time
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31 #include "ats-testing.h"
34 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35 #define TEST_ATS_PREFRENCE_START 1.0
36 #define TEST_ATS_PREFRENCE_DELTA 1.0
38 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
40 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
41 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
42 #define LOGGING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
43 #define TESTNAME_PREFIX "perf_ats_"
44 #define DEFAULT_SLAVES_NUM 2
45 #define DEFAULT_MASTERS_NUM 1
49 static struct GNUNET_SCHEDULER_Task * shutdown_task;
54 static struct GNUNET_SCHEDULER_Task * progress_task;
66 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
78 static char *testname;
83 static char *pref_str;
86 * ATS preference value
93 static struct GNUNET_TIME_Relative perf_duration;
98 static struct GNUNET_TIME_Relative log_frequency;
101 * Number master peers
103 static unsigned int num_masters;
106 * Array of master peers
108 static struct BenchmarkPeer *mps;
113 static unsigned int num_slaves;
116 * Array of master peers
118 static struct BenchmarkPeer *sps;
120 static struct LoggingHandle *l;
127 unsigned int duration;
128 struct BenchmarkPeer *mp;
129 struct BenchmarkPartner *p;
131 unsigned int kb_sent_sec;
132 double kb_sent_percent;
133 unsigned int kb_recv_sec;
134 double kb_recv_percent;
137 duration = (perf_duration.rel_value_us / (1000 * 1000));
138 for (c_m = 0; c_m < num_masters; c_m++)
142 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
143 mp->no, mp->total_bytes_sent / 1024, duration,
144 (mp->total_bytes_sent / 1024) / duration,
145 mp->total_bytes_received / 1024, duration,
146 (mp->total_bytes_received / 1024) / duration);
148 for (c_s = 0; c_s < num_slaves; c_s++)
150 p = &mp->partners[c_s];
154 kb_sent_percent = 0.0;
155 kb_recv_percent = 0.0;
160 kb_sent_sec = (p->bytes_sent / 1024) / duration;
161 kb_recv_sec = (p->bytes_received / 1024) / duration;
164 if (mp->total_bytes_sent > 0)
165 kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
166 if (mp->total_bytes_received > 0)
167 kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
168 if (1000 * p->messages_sent > 0)
169 rtt = p->total_app_rtt / (1000 * p->messages_sent);
171 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
172 (mp->pref_partner == p->dest) ? '*' : ' ',
174 kb_sent_sec, kb_sent_percent,
175 kb_recv_sec, kb_recv_percent);
177 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
178 (mp->pref_partner == p->dest) ? '*' : ' ',
179 mp->no, p->dest->no, rtt);
188 * @param tc the task context
191 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 if (GNUNET_YES == logging)
195 GNUNET_ATS_TEST_logging_clean_up(l);
197 shutdown_task = NULL;
198 if (NULL != progress_task)
200 fprintf (stderr, "0\n");
201 GNUNET_SCHEDULER_cancel (progress_task);
203 progress_task = NULL;
206 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
208 GNUNET_ATS_TEST_shutdown_topology();
216 progress_task = NULL;
218 fprintf (stderr, "%llu..",
219 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
222 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
223 &print_progress, NULL );
227 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct BenchmarkPeer *me = cls;
233 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
234 me->no, me->pref_partner->no, me->pref_value);
235 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
236 &me->pref_partner->id,
237 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
238 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
239 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
240 &ats_pref_task, cls);
249 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
251 if (NULL != shutdown_task)
252 GNUNET_SCHEDULER_cancel(shutdown_task);
253 shutdown_task = GNUNET_SCHEDULER_add_delayed(perf_duration, &do_shutdown,
256 progress_task = GNUNET_SCHEDULER_add_now(&print_progress, NULL );
258 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
259 "Topology connected, start benchmarking...\n");
261 /* Start sending test messages */
262 for (c_m = 0; c_m < num_masters; c_m++)
264 for (c_s = 0; c_s < num_slaves; c_s++)
266 GNUNET_ATS_TEST_generate_traffic_start (&mps[c_m], &mps[c_m].partners[c_s],
267 GNUNET_ATS_TEST_TG_LINEAR, UINT32_MAX, UINT32_MAX,
268 GNUNET_TIME_UNIT_MINUTES, GNUNET_TIME_UNIT_FOREVER_REL);
270 if (pref_val != GNUNET_ATS_PREFERENCE_END)
271 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now(&ats_pref_task, &mps[c_m]);
274 if (GNUNET_YES == logging)
275 l = GNUNET_ATS_TEST_logging_start (log_frequency, testname, mps,
276 num_masters, num_slaves, GNUNET_NO);
280 do_benchmark (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves)
285 GNUNET_SCHEDULER_add_now(&start_benchmark, NULL);
291 static struct BenchmarkPartner *
292 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
295 GNUNET_assert (NULL != me);
296 GNUNET_assert (NULL != peer);
298 for (c_m = 0; c_m < me->num_partners; c_m++)
300 /* Find a partner with other as destination */
301 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
302 sizeof(struct GNUNET_PeerIdentity)))
304 return &me->partners[c_m];
311 test_recv_cb (void *cls,
312 const struct GNUNET_PeerIdentity * peer,
313 const struct GNUNET_MessageHeader * message)
320 log_request_cb (void *cls, const struct GNUNET_HELLO_Address *address,
321 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
322 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
323 const struct GNUNET_ATS_Properties *ats)
325 struct BenchmarkPeer *me = cls;
326 struct BenchmarkPartner *p;
329 p = find_partner (me, &address->peer);
332 /* This is not one of my partners
333 * Will happen since the peers will connect to each other due to gossiping
337 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
339 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
340 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
341 p->bandwidth_in = ntohl (bandwidth_in.value__);
342 p->bandwidth_out = ntohl (bandwidth_out.value__);
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information for peers `%s'\n",
345 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
347 GNUNET_i2s (&p->dest->id));
349 GNUNET_free(peer_id);
351 GNUNET_ATS_TEST_logging_now (l);
356 * Start the performance test case
359 main (int argc, char *argv[])
367 char *prefs[] = GNUNET_ATS_PreferenceTypeString;
372 /* Determine testname
373 * perf_ats_<solver>_<transport>_<preference>[.exe]*/
375 /* Find test prefix, store in temp */
376 tmp = strstr (argv[0], TESTNAME_PREFIX);
379 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
380 return GNUNET_SYSERR;
383 /* Set tmp to end of test name prefix */
384 tmp += strlen (TESTNAME_PREFIX);
386 /* Determine solver name */
387 solver = GNUNET_strdup (tmp);
388 /* Remove .exe prefix */
389 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
392 /* Determine first '_' after solver */
393 tmp_sep = strchr (solver, '_');
396 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
398 return GNUNET_SYSERR;
401 comm_name = GNUNET_strdup (&tmp_sep[1]);
402 tmp_sep = strchr (comm_name, '_');
405 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
407 return GNUNET_SYSERR;
410 for (c = 0; c <= strlen (comm_name); c++)
411 comm_name[c] = toupper (comm_name[c]);
412 if (0 == strcmp (comm_name, "CORE"))
413 test_core = GNUNET_YES;
414 else if (0 == strcmp (comm_name, "TRANSPORT"))
415 test_core = GNUNET_NO;
418 GNUNET_free (comm_name);
419 GNUNET_free (solver);
420 return GNUNET_SYSERR;
423 pref_str = GNUNET_strdup(tmp_sep + 1);
425 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
427 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
429 for (c = 0; c <= strlen (pref_str); c++)
430 pref_str[c] = toupper (pref_str[c]);
433 if (0 != strcmp (pref_str, "NONE"))
435 for (c = 0; c < GNUNET_ATS_PREFERENCE_END; c++)
437 if (0 == strcmp (pref_str, prefs[c]))
446 /* abuse terminator to indicate no pref */
447 pref_val = GNUNET_ATS_PREFERENCE_END;
451 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
453 GNUNET_free(pref_str);
454 GNUNET_free (comm_name);
458 for (c = 0; c < (argc - 1); c++)
460 if (0 == strcmp (argv[c], "-d"))
465 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
466 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
470 perf_duration = BENCHMARK_DURATION;
472 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
474 for (c = 0; c < (argc - 1); c++)
476 if (0 == strcmp (argv[c], "-s"))
481 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
482 && (num_slaves >= 1))
483 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
485 num_slaves = DEFAULT_SLAVES_NUM;
488 num_slaves = DEFAULT_SLAVES_NUM;
490 for (c = 0; c < (argc - 1); c++)
492 if (0 == strcmp (argv[c], "-m"))
497 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
498 && (num_masters >= 2))
499 fprintf (stderr, "Starting %u master peers\n", num_masters);
501 num_masters = DEFAULT_MASTERS_NUM;
504 num_masters = DEFAULT_MASTERS_NUM;
507 for (c = 0; c < argc; c++)
509 if (0 == strcmp (argv[c], "-l"))
510 logging = GNUNET_YES;
513 if (GNUNET_YES == logging)
515 for (c = 0; c < (argc - 1); c++)
517 if (0 == strcmp (argv[c], "-f"))
522 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
523 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
527 log_frequency = LOGGING_FREQUENCY;
529 fprintf (stderr, "Using log frequency %llu ms\n",
530 (unsigned long long) (log_frequency.rel_value_us) / (1000));
533 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
535 if (num_slaves < num_masters)
537 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
538 GNUNET_free(test_name);
540 GNUNET_free(pref_str);
541 GNUNET_free (comm_name);
542 return GNUNET_SYSERR;
548 GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
549 num_slaves, num_masters,
559 /* end of file perf_ats.c */