aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_ats.c
blob: 41d3a94437bfbb16d0df7aed26bd50d146089242 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/
/**
 * @file testing/test_transport_ats.c
 * @brief testcase for ats functionality
 */
#include "platform.h"
#include "gnunet_testing_lib.h"
#include "gnunet_scheduler_lib.h"
#include "gauger.h"

#define VERBOSE GNUNET_NO

#define NUM_PEERS 11
#define MEASUREMENTS 5

#define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)


static int ok;

static int peers_left;

static int failed_peers;

static int measurement_started;

static struct GNUNET_TESTING_PeerGroup *pg;

static  GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
static  GNUNET_SCHEDULER_TaskIdentifier stats_task;
struct GNUNET_TESTING_Daemon * master_deamon;

struct GNUNET_STATISTICS_Handle * stats;

struct TEST_result
{
	uint64_t timestamp;
	uint64_t duration;
	uint64_t mechs;
	uint64_t peers;
	uint64_t solution;
};

static int r_index;
//static int measurements;
static int peers;
static struct TEST_result results[MEASUREMENTS];

struct GNUNET_STATISTICS_GetHandle * s_solution;
struct GNUNET_STATISTICS_GetHandle * s_time;
struct GNUNET_STATISTICS_GetHandle * s_peers;
struct GNUNET_STATISTICS_GetHandle * s_mechs;
struct GNUNET_STATISTICS_GetHandle * s_duration;

/**
 * Check whether peers successfully shut down.
 */
void
shutdown_callback (void *cls, const char *emsg)
{
  if (emsg != NULL)
    {
#if VERBOSE
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
#endif
      if (ok == 0)
        ok = 666;
    }
  else
    {
#if VERBOSE
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "All peers successfully shut down!\n");
      	if (stats != NULL)
      		GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
      	stats = NULL;
#endif
    }
}

static void shutdown_peers()
{
	if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
	{
		GNUNET_SCHEDULER_cancel(shutdown_task);
		shutdown_task = GNUNET_SCHEDULER_NO_TASK;
	}
	if (stats_task != GNUNET_SCHEDULER_NO_TASK)
	{
		GNUNET_SCHEDULER_cancel(stats_task);
		stats_task = GNUNET_SCHEDULER_NO_TASK;
	}

	if (s_time != NULL)
	{
		GNUNET_STATISTICS_get_cancel(s_time);
		s_time = NULL;
	}
	if (s_peers != NULL)
	{
		GNUNET_STATISTICS_get_cancel(s_peers);
		s_peers = NULL;
	}
	if (s_mechs != NULL)
	{
		GNUNET_STATISTICS_get_cancel(s_mechs);
		s_mechs = NULL;
	}
	if (s_solution != NULL)
	{
		GNUNET_STATISTICS_get_cancel(s_solution);
		s_solution = NULL;
	}
	if (s_duration != NULL)
	{
		GNUNET_STATISTICS_get_cancel(s_duration);
		s_duration = NULL;
	}


    GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
}

static void evaluate_measurements()
{
	int c;
	char * output = NULL;
	char * temp;
	double average;
	double stddev;
	double measure = MEASUREMENTS;
	for (c=0; c<MEASUREMENTS;c++)
	{
		average += (double) results[c].duration;
		GNUNET_asprintf(&temp, "%sm%i,%llu,%llu,%llu,%llu,", (output==NULL) ? "" : output, c, results[c].peers, results[c].mechs, results[c].duration, results[c].solution);
		GNUNET_free_non_null (output);
		output = temp;
	}
	average /= measure;

	for (c=0; c<MEASUREMENTS;c++)
	{
		stddev += (results[c].duration - average) * (results[c].duration - average);
	}
	stddev /= measure;
	stddev = sqrt (stddev);

	GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"%savg,%f,stddev,%f\n",output,average,stddev);
	/* only log benchmark time for 10 peers */
	if (results[MEASUREMENTS-1].peers == (10))
	 	{
			GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Send data to gauger\n");
	 		GAUGER ("TRANSPORT", "ATS execution time 10 peers", (int) average , "ms");
	 	}
	shutdown_peers();
}

int stats_cb (void *cls,
			   const char *subsystem,
			   const char *name,
			   uint64_t value,
			   int is_persistent)
{
	if (0 == strcmp (name,"ATS solution"))
	{
		s_solution = NULL;
	}

	if (0 == strcmp (name,"ATS peers"))
	{
		s_peers = NULL;
	}

	if (0 == strcmp (name,"ATS mechanisms"))
	{
		s_mechs = NULL;
	}

	if (0 == strcmp (name,"ATS duration"))
	{
		s_duration = NULL;
	}
	if (0 == strcmp (name,"ATS timestamp"))
	{
		s_time = NULL;
	}

    if ((measurement_started == GNUNET_NO) && (0 == strcmp (name, "ATS peers")) && (value == peers-1))
    {
		measurement_started = GNUNET_YES;
		r_index = 0;
		GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All %llu peers connected\n", value);
    }
    if (measurement_started == GNUNET_YES)
    {
		// GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
		if (0 == strcmp (name,"ATS timestamp"))
		{
			if (results[r_index].timestamp == 0)
				results[r_index].timestamp = value;
			if (results[r_index].timestamp != value)
			{
				r_index++;
				fprintf(stderr, "(%i/%i)", r_index, MEASUREMENTS);
				if (r_index >= MEASUREMENTS)
				{
					fprintf(stderr, "\n");
					if (stats_task != GNUNET_SCHEDULER_NO_TASK)
					{
						GNUNET_SCHEDULER_cancel(stats_task);
						stats_task = GNUNET_SCHEDULER_NO_TASK;
					}
					evaluate_measurements();
					return GNUNET_NO;
				}
				fprintf(stderr, "..");

				results[r_index].timestamp = value;
			}
			//GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS solution: %s %llu \n", r_index, name, value);
		}

		if (0 == strcmp (name,"ATS solution"))
		{
			results[r_index].solution = value;
			//GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS solution: %s %llu \n", r_index, name, value);
		}

		if (0 == strcmp (name,"ATS peers"))
		{
			results[r_index].peers = value;
			// GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS peers: %s %llu \n", r_index, name, value);
		}

		if (0 == strcmp (name,"ATS mechanisms"))
		{
			results[r_index].mechs = value;
			//GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS mechanisms: %s %llu \n", r_index, name, value);
		}

		if (0 == strcmp (name,"ATS duration"))
		{
			results[r_index].duration = value;
			// GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS duration: %s %llu \n", r_index, name, value);
		}
    }
    return GNUNET_OK;
}


void
stats_get_task (void *cls,
			  const struct GNUNET_SCHEDULER_TaskContext *tc)
{
	stats_task = GNUNET_SCHEDULER_NO_TASK;
	if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
	    return;

	s_time = GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp", TIMEOUT, NULL, &stats_cb, NULL);
	s_solution = GNUNET_STATISTICS_get (stats, "transport", "ATS solution", TIMEOUT, NULL, &stats_cb, NULL);
	s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration", TIMEOUT, NULL, &stats_cb, NULL);
	s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers", TIMEOUT, NULL, &stats_cb, NULL);
	s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms", TIMEOUT, NULL, &stats_cb, NULL);

	stats_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250), &stats_get_task, NULL);
}

void
delay (void *cls,
			  const struct GNUNET_SCHEDULER_TaskContext *tc)
{
	shutdown_task = GNUNET_SCHEDULER_NO_TASK;
	if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
	    return;

#if VERBOSE
	GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Delay over\n");
#endif
	shutdown_peers ();
}

static void connect_peers()
{
    shutdown_task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay, NULL);

}

void daemon_connect_cb(void *cls,
						const struct GNUNET_PeerIdentity *first,
						const struct GNUNET_PeerIdentity *second,
						uint32_t distance,
						const struct GNUNET_CONFIGURATION_Handle *first_cfg,
						const struct GNUNET_CONFIGURATION_Handle *second_cfg,
						struct GNUNET_TESTING_Daemon *first_daemon,
						struct GNUNET_TESTING_Daemon *second_daemon,
						const char *emsg)
{
	  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peers `%s'<->`%s': `%s' \n", GNUNET_i2s(first), GNUNET_i2s(second), (emsg==NULL) ? "OK" : emsg);
}

void cont_cb (void *cls, int success)
{
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "STATS cont_cb: %i\n", success);
}

static void
daemon_start_cb (void *cls,
       const struct GNUNET_PeerIdentity *id,
       const struct GNUNET_CONFIGURATION_Handle *cfg,
       struct GNUNET_TESTING_Daemon *d, const char *emsg)
{
  if (id == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Start callback called with error (too long starting peers), aborting test!\n");
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
      failed_peers++;
      if (failed_peers == peers_left)
        {
          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                      "Too many peers failed, ending test!\n");
          ok = 1;
      	shutdown_peers ();
        }
      return;
    }
  peers_left--;

  if (master_deamon == NULL)
  {
	  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Master peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);

	  master_deamon = d;
	  stats = GNUNET_STATISTICS_create("transport", master_deamon->cfg);
	  GNUNET_assert (stats != NULL);
	  stats_task = GNUNET_SCHEDULER_add_now(&stats_get_task, NULL);
  }
  else
  {
	  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer `%s'\n", GNUNET_i2s(id), GNUNET_i2s(&master_deamon->id));
	  GNUNET_TESTING_daemons_connect(d, master_deamon, TIMEOUT, 0, GNUNET_YES,&daemon_connect_cb, NULL);
  }

  if (peers_left == 0)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "All peers started successfully!\n");
      connect_peers();
      ok = 0;
    }
  else if (failed_peers == peers_left)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Too many peers failed, ending test!\n");
      GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
      ok = 1;
    }
}


static void
run (void *cls,
     char *const *args,
     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  ok = 1;
  measurement_started = GNUNET_NO;
#if VERBOSE
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", peers);
#endif
  peers_left = peers;
  pg = GNUNET_TESTING_daemons_start (cfg,
                                     peers_left, /* Total number of peers */
                                     peers_left, /* Number of outstanding connections */
                                     peers_left, /* Number of parallel ssh connections, or peers being started at once */
                                     TIMEOUT,
                                     NULL, NULL,
                                     &daemon_start_cb, NULL, NULL, NULL, NULL);
  GNUNET_assert (pg != NULL);
}

static int
check ()
{
  char *const argv[] = { "test-testing",
    "-c",
    "test_transport_ats.conf",
#if VERBOSE
    "-L", "DEBUG",
#endif
    NULL
  };
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
                      argv, "test-testing-group", "nohelp",
                      options, &run, &ok);
  return ok;
}

int
main (int argc, char *argv[])
{
  int ret;

  GNUNET_log_setup ("test-transport-ats",
#if VERBOSE
                    "DEBUG",
#else
                    "WARNING",
#endif
                    NULL);

  peers = NUM_PEERS;
  if (argc >= 2)
  {
	  peers = atoi(argv[1]);
	  peers++;
	  if(peers <1)
		  peers = NUM_PEERS;
  }
  ret = check ();
  /**
   * Still need to remove the base testing directory here,
   * because group starts will create subdirectories under this
   * main dir.  However, we no longer need to sleep, as the
   * shutdown sequence won't return until everything is cleaned
   * up.
   */
  GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
  return ret;
}

/* end of test_transport_ats.c*/