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.c
22 * @brief testcase for ats functionality
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_transport_service.h"
27 #include "gnunet_scheduler_lib.h"
30 #define VERBOSE GNUNET_NO
33 #define MEASUREMENTS 5
35 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
37 #define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
40 #define ATS_Q_UPDATED 1
41 #define ATS_C_UPDATED 2
42 #define ATS_QC_UPDATED 3
43 #define ATS_UNMODIFIED 4
47 static int peers_left;
49 static int failed_peers;
51 static int measurement_started = GNUNET_NO;
52 static char * config_file;
54 static struct GNUNET_TESTING_PeerGroup *pg;
56 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
57 static GNUNET_SCHEDULER_TaskIdentifier stats_task;
58 static GNUNET_SCHEDULER_TaskIdentifier send_task;
59 struct GNUNET_TESTING_Daemon * master_deamon;
60 struct GNUNET_TESTING_Daemon * ping_deamon;
62 struct GNUNET_STATISTICS_Handle * stats;
76 struct GNUNET_MessageHeader header;
83 static int c_unmodified;
84 static int c_modified;
88 static int force_q_updates;
89 static int force_rebuild;
91 static int machine_parsable;
93 static struct TEST_result results_new [MEASUREMENTS+1];
94 static struct TEST_result results_modified [MEASUREMENTS+1];
95 static struct TEST_result results_unmodified[MEASUREMENTS+1];
96 static struct TEST_result current;
98 static struct GNUNET_STATISTICS_GetHandle * s_solution;
99 static struct GNUNET_STATISTICS_GetHandle * s_time;
100 static struct GNUNET_STATISTICS_GetHandle * s_peers;
101 static struct GNUNET_STATISTICS_GetHandle * s_mechs;
102 static struct GNUNET_STATISTICS_GetHandle * s_duration;
103 static struct GNUNET_STATISTICS_GetHandle * s_invalid;
104 static struct GNUNET_STATISTICS_GetHandle * s_state;
106 struct GNUNET_TRANSPORT_TransmitHandle * t;
107 struct GNUNET_TRANSPORT_Handle * th;
110 * Check whether peers successfully shut down.
113 shutdown_callback (void *cls, const char *emsg)
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119 "Shutdown of peers failed!\n");
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "All peers successfully shut down!\n");
130 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
139 if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
141 GNUNET_SCHEDULER_cancel(shutdown_task);
142 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
144 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
146 GNUNET_SCHEDULER_cancel(stats_task);
147 stats_task = GNUNET_SCHEDULER_NO_TASK;
149 if (send_task != GNUNET_SCHEDULER_NO_TASK)
151 GNUNET_SCHEDULER_cancel(send_task);
152 send_task = GNUNET_SCHEDULER_NO_TASK;
157 GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
160 GNUNET_TRANSPORT_disconnect(th);
163 GNUNET_STATISTICS_get_cancel(s_time);
168 GNUNET_STATISTICS_get_cancel(s_peers);
173 GNUNET_STATISTICS_get_cancel(s_mechs);
176 if (s_solution != NULL)
178 GNUNET_STATISTICS_get_cancel(s_solution);
181 if (s_duration != NULL)
183 GNUNET_STATISTICS_get_cancel(s_duration);
186 if (s_invalid != NULL)
188 GNUNET_STATISTICS_get_cancel(s_invalid);
193 GNUNET_STATISTICS_get_cancel(s_state);
196 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
200 evaluate_measurements()
209 //GNUNET_asprintf(&output, "p,%i,m,%i,",peers, MEASUREMENTS, results_modified[0].mechs,
212 for (c=0; c<c_new;c++)
214 average[0] += (double) results_new[c].duration;
219 for (c=0; c<c_new;c++)
221 stddev[0] += (results_new[c].duration - average[0]) * (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]) * (results_modified[c].duration - average[1]);
242 stddev[1] /= c_modified;
243 stddev[1] = sqrt (stddev[1]);
244 if (!machine_parsable)
246 "modified, %i measurements, average: %f stddev: %f\n",
247 c_modified, average[1], stddev[1]);
250 for (c=0; c<c_unmodified;c++)
252 average[2] += (double) results_unmodified[c].duration;
254 average[2] /= c_unmodified;
256 for (c=0; c<c_unmodified;c++)
258 stddev[2] += (results_unmodified[c].duration - average[2]) * (results_unmodified[c].duration - average[2]);
260 stddev[2] /= c_unmodified;
261 stddev[2] = sqrt (stddev[2]);
263 if (!machine_parsable)
265 "unmodified, %i measurements, average: %f stddev: %f\n",
266 c_unmodified, average[2], stddev[2]);
268 if (machine_parsable)
270 "peers,%i,mechs,%llu,"
274 peers-1, (unsigned long long) results_unmodified[0].mechs,
275 c_new, average[0], stddev[0],
276 c_modified, average[1], stddev[1],
277 c_unmodified, average[2], stddev[2]);
284 const char *subsystem,
289 static int printed = GNUNET_NO;
290 //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", value);
300 return GNUNET_SYSERR;
303 if (0 == strcmp (name,"ATS solution"))
308 if (0 == strcmp (name,"ATS peers"))
313 if (0 == strcmp (name,"ATS mechanisms"))
318 if (0 == strcmp (name,"ATS duration"))
322 if (0 == strcmp (name,"ATS timestamp"))
326 if (0 == strcmp (name,"ATS state"))
331 if ( (measurement_started == GNUNET_NO) &&
332 (0 == strcmp (name, "ATS peers")) &&
335 measurement_started = GNUNET_YES;
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "All %llu peers connected\n",
341 if (! machine_parsable)
342 fprintf(stderr, "%i", count);
346 if (measurement_started == GNUNET_YES)
348 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
349 if (0 == strcmp (name,"ATS timestamp"))
351 if (current.timestamp == 0)
354 current.timestamp = value;
356 if (current.timestamp == value)
358 printed = GNUNET_YES;
360 if (current.timestamp != value)
362 if (current.state == ATS_NEW)
364 if (c_new < MEASUREMENTS)
366 results_new[c_new] = current;
371 force_rebuild = GNUNET_NO;
372 force_q_updates = GNUNET_NO;
373 send_msg = GNUNET_NO;
376 if (current.state == ATS_UNMODIFIED)
378 if (c_unmodified < MEASUREMENTS)
380 results_unmodified[c_unmodified] = current;
385 if (current.state == ATS_Q_UPDATED)
387 if (c_modified < MEASUREMENTS)
389 results_modified[c_modified] = current;
394 force_q_updates = GNUNET_NO;
395 force_rebuild = GNUNET_YES;
401 "(new: %i / modified: %i / unmodified: %i) of %i \n",
402 c_new, c_modified, c_unmodified , MEASUREMENTS);
404 if ((c_modified >= MEASUREMENTS) && (c_new >= MEASUREMENTS) && (c_unmodified >= MEASUREMENTS))
407 if (!machine_parsable)
408 fprintf(stdout, "\n");
410 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
412 GNUNET_SCHEDULER_cancel(stats_task);
413 stats_task = GNUNET_SCHEDULER_NO_TASK;
415 evaluate_measurements();
416 return GNUNET_SYSERR;
420 current.timestamp = value;
422 if (! machine_parsable)
423 fprintf(stderr, "..%i", count);
429 if (0 == strcmp (name,"ATS solution"))
431 current.solution = value;
432 if (printed == GNUNET_NO)
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name, value);
436 if (0 == strcmp (name,"ATS peers"))
438 current.peers = value;
439 if (printed == GNUNET_NO)
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name, value);
443 if (0 == strcmp (name,"ATS mechanisms"))
445 current.mechs = value;
446 if (printed == GNUNET_NO)
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name, value);
450 if (0 == strcmp (name,"ATS duration"))
452 current.duration = value;
453 if (printed == GNUNET_NO)
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n", count, name, value);
456 if (0 == strcmp (name,"ATS state"))
458 current.state = value;
460 if (value == ATS_NEW)
462 if (value == ATS_C_UPDATED)
464 if (value == ATS_Q_UPDATED)
466 if (value == ATS_QC_UPDATED)
468 if (value == ATS_UNMODIFIED)
470 if (printed == GNUNET_NO)
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "[%i] ATS state: %s\n", count, cont);
480 stats_get_task (void *cls,
481 const struct GNUNET_SCHEDULER_TaskContext *tc)
483 stats_task = GNUNET_SCHEDULER_NO_TASK;
484 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
487 s_time = GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp", TIMEOUT, NULL, &stats_cb, NULL);
488 s_solution = GNUNET_STATISTICS_get (stats, "transport", "ATS solution", TIMEOUT, NULL, &stats_cb, NULL);
489 s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration", TIMEOUT, NULL, &stats_cb, NULL);
490 s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers", TIMEOUT, NULL, &stats_cb, NULL);
491 s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms", TIMEOUT, NULL, &stats_cb, NULL);
492 s_invalid = GNUNET_STATISTICS_get (stats, "transport", "ATS invalid solutions", TIMEOUT, NULL, &stats_cb, NULL);
493 s_state = GNUNET_STATISTICS_get (stats, "transport", "ATS state", TIMEOUT, NULL, &stats_cb, NULL);
495 stats_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100),
496 &stats_get_task, NULL);
502 const struct GNUNET_SCHEDULER_TaskContext *tc)
504 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
505 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
517 shutdown_task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay, NULL);
521 send_dummy_data_task (void *cls, size_t size, void *buf)
523 int s = sizeof (struct TestMessage);
524 struct TestMessage hdr;
526 hdr.header.size = htons (s);
527 hdr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ATS);
532 memcpy (buf,&hdr, s);
533 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sent bytes: %i of %i\n", s, s);
540 send_task_f (void *cls,
541 const struct GNUNET_SCHEDULER_TaskContext *tc)
543 send_task = GNUNET_SCHEDULER_NO_TASK;
544 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
549 GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
552 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sent bytes: %i to %s\n", size, GNUNET_i2s(&master_deamon->id));
553 if (send_msg == GNUNET_YES)
554 t = GNUNET_TRANSPORT_notify_transmit_ready(th,
556 sizeof (struct TestMessage), 0,
558 &send_dummy_data_task, NULL);
559 send_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,1000),
564 notify_connect (void *cls,
565 const struct GNUNET_PeerIdentity *peer,
566 const struct GNUNET_TRANSPORT_ATS_Information *ats,
569 send_task = GNUNET_SCHEDULER_add_now(&send_task_f, NULL);
573 notify_disconnect (void *cls,
574 const struct GNUNET_PeerIdentity *peer)
576 if (GNUNET_SCHEDULER_NO_TASK != send_task)
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "Disconnect event before transmission request could be scheduled!\n");
580 GNUNET_SCHEDULER_cancel (send_task);
581 send_task = GNUNET_SCHEDULER_NO_TASK;
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Disconnect event before transmission request could be completed!\n");
587 GNUNET_TRANSPORT_notify_transmit_ready_cancel (t);
593 daemon_connect_cb(void *cls,
594 const struct GNUNET_PeerIdentity *first,
595 const struct GNUNET_PeerIdentity *second,
597 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
598 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
599 struct GNUNET_TESTING_Daemon *first_daemon,
600 struct GNUNET_TESTING_Daemon *second_daemon,
603 char * firstc = strdup(GNUNET_i2s(first));
604 char * secondc = strdup(GNUNET_i2s(second));
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Connected peers `%s'<->`%s' (%i/%i)\n",
608 firstc, secondc, connected, peers-1);
610 GNUNET_free(secondc);
612 if ( ( (first_daemon == ping_deamon) ||
613 (second_daemon == ping_deamon) ) &&
614 (master_deamon != NULL) &&
615 (ping_deamon != NULL) )
617 th = GNUNET_TRANSPORT_connect (ping_deamon->cfg,
622 force_q_updates = GNUNET_YES;
623 send_msg = GNUNET_YES;
630 daemon_start_cb (void *cls,
631 const struct GNUNET_PeerIdentity *id,
632 const struct GNUNET_CONFIGURATION_Handle *cfg,
633 struct GNUNET_TESTING_Daemon *d, const char *emsg)
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Start callback called with error (too long starting peers), aborting test!\n");
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Error from testing: `%s'\n");
642 if (failed_peers == peers_left)
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Too many peers failed, ending test!\n");
653 if (master_deamon == NULL)
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Master peer `%s' '%s'\n",
657 GNUNET_i2s(id), d->cfgfile);
659 stats = GNUNET_STATISTICS_create("transport", master_deamon->cfg);
660 GNUNET_assert (stats != NULL);
661 stats_task = GNUNET_SCHEDULER_add_now(&stats_get_task, NULL);
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Connecting peer `%s'\n",
667 GNUNET_i2s(id), GNUNET_i2s(&master_deamon->id));
668 GNUNET_TESTING_daemons_connect(d, master_deamon, TIMEOUT, 0, GNUNET_YES,&daemon_connect_cb, NULL);
673 if (ping_deamon == NULL)
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "Ping peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 "All peers started successfully!\n");
685 else if (failed_peers == peers_left)
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Too many peers failed, ending test!\n");
698 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
701 measurement_started = GNUNET_NO;
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", peers);
706 pg = GNUNET_TESTING_daemons_start (cfg,
707 peers_left, /* Total number of peers */
708 peers_left, /* Number of outstanding connections */
709 peers_left, /* Number of parallel ssh connections, or peers being started at once */
712 &daemon_start_cb, NULL, NULL, NULL, NULL);
713 GNUNET_assert (pg != NULL);
720 char *const argv[] = { "test-testing",
728 struct GNUNET_GETOPT_CommandLineOption options[] = {
729 GNUNET_GETOPT_OPTION_END
731 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
732 argv, "test-transport-ats", "nohelp",
739 main (int argc, char *argv[])
743 GNUNET_log_setup ("test-transport-ats",
752 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
753 "GLPK not installed, exiting testcase\n");
756 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
757 machine_parsable = GNUNET_NO;
759 config_file = "test_transport_ats_4addr.conf";
764 for (c=0; c<argc; c++)
767 if ((strcmp(argv[c], "-p") == 0) && c < (argc-1))
769 peers = atoi(argv[c+1]);
772 /* set machine parsable */
773 if (strcmp(argv[c], "-m") == 0)
775 machine_parsable = GNUNET_YES;
777 /* set config file */
778 if ((strcmp(argv[c], "-c") == 0) && c < (argc-1))
780 config_file = argv[c+1];
789 * Still need to remove the base testing directory here,
790 * because group starts will create subdirectories under this
791 * main dir. However, we no longer need to sleep, as the
792 * shutdown sequence won't return until everything is cleaned
795 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
799 /* end of test_transport_ats.c*/