2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file testing/test_transport_ats_multiple_peers.c
22 * @brief testcase for ats functionality by starting multiple peers
26 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_transport_service.h"
31 #include "gnunet-service-transport_ats.h"
33 #define VERBOSE GNUNET_EXTRA_LOGGING
36 #define MEASUREMENTS 5
38 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
40 #define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 static int peers_left;
46 static int failed_peers;
48 static int measurement_started = GNUNET_NO;
49 static char *config_file;
51 static struct GNUNET_TESTING_PeerGroup *pg;
53 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
54 static GNUNET_SCHEDULER_TaskIdentifier stats_task;
55 static GNUNET_SCHEDULER_TaskIdentifier send_task;
56 struct GNUNET_TESTING_Daemon *master_deamon;
57 struct GNUNET_TESTING_Daemon *ping_deamon;
59 struct GNUNET_STATISTICS_Handle *stats;
73 struct GNUNET_MessageHeader header;
80 static int c_unmodified;
81 static int c_modified;
85 static int force_q_updates;
86 static int force_rebuild;
88 static int machine_parsable;
90 static struct TEST_result results_new[MEASUREMENTS + 1];
91 static struct TEST_result results_modified[MEASUREMENTS + 1];
92 static struct TEST_result results_unmodified[MEASUREMENTS + 1];
93 static struct TEST_result current;
95 static struct GNUNET_STATISTICS_GetHandle *s_solution;
96 static struct GNUNET_STATISTICS_GetHandle *s_time;
97 static struct GNUNET_STATISTICS_GetHandle *s_peers;
98 static struct GNUNET_STATISTICS_GetHandle *s_mechs;
99 static struct GNUNET_STATISTICS_GetHandle *s_duration;
100 static struct GNUNET_STATISTICS_GetHandle *s_invalid;
101 static struct GNUNET_STATISTICS_GetHandle *s_state;
103 struct GNUNET_TRANSPORT_TransmitHandle *t;
104 struct GNUNET_TRANSPORT_Handle *th;
107 * Check whether peers successfully shut down.
110 shutdown_callback (void *cls, const char *emsg)
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
125 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
134 if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
136 GNUNET_SCHEDULER_cancel (shutdown_task);
137 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
139 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
141 GNUNET_SCHEDULER_cancel (stats_task);
142 stats_task = GNUNET_SCHEDULER_NO_TASK;
144 if (send_task != GNUNET_SCHEDULER_NO_TASK)
146 GNUNET_SCHEDULER_cancel (send_task);
147 send_task = GNUNET_SCHEDULER_NO_TASK;
152 GNUNET_TRANSPORT_notify_transmit_ready_cancel (t);
155 GNUNET_TRANSPORT_disconnect (th);
158 GNUNET_STATISTICS_get_cancel (s_time);
163 GNUNET_STATISTICS_get_cancel (s_peers);
168 GNUNET_STATISTICS_get_cancel (s_mechs);
171 if (s_solution != NULL)
173 GNUNET_STATISTICS_get_cancel (s_solution);
176 if (s_duration != NULL)
178 GNUNET_STATISTICS_get_cancel (s_duration);
181 if (s_invalid != NULL)
183 GNUNET_STATISTICS_get_cancel (s_invalid);
188 GNUNET_STATISTICS_get_cancel (s_state);
191 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
195 evaluate_measurements ()
206 //GNUNET_asprintf(&output, "p,%i,m,%i,",peers, MEASUREMENTS, results_modified[0].mechs,
209 for (c = 0; c < c_new; c++)
211 average[0] += (double) results_new[c].duration;
216 for (c = 0; c < c_new; c++)
219 (results_new[c].duration - average[0]) * (results_new[c].duration -
223 stddev[0] = sqrt (stddev[0]);
224 if (!machine_parsable)
225 fprintf (stderr, "new, %i measurements, average: %f stddev: %f\n", c_new,
226 average[0], stddev[0]);
229 for (c = 0; c < c_modified; c++)
231 average[1] += (double) results_modified[c].duration;
233 average[1] /= c_modified;
236 for (c = 0; c < c_modified; c++)
239 (results_modified[c].duration -
240 average[1]) * (results_modified[c].duration - average[1]);
242 stddev[1] /= c_modified;
243 stddev[1] = sqrt (stddev[1]);
244 if (!machine_parsable)
245 fprintf (stderr, "modified, %i measurements, average: %f stddev: %f\n",
246 c_modified, average[1], stddev[1]);
249 for (c = 0; c < c_unmodified; c++)
251 average[2] += (double) results_unmodified[c].duration;
253 average[2] /= c_unmodified;
255 for (c = 0; c < c_unmodified; c++)
258 (results_unmodified[c].duration -
259 average[2]) * (results_unmodified[c].duration - average[2]);
261 stddev[2] /= c_unmodified;
262 stddev[2] = sqrt (stddev[2]);
264 if (!machine_parsable)
265 fprintf (stderr, "unmodified, %i measurements, average: %f stddev: %f\n",
266 c_unmodified, average[2], stddev[2]);
268 if (machine_parsable)
270 "peers,%i,mechs,%llu," "new,%i,%f,%f," "mod,%i,%f,%f,"
271 "unmod,%i,%f,%f\n", peers - 1,
272 (unsigned long long) results_unmodified[0].mechs, c_new,
273 average[0], stddev[0], c_modified, average[1], stddev[1],
274 c_unmodified, average[2], stddev[2]);
280 stats_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
283 static int printed = GNUNET_NO;
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s = %llu\n", name, value);
288 if (0 == strcmp (name, "ATS invalid solutions"))
290 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
292 GNUNET_SCHEDULER_cancel (stats_task);
293 stats_task = GNUNET_SCHEDULER_NO_TASK;
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "MLP produced invalid %llu result(s)!\n", value);
298 return GNUNET_SYSERR;
301 if (0 == strcmp (name, "ATS solution"))
306 if (0 == strcmp (name, "ATS peers"))
311 if (0 == strcmp (name, "ATS mechanisms"))
316 if (0 == strcmp (name, "ATS duration"))
320 if (0 == strcmp (name, "ATS timestamp"))
324 if (0 == strcmp (name, "ATS state"))
329 if ((measurement_started == GNUNET_NO) && (0 == strcmp (name, "ATS peers")) &&
330 (value == peers - 1))
332 measurement_started = GNUNET_YES;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All %llu peers connected\n", value);
336 if (!machine_parsable)
337 fprintf (stderr, "%i", count);
341 if (measurement_started == GNUNET_YES)
343 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
344 if (0 == strcmp (name, "ATS timestamp"))
346 if (current.timestamp == 0)
349 current.timestamp = value;
351 if (current.timestamp == value)
353 printed = GNUNET_YES;
355 if (current.timestamp != value)
357 if (current.state == ATS_NEW)
359 if (c_new < MEASUREMENTS)
361 results_new[c_new] = current;
366 force_rebuild = GNUNET_NO;
367 force_q_updates = GNUNET_NO;
368 send_msg = GNUNET_NO;
371 if (current.state == ATS_UNMODIFIED)
373 if (c_unmodified < MEASUREMENTS)
375 results_unmodified[c_unmodified] = current;
380 if (current.state == ATS_QUALITY_UPDATED)
382 if (c_modified < MEASUREMENTS)
384 results_modified[c_modified] = current;
389 force_q_updates = GNUNET_NO;
390 force_rebuild = GNUNET_YES;
395 fprintf (stderr, "(new: %i / modified: %i / unmodified: %i) of %i \n",
396 c_new, c_modified, c_unmodified, MEASUREMENTS);
398 if ((c_modified >= MEASUREMENTS) && (c_new >= MEASUREMENTS) &&
399 (c_unmodified >= MEASUREMENTS))
402 if (!machine_parsable)
403 fprintf (stdout, "\n");
405 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
407 GNUNET_SCHEDULER_cancel (stats_task);
408 stats_task = GNUNET_SCHEDULER_NO_TASK;
410 evaluate_measurements ();
411 return GNUNET_SYSERR;
415 current.timestamp = value;
417 if (!machine_parsable)
418 fprintf (stderr, "..%i", count);
424 if (0 == strcmp (name, "ATS solution"))
426 current.solution = value;
427 if (printed == GNUNET_NO)
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name,
432 if (0 == strcmp (name, "ATS peers"))
434 current.peers = value;
435 if (printed == GNUNET_NO)
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name,
440 if (0 == strcmp (name, "ATS mechanisms"))
442 current.mechs = value;
443 if (printed == GNUNET_NO)
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name,
448 if (0 == strcmp (name, "ATS duration"))
450 current.duration = value;
451 if (printed == GNUNET_NO)
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name,
455 if (0 == strcmp (name, "ATS state"))
457 current.state = value;
465 case ATS_COST_UPDATED:
468 case ATS_QUALITY_UPDATED:
471 case ATS_QUALITY_COST_UPDATED:
479 cont = "<undefined>";
482 if (printed == GNUNET_NO)
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS state: %s\n", count,
492 stats_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
494 stats_task = GNUNET_SCHEDULER_NO_TASK;
495 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
499 GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp", TIMEOUT, NULL,
502 GNUNET_STATISTICS_get (stats, "transport", "ATS solution", TIMEOUT, NULL,
505 GNUNET_STATISTICS_get (stats, "transport", "ATS duration", TIMEOUT, NULL,
508 GNUNET_STATISTICS_get (stats, "transport", "ATS peers", TIMEOUT, NULL,
511 GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms", TIMEOUT,
512 NULL, &stats_cb, NULL);
514 GNUNET_STATISTICS_get (stats, "transport", "ATS invalid solutions",
515 TIMEOUT, NULL, &stats_cb, NULL);
517 GNUNET_STATISTICS_get (stats, "transport", "ATS state", TIMEOUT, NULL,
521 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
522 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
523 &stats_get_task, NULL);
528 delay (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
530 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
531 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Delay over\n");
542 shutdown_task = GNUNET_SCHEDULER_add_delayed (DELAY, &delay, NULL);
546 /* To make compiler happy */
550 struct ATS_quality_metric *q = qm;
554 struct ATS_ressource *r = ressources;
561 send_dummy_data_task (void *cls, size_t size, void *buf)
563 int s = sizeof (struct TestMessage);
564 struct TestMessage hdr;
566 hdr.header.size = htons (s);
567 hdr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ATS);
570 else if (force_q_updates)
574 memcpy (buf, &hdr, s);
575 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sent bytes: %i of %i\n", s, s);
582 send_task_f (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
584 send_task = GNUNET_SCHEDULER_NO_TASK;
585 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
590 GNUNET_TRANSPORT_notify_transmit_ready_cancel (t);
594 if (send_msg == GNUNET_YES)
595 t = GNUNET_TRANSPORT_notify_transmit_ready (th, &master_deamon->id,
596 sizeof (struct TestMessage), 0,
598 &send_dummy_data_task, NULL);
600 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
601 (GNUNET_TIME_UNIT_MILLISECONDS, 1000),
607 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
608 const struct GNUNET_TRANSPORT_ATS_Information *ats,
611 send_task = GNUNET_SCHEDULER_add_now (&send_task_f, NULL);
615 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
617 if (GNUNET_SCHEDULER_NO_TASK != send_task)
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620 "Disconnect event before transmission request could be scheduled!\n");
621 GNUNET_SCHEDULER_cancel (send_task);
622 send_task = GNUNET_SCHEDULER_NO_TASK;
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Disconnect event before transmission request could be completed!\n");
628 GNUNET_TRANSPORT_notify_transmit_ready_cancel (t);
634 daemon_connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
635 const struct GNUNET_PeerIdentity *second, uint32_t distance,
636 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
637 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
638 struct GNUNET_TESTING_Daemon *first_daemon,
639 struct GNUNET_TESTING_Daemon *second_daemon,
642 char *firstc = strdup (GNUNET_i2s (first));
643 char *secondc = strdup (GNUNET_i2s (second));
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peers `%s'<->`%s' (%i/%i)\n",
647 firstc, secondc, connected, peers - 1);
648 GNUNET_free (firstc);
649 GNUNET_free (secondc);
651 if (((first_daemon == ping_deamon) || (second_daemon == ping_deamon)) &&
652 (master_deamon != NULL) && (ping_deamon != NULL))
654 th = GNUNET_TRANSPORT_connect (ping_deamon->cfg, &ping_deamon->id, NULL,
655 NULL, ¬ify_connect, ¬ify_disconnect);
656 force_q_updates = GNUNET_YES;
657 send_msg = GNUNET_YES;
664 daemon_start_cb (void *cls, const struct GNUNET_PeerIdentity *id,
665 const struct GNUNET_CONFIGURATION_Handle *cfg,
666 struct GNUNET_TESTING_Daemon *d, const char *emsg)
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "Start callback called with error (too long starting peers), aborting test!\n");
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
674 if (failed_peers == peers_left)
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "Too many peers failed, ending test!\n");
685 if (master_deamon == NULL)
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Master peer `%s' '%s'\n",
688 GNUNET_i2s (id), d->cfgfile);
690 stats = GNUNET_STATISTICS_create ("transport", master_deamon->cfg);
691 GNUNET_assert (stats != NULL);
692 stats_task = GNUNET_SCHEDULER_add_now (&stats_get_task, NULL);
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer `%s'\n",
697 GNUNET_i2s (id), GNUNET_i2s (&master_deamon->id));
698 GNUNET_TESTING_daemons_connect (d, master_deamon, TIMEOUT, 0, GNUNET_YES,
699 &daemon_connect_cb, NULL);
704 if (ping_deamon == NULL)
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ping peer `%s' '%s'\n",
708 GNUNET_i2s (id), d->cfgfile);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers started successfully!\n");
715 else if (failed_peers == peers_left)
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Too many peers failed, ending test!\n");
726 run (void *cls, char *const *args, const char *cfgfile,
727 const struct GNUNET_CONFIGURATION_Handle *cfg)
730 measurement_started = GNUNET_NO;
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", peers);
735 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, /* Total number of peers */
736 peers_left, /* Number of outstanding connections */
737 peers_left, /* Number of parallel ssh connections, or peers being started at once */
738 TIMEOUT, NULL, NULL, &daemon_start_cb,
739 NULL, NULL, NULL, NULL);
740 GNUNET_assert (pg != NULL);
747 char *const argv[] = { "test-testing",
755 struct GNUNET_GETOPT_CommandLineOption options[] = {
756 GNUNET_GETOPT_OPTION_END
758 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
759 "test_transport_ats_multiple_peers", "nohelp", options,
765 main (int argc, char *argv[])
769 GNUNET_log_setup ("test-transport-ats-multiple-peers",
777 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
778 machine_parsable = GNUNET_NO;
780 config_file = "test_transport_ats_4addr.conf";
786 for (c = 0; c < argc; c++)
789 if ((strcmp (argv[c], "-p") == 0) && c < (argc - 1))
791 peers = atoi (argv[c + 1]);
794 /* set machine parsable */
795 if (strcmp (argv[c], "-m") == 0)
797 machine_parsable = GNUNET_YES;
799 /* set config file */
800 if ((strcmp (argv[c], "-c") == 0) && c < (argc - 1))
802 config_file = argv[c + 1];
809 * Still need to remove the base testing directory here,
810 * because group starts will create subdirectories under this
811 * main dir. However, we no longer need to sleep, as the
812 * shutdown sequence won't return until everything is cleaned
815 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
819 /* end of test_transport_ats_multiple_peers.c*/