2 This file is part of GNUnet.
3 Copyright (C) 2010-2013, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
16 * @file ats/perf_ats.c
17 * @brief ats benchmark: start peers and modify preferences, monitor change over time
18 * @author Christian Grothoff
19 * @author Matthias Wachs
22 #include "gnunet_util_lib.h"
23 #include "gnunet_testbed_service.h"
24 #include "gnunet_ats_service.h"
25 #include "gnunet_core_service.h"
26 #include "ats-testing.h"
29 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
30 #define TEST_ATS_PREFRENCE_START 1.0
31 #define TEST_ATS_PREFRENCE_DELTA 1.0
33 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
36 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
37 #define LOGGING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
38 #define TESTNAME_PREFIX "perf_ats_"
39 #define DEFAULT_SLAVES_NUM 2
40 #define DEFAULT_MASTERS_NUM 1
45 static struct GNUNET_SCHEDULER_Task *timeout_task;
50 static struct GNUNET_SCHEDULER_Task *progress_task;
63 * Test core (#GNUNET_YES) or transport (#GNUNET_NO)
75 static char *testname;
80 static char *pref_str;
83 * ATS preference value
90 static struct GNUNET_TIME_Relative perf_duration;
95 static struct GNUNET_TIME_Relative log_frequency;
100 static unsigned int num_masters;
103 * Array of master peers
105 static struct BenchmarkPeer *mps;
110 static unsigned int num_slaves;
113 * Array of master peers
115 static struct BenchmarkPeer *sps;
117 static struct LoggingHandle *l;
125 unsigned int duration;
126 struct BenchmarkPeer *mp;
127 struct BenchmarkPartner *p;
129 unsigned int kb_sent_sec;
130 double kb_sent_percent;
131 unsigned int kb_recv_sec;
132 double kb_recv_percent;
135 duration = 1 + (perf_duration.rel_value_us / (1000 * 1000));
136 for (c_m = 0; c_m < num_masters; c_m++)
140 "Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n",
141 mp->no, mp->total_bytes_sent / 1024, duration,
142 (mp->total_bytes_sent / 1024) / duration,
143 mp->total_bytes_received / 1024, duration,
144 (mp->total_bytes_received / 1024) / duration);
146 for (c_s = 0; c_s < num_slaves; c_s++)
148 p = &mp->partners[c_s];
151 kb_sent_percent = 0.0;
152 kb_recv_percent = 0.0;
157 kb_sent_sec = (p->bytes_sent / 1024) / duration;
158 kb_recv_sec = (p->bytes_received / 1024) / duration;
161 if (mp->total_bytes_sent > 0)
162 kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
163 if (mp->total_bytes_received > 0)
164 kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
165 if (1000 * p->messages_sent > 0)
166 rtt = p->total_app_rtt / (1000 * p->messages_sent);
168 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
169 (mp->pref_partner == p->dest) ? '*' : ' ',
171 kb_sent_sec, kb_sent_percent,
172 kb_recv_sec, kb_recv_percent);
174 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
175 (mp->pref_partner == p->dest) ? '*' : ' ',
176 mp->no, p->dest->no, rtt);
188 do_shutdown (void *cls)
190 if (GNUNET_YES == logging)
191 GNUNET_ATS_TEST_logging_clean_up(l);
192 if (NULL != timeout_task)
194 GNUNET_SCHEDULER_cancel (timeout_task);
197 if (NULL != progress_task)
199 fprintf (stderr, "0\n");
200 GNUNET_SCHEDULER_cancel (progress_task);
201 progress_task = NULL;
203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
204 "Benchmarking done\n");
205 GNUNET_ATS_TEST_shutdown_topology ();
215 do_timeout (void *cls)
217 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
218 "Terminating with timeout\n");
221 GNUNET_SCHEDULER_shutdown ();
226 print_progress (void *cls)
230 progress_task = NULL;
233 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
236 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
243 ats_pref_task (void *cls)
245 struct BenchmarkPeer *me = cls;
249 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
250 me->no, me->pref_partner->no, me->pref_value);
251 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
252 &me->pref_partner->id,
253 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
254 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
255 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
256 &ats_pref_task, cls);
261 start_benchmark (void *cls)
266 progress_task = GNUNET_SCHEDULER_add_now (&print_progress,
269 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
270 "Topology connected, start benchmarking...\n");
272 /* Start sending test messages */
273 for (c_m = 0; c_m < num_masters; c_m++)
275 for (c_s = 0; c_s < num_slaves; c_s++)
277 GNUNET_ATS_TEST_generate_traffic_start (&mps[c_m],
278 &mps[c_m].partners[c_s],
279 GNUNET_ATS_TEST_TG_LINEAR,
282 GNUNET_TIME_UNIT_MINUTES,
283 GNUNET_TIME_UNIT_FOREVER_REL);
285 if (pref_val != GNUNET_ATS_PREFERENCE_END)
286 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task,
290 if (GNUNET_YES == logging)
291 l = GNUNET_ATS_TEST_logging_start (log_frequency,
293 num_masters, num_slaves,
299 do_benchmark (void *cls,
300 struct BenchmarkPeer *masters,
301 struct BenchmarkPeer *slaves)
305 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
307 timeout_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
310 progress_task = GNUNET_SCHEDULER_add_now (&start_benchmark,
315 static struct BenchmarkPartner *
316 find_partner (struct BenchmarkPeer *me,
317 const struct GNUNET_PeerIdentity *peer)
320 GNUNET_assert (NULL != me);
321 GNUNET_assert (NULL != peer);
323 for (c_m = 0; c_m < me->num_partners; c_m++)
325 /* Find a partner with other as destination */
326 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
327 sizeof(struct GNUNET_PeerIdentity)))
329 return &me->partners[c_m];
337 log_request_cb (void *cls,
338 const struct GNUNET_HELLO_Address *address,
340 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
341 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
342 const struct GNUNET_ATS_Properties *ats)
344 struct BenchmarkPeer *me = cls;
345 struct BenchmarkPartner *p;
348 p = find_partner (me, &address->peer);
351 /* This is not one of my partners
352 * Will happen since the peers will connect to each other due to gossiping
356 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
358 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
359 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
360 p->bandwidth_in = ntohl (bandwidth_in.value__);
361 p->bandwidth_out = ntohl (bandwidth_out.value__);
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "%s [%u] received ATS information for peers `%s'\n",
365 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
367 GNUNET_i2s (&p->dest->id));
369 GNUNET_free (peer_id);
371 GNUNET_ATS_TEST_logging_now (l);
376 * Start the performance test case
379 main (int argc, char *argv[])
387 char *prefs[] = GNUNET_ATS_PreferenceTypeString;
392 /* Determine testname
393 * perf_ats_<solver>_<transport>_<preference>[.exe]*/
395 /* Find test prefix, store in temp */
396 tmp = strstr (argv[0], TESTNAME_PREFIX);
399 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
400 return GNUNET_SYSERR;
403 /* Set tmp to end of test name prefix */
404 tmp += strlen (TESTNAME_PREFIX);
406 /* Determine solver name */
407 solver = GNUNET_strdup (tmp);
408 /* Remove .exe prefix */
409 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
412 /* Determine first '_' after solver */
413 tmp_sep = strchr (solver, '_');
416 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
418 return GNUNET_SYSERR;
421 comm_name = GNUNET_strdup (&tmp_sep[1]);
422 tmp_sep = strchr (comm_name, '_');
425 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
427 return GNUNET_SYSERR;
430 for (c = 0; c <= strlen (comm_name); c++)
431 comm_name[c] = toupper (comm_name[c]);
432 if (0 == strcmp (comm_name, "CORE"))
433 test_core = GNUNET_YES;
434 else if (0 == strcmp (comm_name, "TRANSPORT"))
435 test_core = GNUNET_NO;
438 GNUNET_free (comm_name);
439 GNUNET_free (solver);
440 return GNUNET_SYSERR;
443 pref_str = GNUNET_strdup(tmp_sep + 1);
445 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
447 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
449 for (c = 0; c <= strlen (pref_str); c++)
450 pref_str[c] = toupper (pref_str[c]);
453 if (0 != strcmp (pref_str, "NONE"))
455 for (c = 0; c < GNUNET_ATS_PREFERENCE_END; c++)
457 if (0 == strcmp (pref_str, prefs[c]))
466 /* abuse terminator to indicate no pref */
467 pref_val = GNUNET_ATS_PREFERENCE_END;
471 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
473 GNUNET_free(pref_str);
474 GNUNET_free (comm_name);
478 for (c = 0; c < (argc - 1); c++)
480 if (0 == strcmp (argv[c], "-d"))
485 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
486 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
490 perf_duration = BENCHMARK_DURATION;
492 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
494 for (c = 0; c < (argc - 1); c++)
496 if (0 == strcmp (argv[c], "-s"))
501 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
502 && (num_slaves >= 1))
503 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
505 num_slaves = DEFAULT_SLAVES_NUM;
508 num_slaves = DEFAULT_SLAVES_NUM;
510 for (c = 0; c < (argc - 1); c++)
512 if (0 == strcmp (argv[c], "-m"))
517 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
518 && (num_masters >= 2))
519 fprintf (stderr, "Starting %u master peers\n", num_masters);
521 num_masters = DEFAULT_MASTERS_NUM;
524 num_masters = DEFAULT_MASTERS_NUM;
527 for (c = 0; c < argc; c++)
529 if (0 == strcmp (argv[c], "-l"))
530 logging = GNUNET_YES;
533 if (GNUNET_YES == logging)
535 for (c = 0; c < (argc - 1); c++)
537 if (0 == strcmp (argv[c], "-f"))
542 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
543 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
547 log_frequency = LOGGING_FREQUENCY;
549 fprintf (stderr, "Using log frequency %llu ms\n",
550 (unsigned long long) (log_frequency.rel_value_us) / (1000));
553 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
555 if (num_slaves < num_masters)
558 "Number of master peers is lower than slaves! exit...\n");
559 GNUNET_free(test_name);
561 GNUNET_free(pref_str);
562 GNUNET_free (comm_name);
563 return GNUNET_SYSERR;
569 GNUNET_ATS_TEST_create_topology ("perf-ats",
581 /* end of file perf_ats.c */