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 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/>.
19 * @file ats/perf_ats.c
20 * @brief ats benchmark: start peers and modify preferences, monitor change over time
21 * @author Christian Grothoff
22 * @author Matthias Wachs
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testbed_service.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_core_service.h"
29 #include "ats-testing.h"
32 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
33 #define TEST_ATS_PREFRENCE_START 1.0
34 #define TEST_ATS_PREFRENCE_DELTA 1.0
36 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
38 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
39 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
40 #define LOGGING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
41 #define TESTNAME_PREFIX "perf_ats_"
42 #define DEFAULT_SLAVES_NUM 2
43 #define DEFAULT_MASTERS_NUM 1
48 static struct GNUNET_SCHEDULER_Task *timeout_task;
53 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;
128 unsigned int duration;
129 struct BenchmarkPeer *mp;
130 struct BenchmarkPartner *p;
132 unsigned int kb_sent_sec;
133 double kb_sent_percent;
134 unsigned int kb_recv_sec;
135 double kb_recv_percent;
138 duration = 1 + (perf_duration.rel_value_us / (1000 * 1000));
139 for (c_m = 0; c_m < num_masters; c_m++)
143 "Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n",
144 mp->no, mp->total_bytes_sent / 1024, duration,
145 (mp->total_bytes_sent / 1024) / duration,
146 mp->total_bytes_received / 1024, duration,
147 (mp->total_bytes_received / 1024) / duration);
149 for (c_s = 0; c_s < num_slaves; c_s++)
151 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);
191 do_shutdown (void *cls)
193 if (GNUNET_YES == logging)
194 GNUNET_ATS_TEST_logging_clean_up(l);
195 if (NULL != timeout_task)
197 GNUNET_SCHEDULER_cancel (timeout_task);
200 if (NULL != progress_task)
202 fprintf (stderr, "0\n");
203 GNUNET_SCHEDULER_cancel (progress_task);
204 progress_task = NULL;
206 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
207 "Benchmarking done\n");
208 GNUNET_ATS_TEST_shutdown_topology ();
218 do_timeout (void *cls)
220 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
221 "Terminating with timeout\n");
224 GNUNET_SCHEDULER_shutdown ();
229 print_progress (void *cls)
233 progress_task = NULL;
236 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
239 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
246 ats_pref_task (void *cls)
248 struct BenchmarkPeer *me = cls;
252 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
253 me->no, me->pref_partner->no, me->pref_value);
254 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
255 &me->pref_partner->id,
256 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
257 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
258 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
259 &ats_pref_task, cls);
264 start_benchmark (void *cls)
269 progress_task = GNUNET_SCHEDULER_add_now (&print_progress,
272 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
273 "Topology connected, start benchmarking...\n");
275 /* Start sending test messages */
276 for (c_m = 0; c_m < num_masters; c_m++)
278 for (c_s = 0; c_s < num_slaves; c_s++)
280 GNUNET_ATS_TEST_generate_traffic_start (&mps[c_m],
281 &mps[c_m].partners[c_s],
282 GNUNET_ATS_TEST_TG_LINEAR,
285 GNUNET_TIME_UNIT_MINUTES,
286 GNUNET_TIME_UNIT_FOREVER_REL);
288 if (pref_val != GNUNET_ATS_PREFERENCE_END)
289 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task,
293 if (GNUNET_YES == logging)
294 l = GNUNET_ATS_TEST_logging_start (log_frequency,
296 num_masters, num_slaves,
302 do_benchmark (void *cls,
303 struct BenchmarkPeer *masters,
304 struct BenchmarkPeer *slaves)
308 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
310 timeout_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
313 progress_task = GNUNET_SCHEDULER_add_now (&start_benchmark,
318 static struct BenchmarkPartner *
319 find_partner (struct BenchmarkPeer *me,
320 const struct GNUNET_PeerIdentity *peer)
323 GNUNET_assert (NULL != me);
324 GNUNET_assert (NULL != peer);
326 for (c_m = 0; c_m < me->num_partners; c_m++)
328 /* Find a partner with other as destination */
329 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
330 sizeof(struct GNUNET_PeerIdentity)))
332 return &me->partners[c_m];
340 log_request_cb (void *cls,
341 const struct GNUNET_HELLO_Address *address,
343 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
344 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
345 const struct GNUNET_ATS_Properties *ats)
347 struct BenchmarkPeer *me = cls;
348 struct BenchmarkPartner *p;
351 p = find_partner (me, &address->peer);
354 /* This is not one of my partners
355 * Will happen since the peers will connect to each other due to gossiping
359 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
361 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
362 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
363 p->bandwidth_in = ntohl (bandwidth_in.value__);
364 p->bandwidth_out = ntohl (bandwidth_out.value__);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "%s [%u] received ATS information for peers `%s'\n",
368 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
370 GNUNET_i2s (&p->dest->id));
372 GNUNET_free (peer_id);
374 GNUNET_ATS_TEST_logging_now (l);
379 * Start the performance test case
382 main (int argc, char *argv[])
390 char *prefs[] = GNUNET_ATS_PreferenceTypeString;
395 /* Determine testname
396 * perf_ats_<solver>_<transport>_<preference>[.exe]*/
398 /* Find test prefix, store in temp */
399 tmp = strstr (argv[0], TESTNAME_PREFIX);
402 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
403 return GNUNET_SYSERR;
406 /* Set tmp to end of test name prefix */
407 tmp += strlen (TESTNAME_PREFIX);
409 /* Determine solver name */
410 solver = GNUNET_strdup (tmp);
411 /* Remove .exe prefix */
412 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
415 /* Determine first '_' after solver */
416 tmp_sep = strchr (solver, '_');
419 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
421 return GNUNET_SYSERR;
424 comm_name = GNUNET_strdup (&tmp_sep[1]);
425 tmp_sep = strchr (comm_name, '_');
428 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
430 return GNUNET_SYSERR;
433 for (c = 0; c <= strlen (comm_name); c++)
434 comm_name[c] = toupper (comm_name[c]);
435 if (0 == strcmp (comm_name, "CORE"))
436 test_core = GNUNET_YES;
437 else if (0 == strcmp (comm_name, "TRANSPORT"))
438 test_core = GNUNET_NO;
441 GNUNET_free (comm_name);
442 GNUNET_free (solver);
443 return GNUNET_SYSERR;
446 pref_str = GNUNET_strdup(tmp_sep + 1);
448 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
450 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
452 for (c = 0; c <= strlen (pref_str); c++)
453 pref_str[c] = toupper (pref_str[c]);
456 if (0 != strcmp (pref_str, "NONE"))
458 for (c = 0; c < GNUNET_ATS_PREFERENCE_END; c++)
460 if (0 == strcmp (pref_str, prefs[c]))
469 /* abuse terminator to indicate no pref */
470 pref_val = GNUNET_ATS_PREFERENCE_END;
474 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
476 GNUNET_free(pref_str);
477 GNUNET_free (comm_name);
481 for (c = 0; c < (argc - 1); c++)
483 if (0 == strcmp (argv[c], "-d"))
488 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
489 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
493 perf_duration = BENCHMARK_DURATION;
495 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
497 for (c = 0; c < (argc - 1); c++)
499 if (0 == strcmp (argv[c], "-s"))
504 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
505 && (num_slaves >= 1))
506 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
508 num_slaves = DEFAULT_SLAVES_NUM;
511 num_slaves = DEFAULT_SLAVES_NUM;
513 for (c = 0; c < (argc - 1); c++)
515 if (0 == strcmp (argv[c], "-m"))
520 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
521 && (num_masters >= 2))
522 fprintf (stderr, "Starting %u master peers\n", num_masters);
524 num_masters = DEFAULT_MASTERS_NUM;
527 num_masters = DEFAULT_MASTERS_NUM;
530 for (c = 0; c < argc; c++)
532 if (0 == strcmp (argv[c], "-l"))
533 logging = GNUNET_YES;
536 if (GNUNET_YES == logging)
538 for (c = 0; c < (argc - 1); c++)
540 if (0 == strcmp (argv[c], "-f"))
545 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
546 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
550 log_frequency = LOGGING_FREQUENCY;
552 fprintf (stderr, "Using log frequency %llu ms\n",
553 (unsigned long long) (log_frequency.rel_value_us) / (1000));
556 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
558 if (num_slaves < num_masters)
561 "Number of master peers is lower than slaves! exit...\n");
562 GNUNET_free(test_name);
564 GNUNET_free(pref_str);
565 GNUNET_free (comm_name);
566 return GNUNET_SYSERR;
572 GNUNET_ATS_TEST_create_topology ("perf-ats",
584 /* end of file perf_ats.c */