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
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
50 static struct GNUNET_SCHEDULER_Task *timeout_task;
55 static struct GNUNET_SCHEDULER_Task *progress_task;
68 * Test core (#GNUNET_YES) or transport (#GNUNET_NO)
80 static char *testname;
85 static char *pref_str;
88 * ATS preference value
95 static struct GNUNET_TIME_Relative perf_duration;
100 static struct GNUNET_TIME_Relative log_frequency;
103 * Number master peers
105 static unsigned int num_masters;
108 * Array of master peers
110 static struct BenchmarkPeer *mps;
115 static unsigned int num_slaves;
118 * Array of master peers
120 static struct BenchmarkPeer *sps;
122 static struct LoggingHandle *l;
130 unsigned int duration;
131 struct BenchmarkPeer *mp;
132 struct BenchmarkPartner *p;
134 unsigned int kb_sent_sec;
135 double kb_sent_percent;
136 unsigned int kb_recv_sec;
137 double kb_recv_percent;
140 duration = 1 + (perf_duration.rel_value_us / (1000 * 1000));
141 for (c_m = 0; c_m < num_masters; c_m++)
145 "Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n",
146 mp->no, mp->total_bytes_sent / 1024, duration,
147 (mp->total_bytes_sent / 1024) / duration,
148 mp->total_bytes_received / 1024, duration,
149 (mp->total_bytes_received / 1024) / duration);
151 for (c_s = 0; c_s < num_slaves; c_s++)
153 p = &mp->partners[c_s];
156 kb_sent_percent = 0.0;
157 kb_recv_percent = 0.0;
162 kb_sent_sec = (p->bytes_sent / 1024) / duration;
163 kb_recv_sec = (p->bytes_received / 1024) / duration;
166 if (mp->total_bytes_sent > 0)
167 kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
168 if (mp->total_bytes_received > 0)
169 kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
170 if (1000 * p->messages_sent > 0)
171 rtt = p->total_app_rtt / (1000 * p->messages_sent);
173 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
174 (mp->pref_partner == p->dest) ? '*' : ' ',
176 kb_sent_sec, kb_sent_percent,
177 kb_recv_sec, kb_recv_percent);
179 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
180 (mp->pref_partner == p->dest) ? '*' : ' ',
181 mp->no, p->dest->no, rtt);
193 do_shutdown (void *cls)
195 if (GNUNET_YES == logging)
196 GNUNET_ATS_TEST_logging_clean_up(l);
197 if (NULL != timeout_task)
199 GNUNET_SCHEDULER_cancel (timeout_task);
202 if (NULL != progress_task)
204 fprintf (stderr, "0\n");
205 GNUNET_SCHEDULER_cancel (progress_task);
206 progress_task = NULL;
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209 "Benchmarking done\n");
210 GNUNET_ATS_TEST_shutdown_topology ();
220 do_timeout (void *cls)
222 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
223 "Terminating with timeout\n");
226 GNUNET_SCHEDULER_shutdown ();
231 print_progress (void *cls)
235 progress_task = NULL;
238 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
241 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
248 ats_pref_task (void *cls)
250 struct BenchmarkPeer *me = cls;
254 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
255 me->no, me->pref_partner->no, me->pref_value);
256 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
257 &me->pref_partner->id,
258 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
259 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
260 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
261 &ats_pref_task, cls);
266 start_benchmark (void *cls)
271 progress_task = GNUNET_SCHEDULER_add_now (&print_progress,
274 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
275 "Topology connected, start benchmarking...\n");
277 /* Start sending test messages */
278 for (c_m = 0; c_m < num_masters; c_m++)
280 for (c_s = 0; c_s < num_slaves; c_s++)
282 GNUNET_ATS_TEST_generate_traffic_start (&mps[c_m],
283 &mps[c_m].partners[c_s],
284 GNUNET_ATS_TEST_TG_LINEAR,
287 GNUNET_TIME_UNIT_MINUTES,
288 GNUNET_TIME_UNIT_FOREVER_REL);
290 if (pref_val != GNUNET_ATS_PREFERENCE_END)
291 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task,
295 if (GNUNET_YES == logging)
296 l = GNUNET_ATS_TEST_logging_start (log_frequency,
298 num_masters, num_slaves,
304 do_benchmark (void *cls,
305 struct BenchmarkPeer *masters,
306 struct BenchmarkPeer *slaves)
310 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
312 timeout_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
315 progress_task = GNUNET_SCHEDULER_add_now (&start_benchmark,
320 static struct BenchmarkPartner *
321 find_partner (struct BenchmarkPeer *me,
322 const struct GNUNET_PeerIdentity *peer)
325 GNUNET_assert (NULL != me);
326 GNUNET_assert (NULL != peer);
328 for (c_m = 0; c_m < me->num_partners; c_m++)
330 /* Find a partner with other as destination */
331 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
332 sizeof(struct GNUNET_PeerIdentity)))
334 return &me->partners[c_m];
342 log_request_cb (void *cls,
343 const struct GNUNET_HELLO_Address *address,
345 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
346 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
347 const struct GNUNET_ATS_Properties *ats)
349 struct BenchmarkPeer *me = cls;
350 struct BenchmarkPartner *p;
353 p = find_partner (me, &address->peer);
356 /* This is not one of my partners
357 * Will happen since the peers will connect to each other due to gossiping
361 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
363 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
364 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
365 p->bandwidth_in = ntohl (bandwidth_in.value__);
366 p->bandwidth_out = ntohl (bandwidth_out.value__);
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "%s [%u] received ATS information for peers `%s'\n",
370 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
372 GNUNET_i2s (&p->dest->id));
374 GNUNET_free (peer_id);
376 GNUNET_ATS_TEST_logging_now (l);
381 * Start the performance test case
384 main (int argc, char *argv[])
392 char *prefs[] = GNUNET_ATS_PreferenceTypeString;
397 /* Determine testname
398 * perf_ats_<solver>_<transport>_<preference>[.exe]*/
400 /* Find test prefix, store in temp */
401 tmp = strstr (argv[0], TESTNAME_PREFIX);
404 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
405 return GNUNET_SYSERR;
408 /* Set tmp to end of test name prefix */
409 tmp += strlen (TESTNAME_PREFIX);
411 /* Determine solver name */
412 solver = GNUNET_strdup (tmp);
413 /* Remove .exe prefix */
414 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
417 /* Determine first '_' after solver */
418 tmp_sep = strchr (solver, '_');
421 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
423 return GNUNET_SYSERR;
426 comm_name = GNUNET_strdup (&tmp_sep[1]);
427 tmp_sep = strchr (comm_name, '_');
430 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
432 return GNUNET_SYSERR;
435 for (c = 0; c <= strlen (comm_name); c++)
436 comm_name[c] = toupper (comm_name[c]);
437 if (0 == strcmp (comm_name, "CORE"))
438 test_core = GNUNET_YES;
439 else if (0 == strcmp (comm_name, "TRANSPORT"))
440 test_core = GNUNET_NO;
443 GNUNET_free (comm_name);
444 GNUNET_free (solver);
445 return GNUNET_SYSERR;
448 pref_str = GNUNET_strdup(tmp_sep + 1);
450 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
452 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
454 for (c = 0; c <= strlen (pref_str); c++)
455 pref_str[c] = toupper (pref_str[c]);
458 if (0 != strcmp (pref_str, "NONE"))
460 for (c = 0; c < GNUNET_ATS_PREFERENCE_END; c++)
462 if (0 == strcmp (pref_str, prefs[c]))
471 /* abuse terminator to indicate no pref */
472 pref_val = GNUNET_ATS_PREFERENCE_END;
476 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
478 GNUNET_free(pref_str);
479 GNUNET_free (comm_name);
483 for (c = 0; c < (argc - 1); c++)
485 if (0 == strcmp (argv[c], "-d"))
490 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
491 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
495 perf_duration = BENCHMARK_DURATION;
497 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
499 for (c = 0; c < (argc - 1); c++)
501 if (0 == strcmp (argv[c], "-s"))
506 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
507 && (num_slaves >= 1))
508 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
510 num_slaves = DEFAULT_SLAVES_NUM;
513 num_slaves = DEFAULT_SLAVES_NUM;
515 for (c = 0; c < (argc - 1); c++)
517 if (0 == strcmp (argv[c], "-m"))
522 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
523 && (num_masters >= 2))
524 fprintf (stderr, "Starting %u master peers\n", num_masters);
526 num_masters = DEFAULT_MASTERS_NUM;
529 num_masters = DEFAULT_MASTERS_NUM;
532 for (c = 0; c < argc; c++)
534 if (0 == strcmp (argv[c], "-l"))
535 logging = GNUNET_YES;
538 if (GNUNET_YES == logging)
540 for (c = 0; c < (argc - 1); c++)
542 if (0 == strcmp (argv[c], "-f"))
547 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
548 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
552 log_frequency = LOGGING_FREQUENCY;
554 fprintf (stderr, "Using log frequency %llu ms\n",
555 (unsigned long long) (log_frequency.rel_value_us) / (1000));
558 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
560 if (num_slaves < num_masters)
563 "Number of master peers is lower than slaves! exit...\n");
564 GNUNET_free(test_name);
566 GNUNET_free(pref_str);
567 GNUNET_free (comm_name);
568 return GNUNET_SYSERR;
574 GNUNET_ATS_TEST_create_topology ("perf-ats",
586 /* end of file perf_ats.c */