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"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_transport_service.h"
33 #include "gnunet_transport_ats.h"
35 #define VERBOSE GNUNET_NO
38 #define MEASUREMENTS 5
40 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
42 #define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
46 static int peers_left;
48 static int failed_peers;
50 static int measurement_started = GNUNET_NO;
51 static char * config_file;
53 static struct GNUNET_TESTING_PeerGroup *pg;
55 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
56 static GNUNET_SCHEDULER_TaskIdentifier stats_task;
57 static GNUNET_SCHEDULER_TaskIdentifier send_task;
58 struct GNUNET_TESTING_Daemon * master_deamon;
59 struct GNUNET_TESTING_Daemon * ping_deamon;
61 struct GNUNET_STATISTICS_Handle * stats;
75 struct GNUNET_MessageHeader header;
82 static int c_unmodified;
83 static int c_modified;
87 static int force_q_updates;
88 static int force_rebuild;
90 static int machine_parsable;
92 static struct TEST_result results_new [MEASUREMENTS+1];
93 static struct TEST_result results_modified [MEASUREMENTS+1];
94 static struct TEST_result results_unmodified[MEASUREMENTS+1];
95 static struct TEST_result current;
97 static struct GNUNET_STATISTICS_GetHandle * s_solution;
98 static struct GNUNET_STATISTICS_GetHandle * s_time;
99 static struct GNUNET_STATISTICS_GetHandle * s_peers;
100 static struct GNUNET_STATISTICS_GetHandle * s_mechs;
101 static struct GNUNET_STATISTICS_GetHandle * s_duration;
102 static struct GNUNET_STATISTICS_GetHandle * s_invalid;
103 static struct GNUNET_STATISTICS_GetHandle * s_state;
105 struct GNUNET_TRANSPORT_TransmitHandle * t;
106 struct GNUNET_TRANSPORT_Handle * th;
109 * Check whether peers successfully shut down.
112 shutdown_callback (void *cls, const char *emsg)
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118 "Shutdown of peers failed!\n");
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "All peers successfully shut down!\n");
129 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
138 if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
140 GNUNET_SCHEDULER_cancel(shutdown_task);
141 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
143 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
145 GNUNET_SCHEDULER_cancel(stats_task);
146 stats_task = GNUNET_SCHEDULER_NO_TASK;
148 if (send_task != GNUNET_SCHEDULER_NO_TASK)
150 GNUNET_SCHEDULER_cancel(send_task);
151 send_task = GNUNET_SCHEDULER_NO_TASK;
156 GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
159 GNUNET_TRANSPORT_disconnect(th);
162 GNUNET_STATISTICS_get_cancel(s_time);
167 GNUNET_STATISTICS_get_cancel(s_peers);
172 GNUNET_STATISTICS_get_cancel(s_mechs);
175 if (s_solution != NULL)
177 GNUNET_STATISTICS_get_cancel(s_solution);
180 if (s_duration != NULL)
182 GNUNET_STATISTICS_get_cancel(s_duration);
185 if (s_invalid != NULL)
187 GNUNET_STATISTICS_get_cancel(s_invalid);
192 GNUNET_STATISTICS_get_cancel(s_state);
195 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
199 evaluate_measurements()
208 //GNUNET_asprintf(&output, "p,%i,m,%i,",peers, MEASUREMENTS, results_modified[0].mechs,
211 for (c=0; c<c_new;c++)
213 average[0] += (double) results_new[c].duration;
218 for (c=0; c<c_new;c++)
220 stddev[0] += (results_new[c].duration - average[0]) *
221 (results_new[c].duration - average[0]);
224 stddev[0] = sqrt (stddev[0]);
225 if (!machine_parsable)
227 "new, %i measurements, average: %f stddev: %f\n",
228 c_new, average[0], stddev[0]);
231 for (c=0; c<c_modified;c++)
233 average[1] += (double) results_modified[c].duration;
235 average[1] /= c_modified;
238 for (c=0; c<c_modified;c++)
240 stddev[1] += (results_modified[c].duration - average[1]) *
241 (results_modified[c].duration - average[1]);
243 stddev[1] /= c_modified;
244 stddev[1] = sqrt (stddev[1]);
245 if (!machine_parsable)
247 "modified, %i measurements, average: %f stddev: %f\n",
248 c_modified, average[1], stddev[1]);
251 for (c=0; c<c_unmodified;c++)
253 average[2] += (double) results_unmodified[c].duration;
255 average[2] /= c_unmodified;
257 for (c=0; c<c_unmodified;c++)
259 stddev[2] += (results_unmodified[c].duration - average[2]) *
260 (results_unmodified[c].duration - average[2]);
262 stddev[2] /= c_unmodified;
263 stddev[2] = sqrt (stddev[2]);
265 if (!machine_parsable)
267 "unmodified, %i measurements, average: %f stddev: %f\n",
268 c_unmodified, average[2], stddev[2]);
270 if (machine_parsable)
272 "peers,%i,mechs,%llu,"
276 peers-1, (unsigned long long) results_unmodified[0].mechs,
277 c_new, average[0], stddev[0],
278 c_modified, average[1], stddev[1],
279 c_unmodified, average[2], stddev[2]);
286 const char *subsystem,
291 static int printed = GNUNET_NO;
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s = %llu\n", name ,value);
295 if (0 == strcmp (name,"ATS invalid solutions"))
297 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
299 GNUNET_SCHEDULER_cancel(stats_task);
300 stats_task = GNUNET_SCHEDULER_NO_TASK;
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"MLP produced invalid %llu result(s)!\n",
305 return GNUNET_SYSERR;
308 if (0 == strcmp (name,"ATS solution"))
313 if (0 == strcmp (name,"ATS peers"))
318 if (0 == strcmp (name,"ATS mechanisms"))
323 if (0 == strcmp (name,"ATS duration"))
327 if (0 == strcmp (name,"ATS timestamp"))
331 if (0 == strcmp (name,"ATS state"))
336 if ( (measurement_started == GNUNET_NO) &&
337 (0 == strcmp (name, "ATS peers")) &&
340 measurement_started = GNUNET_YES;
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "All %llu peers connected\n",
346 if (! machine_parsable)
347 fprintf(stderr, "%i", count);
351 if (measurement_started == GNUNET_YES)
353 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
354 if (0 == strcmp (name,"ATS timestamp"))
356 if (current.timestamp == 0)
359 current.timestamp = value;
361 if (current.timestamp == value)
363 printed = GNUNET_YES;
365 if (current.timestamp != value)
367 if (current.state == ATS_NEW)
369 if (c_new < MEASUREMENTS)
371 results_new[c_new] = current;
376 force_rebuild = GNUNET_NO;
377 force_q_updates = GNUNET_NO;
378 send_msg = GNUNET_NO;
381 if (current.state == ATS_UNMODIFIED)
383 if (c_unmodified < MEASUREMENTS)
385 results_unmodified[c_unmodified] = current;
390 if (current.state == ATS_QUALITY_UPDATED)
392 if (c_modified < MEASUREMENTS)
394 results_modified[c_modified] = current;
399 force_q_updates = GNUNET_NO;
400 force_rebuild = GNUNET_YES;
406 "(new: %i / modified: %i / unmodified: %i) of %i \n",
407 c_new, c_modified, c_unmodified , MEASUREMENTS);
409 if ((c_modified >= MEASUREMENTS) &&
410 (c_new >= MEASUREMENTS) &&
411 (c_unmodified >= MEASUREMENTS))
414 if (!machine_parsable)
415 fprintf(stdout, "\n");
417 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
419 GNUNET_SCHEDULER_cancel(stats_task);
420 stats_task = GNUNET_SCHEDULER_NO_TASK;
422 evaluate_measurements();
423 return GNUNET_SYSERR;
427 current.timestamp = value;
429 if (! machine_parsable)
430 fprintf(stderr, "..%i", count);
436 if (0 == strcmp (name,"ATS solution"))
438 current.solution = value;
439 if (printed == GNUNET_NO)
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
444 if (0 == strcmp (name,"ATS peers"))
446 current.peers = value;
447 if (printed == GNUNET_NO)
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
452 if (0 == strcmp (name,"ATS mechanisms"))
454 current.mechs = value;
455 if (printed == GNUNET_NO)
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
460 if (0 == strcmp (name,"ATS duration"))
462 current.duration = value;
463 if (printed == GNUNET_NO)
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
467 if (0 == strcmp (name,"ATS state"))
469 current.state = value;
476 case ATS_COST_UPDATED:
479 case ATS_QUALITY_UPDATED:
482 case ATS_QUALITY_COST_UPDATED:
490 cont = "<undefined>";
493 if (printed == GNUNET_NO)
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "[%i] ATS state: %s\n",
505 stats_get_task (void *cls,
506 const struct GNUNET_SCHEDULER_TaskContext *tc)
508 stats_task = GNUNET_SCHEDULER_NO_TASK;
509 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
512 s_time = GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp",
513 TIMEOUT, NULL, &stats_cb, NULL);
514 s_solution = GNUNET_STATISTICS_get (stats, "transport", "ATS solution",
515 TIMEOUT, NULL, &stats_cb, NULL);
516 s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration",
517 TIMEOUT, NULL, &stats_cb, NULL);
518 s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers",
519 TIMEOUT, NULL, &stats_cb, NULL);
520 s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms",
521 TIMEOUT, NULL, &stats_cb, NULL);
522 s_invalid = GNUNET_STATISTICS_get (stats, "transport", "ATS invalid solutions",
523 TIMEOUT, NULL, &stats_cb, NULL);
524 s_state = GNUNET_STATISTICS_get (stats, "transport", "ATS state",
525 TIMEOUT, NULL, &stats_cb, NULL);
527 stats_task = GNUNET_SCHEDULER_add_delayed(
528 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100),
536 const struct GNUNET_SCHEDULER_TaskContext *tc)
538 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
539 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
551 shutdown_task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay, NULL);
555 /* To make compiler happy */
558 struct ATS_quality_metric * q = qm;
560 struct ATS_ressource * r = ressources;
565 send_dummy_data_task (void *cls, size_t size, void *buf)
567 int s = sizeof (struct TestMessage);
568 struct TestMessage hdr;
570 hdr.header.size = htons (s);
571 hdr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ATS);
574 else if (force_q_updates)
578 memcpy (buf, &hdr, s);
579 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sent bytes: %i of %i\n", s, s);
586 send_task_f (void *cls,
587 const struct GNUNET_SCHEDULER_TaskContext *tc)
589 send_task = GNUNET_SCHEDULER_NO_TASK;
590 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
595 GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
599 if (send_msg == GNUNET_YES)
600 t = GNUNET_TRANSPORT_notify_transmit_ready(th,
602 sizeof (struct TestMessage), 0,
604 &send_dummy_data_task, NULL);
605 send_task = GNUNET_SCHEDULER_add_delayed(
606 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,1000),
613 notify_connect (void *cls,
614 const struct GNUNET_PeerIdentity *peer,
615 const struct GNUNET_TRANSPORT_ATS_Information *ats,
618 send_task = GNUNET_SCHEDULER_add_now(&send_task_f, NULL);
622 notify_disconnect (void *cls,
623 const struct GNUNET_PeerIdentity *peer)
625 if (GNUNET_SCHEDULER_NO_TASK != send_task)
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Disconnect event before transmission request could be scheduled!\n");
629 GNUNET_SCHEDULER_cancel (send_task);
630 send_task = GNUNET_SCHEDULER_NO_TASK;
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Disconnect event before transmission request could be completed!\n");
636 GNUNET_TRANSPORT_notify_transmit_ready_cancel (t);
642 daemon_connect_cb(void *cls,
643 const struct GNUNET_PeerIdentity *first,
644 const struct GNUNET_PeerIdentity *second,
646 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
647 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
648 struct GNUNET_TESTING_Daemon *first_daemon,
649 struct GNUNET_TESTING_Daemon *second_daemon,
652 char * firstc = strdup(GNUNET_i2s(first));
653 char * secondc = strdup(GNUNET_i2s(second));
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Connected peers `%s'<->`%s' (%i/%i)\n",
657 firstc, secondc, connected, peers-1);
659 GNUNET_free(secondc);
661 if ( ( (first_daemon == ping_deamon) ||
662 (second_daemon == ping_deamon) ) &&
663 (master_deamon != NULL) &&
664 (ping_deamon != NULL) )
666 th = GNUNET_TRANSPORT_connect (ping_deamon->cfg,
671 force_q_updates = GNUNET_YES;
672 send_msg = GNUNET_YES;
679 daemon_start_cb (void *cls,
680 const struct GNUNET_PeerIdentity *id,
681 const struct GNUNET_CONFIGURATION_Handle *cfg,
682 struct GNUNET_TESTING_Daemon *d, const char *emsg)
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Start callback called with error (too long starting peers), aborting test!\n");
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Error from testing: `%s'\n");
691 if (failed_peers == peers_left)
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Too many peers failed, ending test!\n");
702 if (master_deamon == NULL)
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Master peer `%s' '%s'\n",
706 GNUNET_i2s(id), d->cfgfile);
708 stats = GNUNET_STATISTICS_create("transport", master_deamon->cfg);
709 GNUNET_assert (stats != NULL);
710 stats_task = GNUNET_SCHEDULER_add_now(&stats_get_task, NULL);
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 "Connecting peer `%s'\n",
716 GNUNET_i2s(id), GNUNET_i2s(&master_deamon->id));
717 GNUNET_TESTING_daemons_connect(d,
728 if (ping_deamon == NULL)
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Ping peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "All peers started successfully!\n");
740 else if (failed_peers == peers_left)
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Too many peers failed, ending test!\n");
753 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
756 measurement_started = GNUNET_NO;
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", peers);
761 pg = GNUNET_TESTING_daemons_start (cfg,
762 peers_left, /* Total number of peers */
763 peers_left, /* Number of outstanding connections */
764 peers_left, /* Number of parallel ssh connections, or peers being started at once */
767 &daemon_start_cb, NULL, NULL, NULL, NULL);
768 GNUNET_assert (pg != NULL);
775 char *const argv[] = { "test-testing",
783 struct GNUNET_GETOPT_CommandLineOption options[] = {
784 GNUNET_GETOPT_OPTION_END
786 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
787 argv, "test_transport_ats_multiple_peers", "nohelp",
795 main (int argc, char *argv[])
799 GNUNET_log_setup ("test-transport-ats-multiple-peers",
808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
809 "GLPK not installed, exiting testcase\n");
812 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
813 machine_parsable = GNUNET_NO;
815 config_file = "test_transport_ats_4addr.conf";
820 for (c=0; c<argc; c++)
823 if ((strcmp(argv[c], "-p") == 0) && c < (argc-1))
825 peers = atoi(argv[c+1]);
828 /* set machine parsable */
829 if (strcmp(argv[c], "-m") == 0)
831 machine_parsable = GNUNET_YES;
833 /* set config file */
834 if ((strcmp(argv[c], "-c") == 0) && c < (argc-1))
836 config_file = argv[c+1];
843 * Still need to remove the base testing directory here,
844 * because group starts will create subdirectories under this
845 * main dir. However, we no longer need to sleep, as the
846 * shutdown sequence won't return until everything is cleaned
849 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
854 /* end of test_transport_ats_multiple_peers.c*/