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_scheduler_lib.h"
28 #define VERBOSE GNUNET_YES
31 #define MEASUREMENTS 5
33 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
39 static int peers_left;
41 static int failed_peers;
43 static int measurement_started;
45 static struct GNUNET_TESTING_PeerGroup *pg;
47 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
48 static GNUNET_SCHEDULER_TaskIdentifier stats_task;
49 struct GNUNET_TESTING_Daemon * master_deamon;
51 struct GNUNET_STATISTICS_Handle * stats;
63 //static int measurements;
65 static struct TEST_result results[MEASUREMENTS];
67 struct GNUNET_STATISTICS_GetHandle * s_solution;
68 struct GNUNET_STATISTICS_GetHandle * s_time;
69 struct GNUNET_STATISTICS_GetHandle * s_peers;
70 struct GNUNET_STATISTICS_GetHandle * s_mechs;
71 struct GNUNET_STATISTICS_GetHandle * s_duration;
74 * Check whether peers successfully shut down.
77 shutdown_callback (void *cls, const char *emsg)
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "All peers successfully shut down!\n");
93 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
99 static void shutdown_peers()
101 if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel(shutdown_task);
104 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
106 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
108 GNUNET_SCHEDULER_cancel(stats_task);
109 stats_task = GNUNET_SCHEDULER_NO_TASK;
114 GNUNET_STATISTICS_get_cancel(s_time);
119 GNUNET_STATISTICS_get_cancel(s_peers);
124 GNUNET_STATISTICS_get_cancel(s_mechs);
127 if (s_solution != NULL)
129 GNUNET_STATISTICS_get_cancel(s_solution);
132 if (s_duration != NULL)
134 GNUNET_STATISTICS_get_cancel(s_duration);
139 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
142 static void evaluate_measurements()
145 char * output = NULL;
147 for (c=0; c<MEASUREMENTS;c++)
149 GNUNET_asprintf(&temp, "%s ,%i,%llu,%llu,%llu,%llu,", (output==NULL) ? "" : output, c, results[c].peers, results[c].mechs, results[c].duration, results[c].solution);
150 GNUNET_free_non_null (output);
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"%s\n",output);
159 int stats_cb (void *cls,
160 const char *subsystem,
165 if (0 == strcmp (name,"ATS solution"))
170 if (0 == strcmp (name,"ATS peers"))
175 if (0 == strcmp (name,"ATS mechanisms"))
180 if (0 == strcmp (name,"ATS duration"))
184 if (0 == strcmp (name,"ATS timestamp"))
189 if ((measurement_started == GNUNET_NO) && (0 == strcmp (name, "ATS peers")) && (value == peers-1))
191 measurement_started = GNUNET_YES;
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All %llu peers connected\n", value);
195 if (measurement_started == GNUNET_YES)
197 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
198 if (0 == strcmp (name,"ATS timestamp"))
200 if (results[r_index].timestamp == 0)
201 results[r_index].timestamp = value;
202 if (results[r_index].timestamp != value)
205 if (r_index >= MEASUREMENTS)
207 if (stats_task != GNUNET_SCHEDULER_NO_TASK)
209 GNUNET_SCHEDULER_cancel(stats_task);
210 stats_task = GNUNET_SCHEDULER_NO_TASK;
212 evaluate_measurements();
215 results[r_index].timestamp = value;
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "[%i] ATS solution: %s %llu \n", r_index, name, value);
222 if (0 == strcmp (name,"ATS solution"))
224 results[r_index].solution = value;
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "[%i] ATS solution: %s %llu \n", r_index, name, value);
229 if (0 == strcmp (name,"ATS peers"))
231 results[r_index].peers = value;
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "[%i] ATS peers: %s %llu \n", r_index, name, value);
236 if (0 == strcmp (name,"ATS mechanisms"))
238 results[r_index].mechs = value;
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS mechanisms: %s %llu \n", r_index, name, value);
242 if (0 == strcmp (name,"ATS duration"))
244 results[r_index].duration = value;
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS duration: %s %llu \n", r_index, name, value);
253 stats_get_task (void *cls,
254 const struct GNUNET_SCHEDULER_TaskContext *tc)
256 stats_task = GNUNET_SCHEDULER_NO_TASK;
257 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
260 s_time = GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp", TIMEOUT, NULL, &stats_cb, NULL);
261 s_solution = GNUNET_STATISTICS_get (stats, "transport", "ATS solution", TIMEOUT, NULL, &stats_cb, NULL);
262 s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration", TIMEOUT, NULL, &stats_cb, NULL);
263 s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers", TIMEOUT, NULL, &stats_cb, NULL);
264 s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms", TIMEOUT, NULL, &stats_cb, NULL);
266 stats_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250), &stats_get_task, NULL);
271 const struct GNUNET_SCHEDULER_TaskContext *tc)
273 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
274 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Delay over\n");
283 static void connect_peers()
285 shutdown_task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay, NULL);
289 void daemon_connect_cb(void *cls,
290 const struct GNUNET_PeerIdentity *first,
291 const struct GNUNET_PeerIdentity *second,
293 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
294 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
295 struct GNUNET_TESTING_Daemon *first_daemon,
296 struct GNUNET_TESTING_Daemon *second_daemon,
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peers `%s'<->`%s': `%s' \n", GNUNET_i2s(first), GNUNET_i2s(second), (emsg==NULL) ? "OK" : emsg);
302 void cont_cb (void *cls, int success)
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305 "STATS cont_cb: %i\n", success);
309 daemon_start_cb (void *cls,
310 const struct GNUNET_PeerIdentity *id,
311 const struct GNUNET_CONFIGURATION_Handle *cfg,
312 struct GNUNET_TESTING_Daemon *d, const char *emsg)
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Start callback called with error (too long starting peers), aborting test!\n");
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
320 if (failed_peers == peers_left)
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Too many peers failed, ending test!\n");
331 if (master_deamon == NULL)
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Master peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);
336 stats = GNUNET_STATISTICS_create("transport", master_deamon->cfg);
337 GNUNET_assert (stats != NULL);
338 stats_task = GNUNET_SCHEDULER_add_now(&stats_get_task, NULL);
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer `%s'\n", GNUNET_i2s(id), GNUNET_i2s(&master_deamon->id));
343 GNUNET_TESTING_daemons_connect(d, master_deamon, TIMEOUT, 0, GNUNET_YES,&daemon_connect_cb, NULL);
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "All peers started successfully!\n");
353 else if (failed_peers == peers_left)
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Too many peers failed, ending test!\n");
357 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
366 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
369 measurement_started = GNUNET_NO;
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", NUM_PEERS);
374 pg = GNUNET_TESTING_daemons_start (cfg,
375 peers_left, /* Total number of peers */
376 peers_left, /* Number of outstanding connections */
377 peers_left, /* Number of parallel ssh connections, or peers being started at once */
380 &daemon_start_cb, NULL, NULL, NULL, NULL);
381 GNUNET_assert (pg != NULL);
387 char *const argv[] = { "test-testing",
389 "test_transport_ats.conf",
395 struct GNUNET_GETOPT_CommandLineOption options[] = {
396 GNUNET_GETOPT_OPTION_END
398 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
399 argv, "test-testing-group", "nohelp",
405 main (int argc, char *argv[])
409 GNUNET_log_setup ("test-transport-ats",
420 peers = atoi(argv[1]);
427 * Still need to remove the base testing directory here,
428 * because group starts will create subdirectories under this
429 * main dir. However, we no longer need to sleep, as the
430 * shutdown sequence won't return until everything is cleaned
433 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
437 /* end of test_transport_ats.c*/