improvements
[oweals/gnunet.git] / src / ats-tests / perf_ats.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20 /**
21  * @file ats/perf_ats.c
22  * @brief ats benchmark: start peers and modify preferences, monitor change over time
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31 #include "ats-testing.h"
32
33
34 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35 #define TEST_ATS_PREFRENCE_START 1.0
36 #define TEST_ATS_PREFRENCE_DELTA 1.0
37
38 #define TEST_MESSAGE_TYPE_PING 12345
39 #define TEST_MESSAGE_TYPE_PONG 12346
40 #define TEST_MESSAGE_SIZE 1000
41 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
42
43 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
44 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
45 #define LOGGING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
46 #define TESTNAME_PREFIX "perf_ats_"
47 #define DEFAULT_SLAVES_NUM 2
48 #define DEFAULT_MASTERS_NUM 1
49 /**
50  * Shutdown task
51  */
52 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
53
54 /**
55  * Progress task
56  */
57 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
58
59 /**
60  * Test result
61  */
62 static int result;
63
64 /**
65  * Test result logging
66  */
67 static int logging;
68
69 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
70  */
71 static int test_core;
72
73 /**
74  * Solver string
75  */
76 static char *solver;
77
78 /**
79  * Preference string
80  */
81 static char *testname;
82
83 /**
84  * Preference string
85  */
86 static char *pref_str;
87
88 /**
89  * ATS preference value
90  */
91 static int pref_val;
92
93 /**
94  * Benchmark duration
95  */
96 static struct GNUNET_TIME_Relative perf_duration;
97
98 /**
99  * Logging frequency
100  */
101 static struct GNUNET_TIME_Relative log_frequency;
102
103 /**
104  * Number master peers
105  */
106 static unsigned int num_masters;
107
108 /**
109  * Array of master peers
110  */
111 static struct BenchmarkPeer *mps;
112
113 /**
114  * Number slave peers
115  */
116 static unsigned int num_slaves;
117
118 /**
119  * Array of master peers
120  */
121 static struct BenchmarkPeer *sps;
122
123 static void
124 evaluate ()
125 {
126   int c_m;
127   int c_s;
128   unsigned int duration;
129   struct BenchmarkPeer *mp;
130   struct BenchmarkPartner *p;
131
132   unsigned int kb_sent_sec;
133   double kb_sent_percent;
134   unsigned int kb_recv_sec;
135   double kb_recv_percent;
136   unsigned int rtt;
137
138   duration = (perf_duration.rel_value_us / (1000 * 1000));
139   for (c_m = 0; c_m < num_masters; c_m++)
140   {
141     mp = &mps[c_m];
142     fprintf (stderr,
143         _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
144         mp->no, mp->total_bytes_sent / 1024, duration,
145         (mp->total_bytes_sent / 1024) / duration,
146         mp->total_bytes_received / 1024, duration,
147         (mp->total_bytes_received / 1024) / duration);
148
149     for (c_s = 0; c_s < num_slaves; c_s++)
150     {
151       p = &mp->partners[c_s];
152
153       kb_sent_sec = 0;
154       kb_recv_sec = 0;
155       kb_sent_percent = 0.0;
156       kb_recv_percent = 0.0;
157       rtt = 0;
158
159       if (duration > 0)
160       {
161           kb_sent_percent = (p->bytes_sent / 1024) / duration;
162           kb_recv_percent = (p->bytes_received / 1024) / duration;
163       }
164       if (mp->total_bytes_sent > 0)
165           kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
166       if (mp->total_bytes_received > 0)
167           kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
168       if (1000 * p->messages_sent > 0)
169           rtt = p->total_app_rtt / (1000 * p->messages_sent);
170       fprintf (stderr,
171           "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
172           (mp->pref_partner == p->dest) ? '*' : ' ',
173           mp->no, p->dest->no,
174           kb_sent_sec, kb_sent_percent,
175                   kb_recv_sec, kb_recv_percent);
176
177       fprintf (stderr,
178           "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
179           (mp->pref_partner == p->dest) ? '*' : ' ',
180           mp->no, p->dest->no, rtt);
181     }
182   }
183 }
184
185 /**
186  * Shutdown nicely
187  *
188  * @param cls NULL
189  * @param tc the task context
190  */
191 static void
192 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193 {
194
195   if (GNUNET_YES == logging)
196     GNUNET_ATS_TEST_logging_stop();
197
198   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
199   if (GNUNET_SCHEDULER_NO_TASK != progress_task)
200   {
201     fprintf (stderr, "0\n");
202     GNUNET_SCHEDULER_cancel (progress_task);
203   }
204   progress_task = GNUNET_SCHEDULER_NO_TASK;
205
206   evaluate ();
207   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
208
209   GNUNET_ATS_TEST_shutdown_topology();
210 }
211
212 static size_t
213 comm_send_ready (void *cls, size_t size, void *buf)
214 {
215   static char msgbuf[TEST_MESSAGE_SIZE];
216   struct BenchmarkPartner *p = cls;
217   struct GNUNET_MessageHeader *msg;
218
219   if (GNUNET_YES == test_core)
220     p->cth = NULL;
221   else
222     p->tth = NULL;
223
224   if (NULL == buf)
225   {
226     GNUNET_break (0);
227     return 0;
228   }
229   if (size < TEST_MESSAGE_SIZE)
230   {
231     GNUNET_break (0);
232     return 0;
233   }
234
235   GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Master [%u]: Sending PING to [%u]\n",
236       p->me->no, p->dest->no);
237
238   p->messages_sent++;
239   p->bytes_sent += TEST_MESSAGE_SIZE;
240   p->me->total_messages_sent++;
241   p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
242
243   msg = (struct GNUNET_MessageHeader *) &msgbuf;
244   memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
245   msg->type = htons (TEST_MESSAGE_TYPE_PING);
246   msg->size = htons (TEST_MESSAGE_SIZE);
247   memcpy (buf, msg, TEST_MESSAGE_SIZE);
248   return TEST_MESSAGE_SIZE;
249 }
250
251 static void
252 comm_schedule_send (struct BenchmarkPartner *p)
253 {
254   p->last_message_sent = GNUNET_TIME_absolute_get();
255   if (GNUNET_YES == test_core)
256   {
257     p->cth = GNUNET_CORE_notify_transmit_ready (
258       p->me->ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &p->dest->id,
259       TEST_MESSAGE_SIZE, &comm_send_ready, p);
260   }
261   else
262   {
263     p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
264       p->me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
265       &comm_send_ready, p);
266   }
267
268 }
269
270 static void
271 print_progress ()
272 {
273   static int calls;
274   progress_task = GNUNET_SCHEDULER_NO_TASK;
275
276   fprintf (stderr, "%llu..",
277       (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
278   calls++;
279
280   progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
281       &print_progress, NULL );
282 }
283
284 static void
285 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
286 {
287   struct BenchmarkPeer *me = cls;
288
289   me->ats_task = GNUNET_SCHEDULER_NO_TASK;
290
291   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
292       me->no, me->pref_partner->no, me->pref_value);
293   GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
294       &me->pref_partner->id,
295       pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
296   me->pref_value += TEST_ATS_PREFRENCE_DELTA;
297   me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
298       &ats_pref_task, cls);
299 }
300
301 static void
302 do_benchmark (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves)
303 {
304   int c_m;
305   int c_s;
306
307   mps = masters;
308   sps = slaves;
309
310   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
311
312   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
313     GNUNET_SCHEDULER_cancel (shutdown_task);
314   shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
315       &do_shutdown, NULL );
316
317   progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
318
319   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Topology connected, start benchmarking...\n");
320
321   /* Start sending test messages */
322   for (c_m = 0; c_m < num_masters; c_m++)
323   {
324     for (c_s = 0; c_s < num_slaves; c_s++)
325       comm_schedule_send (&masters[c_m].partners[c_s]);
326     if (pref_val != GNUNET_ATS_PREFERENCE_END)
327       masters[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &masters[c_m]);
328   }
329
330   if (GNUNET_YES == logging)
331     GNUNET_ATS_TEST_logging_start (log_frequency, testname, mps, num_masters);
332 }
333
334
335 static size_t
336 comm_send_pong_ready (void *cls, size_t size, void *buf)
337 {
338   static char msgbuf[TEST_MESSAGE_SIZE];
339   struct BenchmarkPartner *p = cls;
340   struct GNUNET_MessageHeader *msg;
341
342   if (GNUNET_YES == test_core)
343     p->cth = NULL;
344   else
345     p->tth = NULL;
346
347   p->messages_sent++;
348   p->bytes_sent += TEST_MESSAGE_SIZE;
349   p->me->total_messages_sent++;
350   p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
351
352   msg = (struct GNUNET_MessageHeader *) &msgbuf;
353   memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
354   msg->type = htons (TEST_MESSAGE_TYPE_PONG);
355   msg->size = htons (TEST_MESSAGE_SIZE);
356   memcpy (buf, msg, TEST_MESSAGE_SIZE);
357
358   return TEST_MESSAGE_SIZE;
359 }
360
361 static struct BenchmarkPartner *
362 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
363 {
364   int c_m;
365   GNUNET_assert (NULL != me);
366   GNUNET_assert (NULL != peer);
367
368   for (c_m = 0; c_m < me->num_partners; c_m++)
369   {
370     /* Find a partner with other as destination */
371     if (0 == memcmp (peer, &me->partners[c_m].dest->id,
372             sizeof(struct GNUNET_PeerIdentity)))
373     {
374       return &me->partners[c_m];
375     }
376   }
377   return NULL;
378 }
379
380 static int
381 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
382     const struct GNUNET_MessageHeader *message)
383 {
384
385   struct BenchmarkPeer *me = cls;
386   struct BenchmarkPartner *p = NULL;
387
388   if (NULL == (p = find_partner(me, other)))
389   {
390     GNUNET_break(0);
391     return GNUNET_SYSERR;
392   }
393
394   GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
395       "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
396       p->dest->no);
397
398   p->messages_received++;
399   p->bytes_received += TEST_MESSAGE_SIZE;
400   p->me->total_messages_received++;
401   p->me->total_bytes_received += TEST_MESSAGE_SIZE;
402
403   if (GNUNET_YES == test_core)
404   {
405     GNUNET_assert (NULL == p->cth);
406     p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
407         GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
408         &comm_send_pong_ready, p);
409   }
410   else
411   {
412     GNUNET_assert (NULL == p->tth);
413     p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
414         TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
415         p);
416   }
417   return GNUNET_OK;
418 }
419
420 static int
421 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
422     const struct GNUNET_MessageHeader *message)
423 {
424   struct BenchmarkPeer *me = cls;
425   struct BenchmarkPartner *p = NULL;
426
427   if (NULL == (p = find_partner (me, other)))
428   {
429     GNUNET_break(0);
430     return GNUNET_SYSERR;
431   }
432
433   GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
434       "Master [%u]: Received PONG from [%u], next message\n", me->no,
435       p->dest->no);
436
437   p->messages_received++;
438   p->bytes_received += TEST_MESSAGE_SIZE;
439   p->me->total_messages_received++;
440   p->me->total_bytes_received += TEST_MESSAGE_SIZE;
441   p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
442       GNUNET_TIME_absolute_get()).rel_value_us;
443
444   comm_schedule_send (p);
445   return GNUNET_OK;
446 }
447
448
449 static void
450 test_recv_cb (void *cls,
451                       const struct GNUNET_PeerIdentity * peer,
452                       const struct GNUNET_MessageHeader * message)
453 {
454   if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
455       (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
456       TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
457   {
458     return;
459   }
460   if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
461     comm_handle_ping (cls, peer, message);
462
463   if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
464     comm_handle_pong (cls, peer, message);
465 }
466
467
468 static void
469 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
470     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
471     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
472     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
473 {
474   struct BenchmarkPeer *me = cls;
475   struct BenchmarkPartner *p;
476   int c_a;
477   int log;
478   char *peer_id;
479
480   p = find_partner (me, &address->peer);
481   if (NULL == p)
482   {
483     /* This is not one of my partners
484      * Will happen since the peers will connect to each other due to gossiping
485      */
486     return;
487   }
488   peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
489
490   log = GNUNET_NO;
491   if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
492       (p->bandwidth_out != ntohl (bandwidth_out.value__)))
493       log = GNUNET_YES;
494   p->bandwidth_in = ntohl (bandwidth_in.value__);
495   p->bandwidth_out = ntohl (bandwidth_out.value__);
496
497   for (c_a = 0; c_a < ats_count; c_a++)
498   {
499     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
500         (GNUNET_YES == p->me->master) ? "Master" : "Slave",
501         p->me->no,
502         GNUNET_i2s (&p->dest->id),
503         GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
504         ntohl(ats[c_a].value));
505     switch (ntohl (ats[c_a].type ))
506     {
507       case GNUNET_ATS_ARRAY_TERMINATOR:
508         break;
509       case GNUNET_ATS_UTILIZATION_OUT:
510         if (p->ats_utilization_up != ntohl (ats[c_a].value))
511             log = GNUNET_YES;
512         p->ats_utilization_up = ntohl (ats[c_a].value);
513
514         break;
515       case GNUNET_ATS_UTILIZATION_IN:
516         if (p->ats_utilization_down != ntohl (ats[c_a].value))
517             log = GNUNET_YES;
518         p->ats_utilization_down = ntohl (ats[c_a].value);
519         break;
520       case GNUNET_ATS_NETWORK_TYPE:
521         if (p->ats_network_type != ntohl (ats[c_a].value))
522             log = GNUNET_YES;
523         p->ats_network_type = ntohl (ats[c_a].value);
524         break;
525       case GNUNET_ATS_QUALITY_NET_DELAY:
526         if (p->ats_delay != ntohl (ats[c_a].value))
527             log = GNUNET_YES;
528         p->ats_delay = ntohl (ats[c_a].value);
529         break;
530       case GNUNET_ATS_QUALITY_NET_DISTANCE:
531         if (p->ats_distance != ntohl (ats[c_a].value))
532             log = GNUNET_YES;
533         p->ats_distance = ntohl (ats[c_a].value);
534         GNUNET_break (0);
535         break;
536       case GNUNET_ATS_COST_WAN:
537         if (p->ats_cost_wan != ntohl (ats[c_a].value))
538             log = GNUNET_YES;
539         p->ats_cost_wan = ntohl (ats[c_a].value);
540         break;
541       case GNUNET_ATS_COST_LAN:
542         if (p->ats_cost_lan != ntohl (ats[c_a].value))
543             log = GNUNET_YES;
544         p->ats_cost_lan = ntohl (ats[c_a].value);
545         break;
546       case GNUNET_ATS_COST_WLAN:
547         if (p->ats_cost_wlan != ntohl (ats[c_a].value))
548             log = GNUNET_YES;
549         p->ats_cost_wlan = ntohl (ats[c_a].value);
550         break;
551       default:
552         break;
553     }
554   }
555
556   if ((GNUNET_YES == logging) && (GNUNET_YES == log))
557     GNUNET_ATS_TEST_logging_now();
558
559   GNUNET_free(peer_id);
560 }
561
562
563 /*
564  * Start the performance test case
565  */
566 int
567 main (int argc, char *argv[])
568 {
569   char *tmp;
570   char *tmp_sep;
571   char *test_name;
572   char *conf_name;
573   char *comm_name;
574   char *dotexe;
575   char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
576   int c;
577
578   result = 0;
579
580   /* figure out testname */
581   tmp = strstr (argv[0], TESTNAME_PREFIX);
582   if (NULL == tmp)
583   {
584     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
585     return GNUNET_SYSERR;
586   }
587   tmp += strlen (TESTNAME_PREFIX);
588   solver = GNUNET_strdup (tmp);
589   if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
590     dotexe[0] = '\0';
591   tmp_sep = strchr (solver, '_');
592   if (NULL == tmp_sep)
593   {
594     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
595     GNUNET_free(solver);
596     return GNUNET_SYSERR;
597   }
598   tmp_sep[0] = '\0';
599   comm_name = GNUNET_strdup (&tmp_sep[1]);
600   tmp_sep = strchr (comm_name, '_');
601   if (NULL == tmp_sep)
602   {
603     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
604     GNUNET_free(solver);
605     return GNUNET_SYSERR;
606   }
607   tmp_sep[0] = '\0';
608   for (c = 0; c <= strlen (comm_name); c++)
609     comm_name[c] = toupper (comm_name[c]);
610   if (0 == strcmp (comm_name, "CORE"))
611     test_core = GNUNET_YES;
612   else if (0 == strcmp (comm_name, "TRANSPORT"))
613     test_core = GNUNET_NO;
614   else
615   {
616     GNUNET_free (comm_name);
617     GNUNET_free (solver);
618     return GNUNET_SYSERR;
619   }
620
621   pref_str = GNUNET_strdup(tmp_sep + 1);
622
623   GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
624       pref_str);
625   GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
626
627   for (c = 0; c <= strlen (pref_str); c++)
628     pref_str[c] = toupper (pref_str[c]);
629   pref_val = -1;
630
631   if (0 != strcmp (pref_str, "NONE"))
632   {
633     for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
634     {
635       if (0 == strcmp (pref_str, prefs[c]))
636       {
637         pref_val = c;
638         break;
639       }
640     }
641   }
642   else
643   {
644     /* abuse terminator to indicate no pref */
645     pref_val = GNUNET_ATS_PREFERENCE_END;
646   }
647   if (-1 == pref_val)
648   {
649     fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
650     GNUNET_free(solver);
651     GNUNET_free(pref_str);
652     GNUNET_free (comm_name);
653     return -1;
654   }
655
656   for (c = 0; c < (argc - 1); c++)
657   {
658     if (0 == strcmp (argv[c], "-d"))
659       break;
660   }
661   if (c < argc - 1)
662   {
663     if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
664         fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
665   }
666   else
667   {
668     perf_duration = BENCHMARK_DURATION;
669   }
670   fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
671
672   for (c = 0; c < (argc - 1); c++)
673   {
674     if (0 == strcmp (argv[c], "-s"))
675       break;
676   }
677   if (c < argc - 1)
678   {
679     if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
680         && (num_slaves >= 1))
681       fprintf (stderr, "Starting %u slave peers\n", num_slaves);
682     else
683       num_slaves = DEFAULT_SLAVES_NUM;
684   }
685   else
686     num_slaves = DEFAULT_SLAVES_NUM;
687
688   for (c = 0; c < (argc - 1); c++)
689   {
690     if (0 == strcmp (argv[c], "-m"))
691       break;
692   }
693   if (c < argc - 1)
694   {
695     if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
696         && (num_masters >= 2))
697       fprintf (stderr, "Starting %u master peers\n", num_masters);
698     else
699       num_masters = DEFAULT_MASTERS_NUM;
700   }
701   else
702     num_masters = DEFAULT_MASTERS_NUM;
703
704   logging = GNUNET_NO;
705   for (c = 0; c < argc; c++)
706   {
707     if (0 == strcmp (argv[c], "-l"))
708       logging = GNUNET_YES;
709   }
710
711   if (GNUNET_YES == logging)
712   {
713     for (c = 0; c < (argc - 1); c++)
714     {
715       if (0 == strcmp (argv[c], "-f"))
716         break;
717     }
718     if (c < argc - 1)
719     {
720       if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
721           fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
722     }
723     else
724     {
725       log_frequency = LOGGING_FREQUENCY;
726     }
727     fprintf (stderr, "Using log frequency %llu ms\n",
728         (unsigned long long) (log_frequency.rel_value_us) / (1000));
729   }
730
731   GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
732
733   if (num_slaves < num_masters)
734   {
735     fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
736     GNUNET_free(test_name);
737     GNUNET_free(solver);
738     GNUNET_free(pref_str);
739     GNUNET_free (comm_name);
740     return GNUNET_SYSERR;
741   }
742
743   /**
744    * Core message handler to use for PING/PONG messages
745    */
746   static struct GNUNET_CORE_MessageHandler handlers[] = {
747       {&comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 },
748       {&comm_handle_pong, TEST_MESSAGE_TYPE_PONG, 0 },
749       { NULL, 0, 0 } };
750
751   /**
752    * Setup the topology
753    */
754   GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
755       num_slaves, num_masters,
756       test_core,
757       &do_benchmark,
758       NULL, handlers,
759       &test_recv_cb,
760       &ats_performance_info_cb);
761
762   return result;
763 }
764
765 /* end of file perf_ats.c */