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
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_transport_service.h"
27 #include "gnunet_scheduler_lib.h"
29 #include "transport_ats.h"
31 #define VERBOSE GNUNET_NO
34 #define MEASUREMENTS 5
36 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
38 #define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
42 static int peers_left;
44 static int failed_peers;
46 static int measurement_started = GNUNET_NO;
47 static char * config_file;
49 static struct GNUNET_TESTING_PeerGroup *pg;
51 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
52 static GNUNET_SCHEDULER_TaskIdentifier stats_task;
53 static GNUNET_SCHEDULER_TaskIdentifier send_task;
54 struct GNUNET_TESTING_Daemon * master_deamon;
55 struct GNUNET_TESTING_Daemon * ping_deamon;
57 struct GNUNET_STATISTICS_Handle * stats;
71 struct GNUNET_MessageHeader header;
78 static int c_unmodified;
79 static int c_modified;
83 static int force_q_updates;
84 static int force_rebuild;
86 static int machine_parsable;
88 static struct TEST_result results_new [MEASUREMENTS+1];
89 static struct TEST_result results_modified [MEASUREMENTS+1];
90 static struct TEST_result results_unmodified[MEASUREMENTS+1];
91 static struct TEST_result current;
93 static struct GNUNET_STATISTICS_GetHandle * s_solution;
94 static struct GNUNET_STATISTICS_GetHandle * s_time;
95 static struct GNUNET_STATISTICS_GetHandle * s_peers;
96 static struct GNUNET_STATISTICS_GetHandle * s_mechs;
97 static struct GNUNET_STATISTICS_GetHandle * s_duration;
98 static struct GNUNET_STATISTICS_GetHandle * s_invalid;
99 static struct GNUNET_STATISTICS_GetHandle * s_state;
101 struct GNUNET_TRANSPORT_TransmitHandle * t;
102 struct GNUNET_TRANSPORT_Handle * th;
105 * Check whether peers successfully shut down.
108 shutdown_callback (void *cls, const char *emsg)
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114 "Shutdown of peers failed!\n");
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 "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()
204 //GNUNET_asprintf(&output, "p,%i,m,%i,",peers, MEASUREMENTS, results_modified[0].mechs,
207 for (c=0; c<c_new;c++)
209 average[0] += (double) results_new[c].duration;
214 for (c=0; c<c_new;c++)
216 stddev[0] += (results_new[c].duration - average[0]) *
217 (results_new[c].duration - average[0]);
220 stddev[0] = sqrt (stddev[0]);
221 if (!machine_parsable)
223 "new, %i measurements, average: %f stddev: %f\n",
224 c_new, average[0], stddev[0]);
227 for (c=0; c<c_modified;c++)
229 average[1] += (double) results_modified[c].duration;
231 average[1] /= c_modified;
234 for (c=0; c<c_modified;c++)
236 stddev[1] += (results_modified[c].duration - average[1]) *
237 (results_modified[c].duration - average[1]);
239 stddev[1] /= c_modified;
240 stddev[1] = sqrt (stddev[1]);
241 if (!machine_parsable)
243 "modified, %i measurements, average: %f stddev: %f\n",
244 c_modified, average[1], stddev[1]);
247 for (c=0; c<c_unmodified;c++)
249 average[2] += (double) results_unmodified[c].duration;
251 average[2] /= c_unmodified;
253 for (c=0; c<c_unmodified;c++)
255 stddev[2] += (results_unmodified[c].duration - average[2]) *
256 (results_unmodified[c].duration - average[2]);
258 stddev[2] /= c_unmodified;
259 stddev[2] = sqrt (stddev[2]);
261 if (!machine_parsable)
263 "unmodified, %i measurements, average: %f stddev: %f\n",
264 c_unmodified, average[2], stddev[2]);
266 if (machine_parsable)
268 "peers,%i,mechs,%llu,"
272 peers-1, (unsigned long long) results_unmodified[0].mechs,
273 c_new, average[0], stddev[0],
274 c_modified, average[1], stddev[1],
275 c_unmodified, average[2], stddev[2]);
282 const char *subsystem,
287 static int printed = GNUNET_NO;
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s = %llu\n", name ,value);
291 if (0 == strcmp (name,"ATS invalid solutions"))
293 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
295 GNUNET_SCHEDULER_cancel(stats_task);
296 stats_task = GNUNET_SCHEDULER_NO_TASK;
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"MLP produced invalid %llu result(s)!\n",
301 return GNUNET_SYSERR;
304 if (0 == strcmp (name,"ATS solution"))
309 if (0 == strcmp (name,"ATS peers"))
314 if (0 == strcmp (name,"ATS mechanisms"))
319 if (0 == strcmp (name,"ATS duration"))
323 if (0 == strcmp (name,"ATS timestamp"))
327 if (0 == strcmp (name,"ATS state"))
332 if ( (measurement_started == GNUNET_NO) &&
333 (0 == strcmp (name, "ATS peers")) &&
336 measurement_started = GNUNET_YES;
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "All %llu peers connected\n",
342 if (! machine_parsable)
343 fprintf(stderr, "%i", count);
347 if (measurement_started == GNUNET_YES)
349 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
350 if (0 == strcmp (name,"ATS timestamp"))
352 if (current.timestamp == 0)
355 current.timestamp = value;
357 if (current.timestamp == value)
359 printed = GNUNET_YES;
361 if (current.timestamp != value)
363 if (current.state == ATS_NEW)
365 if (c_new < MEASUREMENTS)
367 results_new[c_new] = current;
372 force_rebuild = GNUNET_NO;
373 force_q_updates = GNUNET_NO;
374 send_msg = GNUNET_NO;
377 if (current.state == ATS_UNMODIFIED)
379 if (c_unmodified < MEASUREMENTS)
381 results_unmodified[c_unmodified] = current;
386 if (current.state == ATS_QUALITY_UPDATED)
388 if (c_modified < MEASUREMENTS)
390 results_modified[c_modified] = current;
395 force_q_updates = GNUNET_NO;
396 force_rebuild = GNUNET_YES;
402 "(new: %i / modified: %i / unmodified: %i) of %i \n",
403 c_new, c_modified, c_unmodified , MEASUREMENTS);
405 if ((c_modified >= MEASUREMENTS) &&
406 (c_new >= MEASUREMENTS) &&
407 (c_unmodified >= MEASUREMENTS))
410 if (!machine_parsable)
411 fprintf(stdout, "\n");
413 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
415 GNUNET_SCHEDULER_cancel(stats_task);
416 stats_task = GNUNET_SCHEDULER_NO_TASK;
418 evaluate_measurements();
419 return GNUNET_SYSERR;
423 current.timestamp = value;
425 if (! machine_parsable)
426 fprintf(stderr, "..%i", count);
432 if (0 == strcmp (name,"ATS solution"))
434 current.solution = value;
435 if (printed == GNUNET_NO)
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
440 if (0 == strcmp (name,"ATS peers"))
442 current.peers = value;
443 if (printed == GNUNET_NO)
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
448 if (0 == strcmp (name,"ATS mechanisms"))
450 current.mechs = value;
451 if (printed == GNUNET_NO)
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
456 if (0 == strcmp (name,"ATS duration"))
458 current.duration = value;
459 if (printed == GNUNET_NO)
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
463 if (0 == strcmp (name,"ATS state"))
465 current.state = value;
467 if (value == ATS_NEW)
469 if (value == ATS_COST_UPDATED)
471 if (value == ATS_QUALITY_UPDATED)
473 if (value == ATS_QUALITY_COST_UPDATED)
475 if (value == ATS_UNMODIFIED)
477 if (printed == GNUNET_NO)
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "[%i] ATS state: %s\n", count, cont);
487 stats_get_task (void *cls,
488 const struct GNUNET_SCHEDULER_TaskContext *tc)
490 stats_task = GNUNET_SCHEDULER_NO_TASK;
491 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
494 s_time = GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp",
495 TIMEOUT, NULL, &stats_cb, NULL);
496 s_solution = GNUNET_STATISTICS_get (stats, "transport", "ATS solution",
497 TIMEOUT, NULL, &stats_cb, NULL);
498 s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration",
499 TIMEOUT, NULL, &stats_cb, NULL);
500 s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers",
501 TIMEOUT, NULL, &stats_cb, NULL);
502 s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms",
503 TIMEOUT, NULL, &stats_cb, NULL);
504 s_invalid = GNUNET_STATISTICS_get (stats, "transport", "ATS invalid solutions",
505 TIMEOUT, NULL, &stats_cb, NULL);
506 s_state = GNUNET_STATISTICS_get (stats, "transport", "ATS state",
507 TIMEOUT, NULL, &stats_cb, NULL);
509 stats_task = GNUNET_SCHEDULER_add_delayed(
510 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100),
518 const struct GNUNET_SCHEDULER_TaskContext *tc)
520 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
521 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
524 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
533 shutdown_task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay, NULL);
537 send_dummy_data_task (void *cls, size_t size, void *buf)
539 int s = sizeof (struct TestMessage);
540 struct TestMessage hdr;
542 hdr.header.size = htons (s);
543 hdr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ATS);
548 memcpy (buf,&hdr, s);
549 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sent bytes: %i of %i\n", s, s);
556 send_task_f (void *cls,
557 const struct GNUNET_SCHEDULER_TaskContext *tc)
559 send_task = GNUNET_SCHEDULER_NO_TASK;
560 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
565 GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
569 if (send_msg == GNUNET_YES)
570 t = GNUNET_TRANSPORT_notify_transmit_ready(th,
572 sizeof (struct TestMessage), 0,
574 &send_dummy_data_task, NULL);
575 send_task = GNUNET_SCHEDULER_add_delayed(
576 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,1000),
583 notify_connect (void *cls,
584 const struct GNUNET_PeerIdentity *peer,
585 const struct GNUNET_TRANSPORT_ATS_Information *ats,
588 send_task = GNUNET_SCHEDULER_add_now(&send_task_f, NULL);
592 notify_disconnect (void *cls,
593 const struct GNUNET_PeerIdentity *peer)
595 if (GNUNET_SCHEDULER_NO_TASK != send_task)
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Disconnect event before transmission request could be scheduled!\n");
599 GNUNET_SCHEDULER_cancel (send_task);
600 send_task = GNUNET_SCHEDULER_NO_TASK;
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Disconnect event before transmission request could be completed!\n");
606 GNUNET_TRANSPORT_notify_transmit_ready_cancel (t);
612 daemon_connect_cb(void *cls,
613 const struct GNUNET_PeerIdentity *first,
614 const struct GNUNET_PeerIdentity *second,
616 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
617 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
618 struct GNUNET_TESTING_Daemon *first_daemon,
619 struct GNUNET_TESTING_Daemon *second_daemon,
622 char * firstc = strdup(GNUNET_i2s(first));
623 char * secondc = strdup(GNUNET_i2s(second));
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Connected peers `%s'<->`%s' (%i/%i)\n",
627 firstc, secondc, connected, peers-1);
629 GNUNET_free(secondc);
631 if ( ( (first_daemon == ping_deamon) ||
632 (second_daemon == ping_deamon) ) &&
633 (master_deamon != NULL) &&
634 (ping_deamon != NULL) )
636 th = GNUNET_TRANSPORT_connect (ping_deamon->cfg,
641 force_q_updates = GNUNET_YES;
642 send_msg = GNUNET_YES;
649 daemon_start_cb (void *cls,
650 const struct GNUNET_PeerIdentity *id,
651 const struct GNUNET_CONFIGURATION_Handle *cfg,
652 struct GNUNET_TESTING_Daemon *d, const char *emsg)
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Start callback called with error (too long starting peers), aborting test!\n");
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Error from testing: `%s'\n");
661 if (failed_peers == peers_left)
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Too many peers failed, ending test!\n");
672 if (master_deamon == NULL)
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Master peer `%s' '%s'\n",
676 GNUNET_i2s(id), d->cfgfile);
678 stats = GNUNET_STATISTICS_create("transport", master_deamon->cfg);
679 GNUNET_assert (stats != NULL);
680 stats_task = GNUNET_SCHEDULER_add_now(&stats_get_task, NULL);
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Connecting peer `%s'\n",
686 GNUNET_i2s(id), GNUNET_i2s(&master_deamon->id));
687 GNUNET_TESTING_daemons_connect(d,
698 if (ping_deamon == NULL)
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Ping peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 "All peers started successfully!\n");
710 else if (failed_peers == peers_left)
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713 "Too many peers failed, ending test!\n");
723 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
726 measurement_started = GNUNET_NO;
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", peers);
731 pg = GNUNET_TESTING_daemons_start (cfg,
732 peers_left, /* Total number of peers */
733 peers_left, /* Number of outstanding connections */
734 peers_left, /* Number of parallel ssh connections, or peers being started at once */
737 &daemon_start_cb, NULL, NULL, NULL, NULL);
738 GNUNET_assert (pg != NULL);
745 char *const argv[] = { "test-testing",
753 struct GNUNET_GETOPT_CommandLineOption options[] = {
754 GNUNET_GETOPT_OPTION_END
756 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
757 argv, "test-transport-ats", "nohelp",
764 main (int argc, char *argv[])
768 GNUNET_log_setup ("test-transport-ats-multiple-peers",
776 #if !HAVE_LIBGLPK && !HAVE_GLPK
777 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
778 "GLPK not installed, exiting testcase\n");
782 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
783 machine_parsable = GNUNET_NO;
785 config_file = "test_transport_ats_4addr.conf";
790 for (c=0; c<argc; c++)
793 if ((strcmp(argv[c], "-p") == 0) && c < (argc-1))
795 peers = atoi(argv[c+1]);
798 /* set machine parsable */
799 if (strcmp(argv[c], "-m") == 0)
801 machine_parsable = GNUNET_YES;
803 /* set config file */
804 if ((strcmp(argv[c], "-c") == 0) && c < (argc-1))
806 config_file = argv[c+1];
815 * Still need to remove the base testing directory here,
816 * because group starts will create subdirectories under this
817 * main dir. However, we no longer need to sleep, as the
818 * shutdown sequence won't return until everything is cleaned
821 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
825 /* end of test_transport_ats_multiple_peers.c*/