2 This file is part of GNUnet.
3 (C) 2010-2013 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 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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
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
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)
44 * Connect peers with testbed
46 struct TestbedConnectOperation
49 * The benchmarking master initiating this connection
51 struct BenchmarkPeer *master;
54 * The benchmarking slave to connect to
56 struct BenchmarkPeer *slave;
59 * Testbed operation to connect peers
61 struct GNUNET_TESTBED_Operation *connect_op;
65 * Overall state of the performance benchmark
69 /* Are we connected to ATS service of all peers: GNUNET_YES/NO */
70 int connected_ATS_service;
72 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
73 int connected_COMM_service;
75 /* Are we connected to all peers: GNUNET_YES/NO */
78 /* Are we connected to all slave peers on CORE level: GNUNET_YES/NO */
81 /* Are we connected to CORE service of all peers: GNUNET_YES/NO */
88 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
93 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
101 * Test result logging
105 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
107 static int test_core;
117 static char *testname;
122 static char *pref_str;
125 * ATS preference value
130 * Number master peers
132 static int num_masters;
135 * Array of master peers
137 static struct BenchmarkPeer *mps;
142 static int num_slaves;
144 * Array of slave peers
146 static struct BenchmarkPeer *sps;
151 static struct GNUNET_TIME_Relative perf_duration;
156 static struct BenchmarkState state;
163 unsigned int duration;
164 struct BenchmarkPeer *mp;
166 duration = (perf_duration.rel_value_us / (1000 * 1000));
167 for (c_m = 0; c_m < num_masters; c_m++)
171 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
172 mp->no, mp->total_bytes_sent / 1024, duration,
173 (mp->total_bytes_sent / 1024) / duration,
174 mp->total_bytes_received / 1024, duration,
175 (mp->total_bytes_received / 1024) / duration);
177 for (c_s = 0; c_s < num_slaves; c_s++)
180 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f \%), received %u KiB/s (%.2f \%)\n",
181 (mp->pref_partner == mp->partners[c_s].dest) ? '*' : ' ',
182 mp->no, mp->partners[c_s].dest->no,
183 (mp->partners[c_s].bytes_sent / 1024) / duration,
184 ((double) mp->partners[c_s].bytes_sent * 100) / mp->total_bytes_sent,
185 (mp->partners[c_s].bytes_received / 1024) / duration,
186 ((double) mp->partners[c_s].bytes_received * 100) / mp->total_bytes_received );
195 * @param tc the task context
198 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
204 if (GNUNET_YES == logging)
207 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
208 if (GNUNET_SCHEDULER_NO_TASK != progress_task)
210 fprintf (stderr, "0\n");
211 GNUNET_SCHEDULER_cancel (progress_task);
213 progress_task = GNUNET_SCHEDULER_NO_TASK;
216 state.benchmarking = GNUNET_NO;
217 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
219 for (c_m = 0; c_m < num_masters; c_m++)
221 if (NULL != mps[c_m].peer_id_op)
223 GNUNET_TESTBED_operation_done (mps[c_m].peer_id_op);
224 mps[c_m].peer_id_op = NULL;
227 if (GNUNET_SCHEDULER_NO_TASK != mps[c_m].ats_task)
228 GNUNET_SCHEDULER_cancel (mps[c_m].ats_task);
229 mps[c_m].ats_task = GNUNET_SCHEDULER_NO_TASK;
231 for (c_op = 0; c_op < num_slaves; c_op++)
234 if (NULL != mps[c_m].partners[c_op].cth)
236 GNUNET_CORE_notify_transmit_ready_cancel (mps[c_m].partners[c_op].cth);
237 mps[c_m].partners[c_op].cth = NULL;
239 if (NULL != mps[c_m].partners[c_op].tth)
241 GNUNET_TRANSPORT_notify_transmit_ready_cancel (mps[c_m].partners[c_op].tth);
242 mps[c_m].partners[c_op].tth = NULL;
246 if (NULL != mps[c_m].core_connect_ops[c_op].connect_op)
248 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
249 _("Failed to connect peer 0 and %u\n"), c_op);
250 GNUNET_TESTBED_operation_done (
251 mps[c_m].core_connect_ops[c_op].connect_op);
252 mps[c_m].core_connect_ops[c_op].connect_op = NULL;
257 if (NULL != mps[c_m].ats_perf_op)
259 GNUNET_TESTBED_operation_done (mps[c_m].ats_perf_op);
260 mps[c_m].ats_perf_op = NULL;
263 if (NULL != mps[c_m].comm_op)
265 GNUNET_TESTBED_operation_done (mps[c_m].comm_op);
266 mps[c_m].comm_op = NULL;
268 GNUNET_free(mps[c_m].core_connect_ops);
269 GNUNET_free(mps[c_m].partners);
270 mps[c_m].partners = NULL;
273 for (c_s = 0; c_s < num_slaves; c_s++)
275 if (NULL != sps[c_s].peer_id_op)
277 GNUNET_TESTBED_operation_done (sps[c_s].peer_id_op);
278 sps[c_s].peer_id_op = NULL;
281 for (c_op = 0; c_op < num_slaves; c_op++)
283 if (NULL != sps[c_s].partners[c_op].cth)
285 GNUNET_CORE_notify_transmit_ready_cancel (sps[c_s].partners[c_op].cth);
286 sps[c_s].partners[c_op].cth = NULL;
289 if (NULL != sps[c_s].partners[c_op].tth)
291 GNUNET_TRANSPORT_notify_transmit_ready_cancel (sps[c_s].partners[c_op].tth);
292 sps[c_s].partners[c_op].tth = NULL;
296 if (NULL != sps[c_s].ats_perf_op)
298 GNUNET_TESTBED_operation_done (sps[c_s].ats_perf_op);
299 sps[c_s].ats_perf_op = NULL;
301 if (NULL != sps[c_s].comm_op)
303 GNUNET_TESTBED_operation_done (sps[c_s].comm_op);
304 sps[c_s].comm_op = NULL;
307 GNUNET_free(sps[c_s].partners);
308 sps[c_s].partners = NULL;
311 GNUNET_SCHEDULER_shutdown ();
314 static struct BenchmarkPeer *
315 find_peer (const struct GNUNET_PeerIdentity * peer)
319 for (c_p = 0; c_p < num_masters; c_p++)
321 if (0 == memcmp (&mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
325 for (c_p = 0; c_p < num_slaves; c_p++)
327 if (0 == memcmp (&sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
334 * Controller event callback
337 * @param event the controller event
340 controller_event_cb (void *cls,
341 const struct GNUNET_TESTBED_EventInformation *event)
343 //struct BenchmarkPeer *p = cls;
346 case GNUNET_TESTBED_ET_CONNECT:
348 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
353 GNUNET_SCHEDULER_cancel (shutdown_task);
354 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
359 comm_send_ready (void *cls, size_t size, void *buf)
361 static char msgbuf[TEST_MESSAGE_SIZE];
362 struct BenchmarkPartner *p = cls;
363 struct GNUNET_MessageHeader *msg;
365 if (GNUNET_YES == test_core)
370 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
371 p->me->no, p->dest->no);
374 p->bytes_sent += TEST_MESSAGE_SIZE;
375 p->me->total_messages_sent++;
376 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
378 msg = (struct GNUNET_MessageHeader *) &msgbuf;
379 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
380 msg->type = htons (TEST_MESSAGE_TYPE_PING);
381 msg->size = htons (TEST_MESSAGE_SIZE);
382 memcpy (buf, msg, TEST_MESSAGE_SIZE);
383 return TEST_MESSAGE_SIZE;
390 progress_task = GNUNET_SCHEDULER_NO_TASK;
392 fprintf (stderr, "%llu..",
393 (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
396 progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
397 &print_progress, NULL );
401 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
403 struct BenchmarkPeer *me = cls;
405 me->ats_task = GNUNET_SCHEDULER_NO_TASK;
407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
408 me->no, me->pref_partner->no, me->pref_value);
409 GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
410 &me->pref_partner->id,
411 pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
412 me->pref_value += TEST_ATS_PREFRENCE_DELTA;
413 me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
414 &ats_pref_task, cls);
423 if ((state.connected_ATS_service == GNUNET_NO)
424 || (state.connected_COMM_service == GNUNET_NO)
425 || (state.connected_PEERS == GNUNET_NO)
426 || (state.connected_CORE == GNUNET_NO))
429 state.benchmarking = GNUNET_YES;
430 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
432 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
433 GNUNET_SCHEDULER_cancel (shutdown_task);
434 shutdown_task = GNUNET_SCHEDULER_add_delayed (perf_duration,
435 &do_shutdown, NULL );
437 progress_task = GNUNET_SCHEDULER_add_now (&print_progress, NULL );
439 /* Start sending test messages */
440 for (c_m = 0; c_m < num_masters; c_m++)
442 for (c_s = 0; c_s < num_slaves; c_s++)
444 if (GNUNET_YES == test_core)
445 mps[c_m].partners[c_s].cth = GNUNET_CORE_notify_transmit_ready (
446 mps[c_m].ch, GNUNET_NO, 0, GNUNET_TIME_UNIT_MINUTES, &sps[c_s].id,
447 TEST_MESSAGE_SIZE, &comm_send_ready, &mps[c_m].partners[c_s]);
450 mps[c_m].partners[c_s].tth = GNUNET_TRANSPORT_notify_transmit_ready (
451 mps[c_m].th, &sps[c_s].id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
452 &comm_send_ready, &mps[c_m].partners[c_s]);
455 if (pref_val != GNUNET_ATS_PREFERENCE_END)
456 mps[c_m].ats_task = GNUNET_SCHEDULER_add_now (&ats_pref_task, &mps[c_m]);
458 if (GNUNET_YES == logging)
459 perf_logging_start (testname, mps, num_masters);
464 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
467 struct TestbedConnectOperation *cop = cls;
472 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
473 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
478 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
479 _("Failed to connect master peer [%u] with slave [%u]\n"),
480 cop->master->no, cop->slave->no);
482 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
483 GNUNET_SCHEDULER_cancel (shutdown_task);
484 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
486 GNUNET_TESTBED_operation_done (op);
488 for (c = 0; c < num_slaves; c++)
490 if (cop == &cop->master->core_connect_ops[c])
491 cop->master->core_connect_ops[c].connect_op = NULL;
493 if (ops == num_masters * num_slaves)
495 state.connected_PEERS = GNUNET_YES;
496 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
501 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
505 struct BenchmarkPeer *p;
507 if ((state.connected_ATS_service == GNUNET_NO)
508 || (state.connected_COMM_service == GNUNET_NO))
511 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
513 for (c_m = 0; c_m < num_masters; c_m++)
516 p->core_connect_ops = GNUNET_malloc (num_slaves *
517 sizeof (struct TestbedConnectOperation));
519 for (c_s = 0; c_s < num_slaves; c_s++)
521 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
522 _("Connecting master [%u] with slave [%u]\n"), p->no, sps[c_s].no);
523 p->core_connect_ops[c_s].master = p;
524 p->core_connect_ops[c_s].slave = &sps[c_s];
525 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
526 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
527 sps[c_s].peer, p->peer);
528 if (NULL == p->core_connect_ops[c_s].connect_op)
530 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
531 _("Could not connect master [%u] and slave [%u]\n"), p->no,
534 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
535 GNUNET_SCHEDULER_cancel (shutdown_task);
536 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
544 * Method called whenever a given peer connects.
547 * @param peer peer identity this notification is about
550 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
552 struct BenchmarkPeer *me = cls;
553 struct BenchmarkPeer *remote;
558 remote = find_peer (peer);
565 id = GNUNET_strdup (GNUNET_i2s (&me->id));
566 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
567 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
568 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
571 me->core_connections++;
572 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
573 && (GNUNET_NO == state.connected_CORE))
575 me->core_slave_connections++;
577 if (me->core_slave_connections == num_slaves)
579 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
582 completed = GNUNET_YES;
583 for (c = 0; c < num_masters; c++)
585 if (mps[c].core_slave_connections != num_slaves)
586 completed = GNUNET_NO;
588 if (GNUNET_YES == completed)
590 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
591 "All master peers connected all slave peers\n", id,
593 state.connected_CORE = GNUNET_YES;
594 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL );
601 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
603 struct BenchmarkPeer *me = cls;
604 struct BenchmarkPeer *remote;
607 remote = find_peer (peer);
614 id = GNUNET_strdup (GNUNET_i2s (&me->id));
615 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
617 GNUNET_assert(me->core_connections > 0);
618 me->core_connections--;
620 if ((GNUNET_YES == state.benchmarking)
621 && ((GNUNET_YES == me->master) || (GNUNET_YES == remote->master)))
623 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
624 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
630 comm_send_pong_ready (void *cls, size_t size, void *buf)
632 static char msgbuf[TEST_MESSAGE_SIZE];
633 struct BenchmarkPartner *p = cls;
634 struct GNUNET_MessageHeader *msg;
636 if (GNUNET_YES == test_core)
642 p->bytes_sent += TEST_MESSAGE_SIZE;
643 p->me->total_messages_sent++;
644 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
646 msg = (struct GNUNET_MessageHeader *) &msgbuf;
647 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
648 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
649 msg->size = htons (TEST_MESSAGE_SIZE);
650 memcpy (buf, msg, TEST_MESSAGE_SIZE);
652 return TEST_MESSAGE_SIZE;
656 comm_handle_ping (void *cls, const struct GNUNET_PeerIdentity *other,
657 const struct GNUNET_MessageHeader *message)
660 struct BenchmarkPeer *me = cls;
661 struct BenchmarkPartner *p = NULL;
662 for (c_m = 0; c_m < num_masters; c_m++)
664 /* Find a partner with other as destination */
665 if (0 == memcmp (other, &me->partners[c_m].dest->id,
666 sizeof(struct GNUNET_PeerIdentity)))
668 p = &me->partners[c_m];
675 return GNUNET_SYSERR;
678 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
679 "Slave [%u]: Received PING from [%u], sending PONG\n", me->no,
682 p->messages_received++;
683 p->bytes_received += TEST_MESSAGE_SIZE;
684 p->me->total_messages_received++;
685 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
687 if (GNUNET_YES == test_core)
689 GNUNET_assert (NULL == p->cth);
690 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
691 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
692 &comm_send_pong_ready, p);
696 GNUNET_assert (NULL == p->tth);
697 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (me->th, &p->dest->id,
698 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
705 comm_handle_pong (void *cls, const struct GNUNET_PeerIdentity *other,
706 const struct GNUNET_MessageHeader *message)
709 struct BenchmarkPeer *me = cls;
710 struct BenchmarkPartner *p = NULL;
712 for (c_s = 0; c_s < num_slaves; c_s++)
715 == memcmp (other, &me->partners[c_s].dest->id,
716 sizeof(struct GNUNET_PeerIdentity)))
718 p = &me->partners[c_s];
725 return GNUNET_SYSERR;
728 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
729 "Master [%u]: Received PONG from [%u], next message\n", me->no,
732 p->messages_received++;
733 p->bytes_received += TEST_MESSAGE_SIZE;
734 p->me->total_messages_received++;
735 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
737 if (GNUNET_YES == test_core)
739 GNUNET_assert (NULL == p->cth);
740 p->cth = GNUNET_CORE_notify_transmit_ready (me->ch, GNUNET_NO, 0,
741 GNUNET_TIME_UNIT_MINUTES, &p->dest->id, TEST_MESSAGE_SIZE,
742 &comm_send_ready, p);
746 GNUNET_assert (NULL == p->tth);
747 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
748 me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
749 &comm_send_ready, p);
756 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
758 struct BenchmarkPeer *me = cls;
760 static const struct GNUNET_CORE_MessageHandler handlers[] = { {
761 &comm_handle_ping, TEST_MESSAGE_TYPE_PING, 0 }, { &comm_handle_pong,
762 TEST_MESSAGE_TYPE_PONG, 0 }, { NULL, 0, 0 } };
764 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
765 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
767 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
772 core_disconnect_adapter (void *cls, void *op_result)
774 struct BenchmarkPeer *me = cls;
776 GNUNET_CORE_disconnect (me->ch);
781 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
782 void *ca_result, const char *emsg)
784 static int comm_done = 0;
785 if ((NULL != emsg) || (NULL == ca_result))
787 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
789 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
790 GNUNET_SCHEDULER_cancel (shutdown_task);
791 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
796 if (comm_done == num_slaves + num_masters)
798 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
799 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
800 state.connected_COMM_service = GNUNET_YES;
801 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
806 transport_recv_cb (void *cls,
807 const struct GNUNET_PeerIdentity * peer,
808 const struct GNUNET_MessageHeader * message)
810 if (TEST_MESSAGE_SIZE != ntohs (message->size) ||
811 (TEST_MESSAGE_TYPE_PING != ntohs (message->type) &&
812 TEST_MESSAGE_TYPE_PONG != ntohs (message->type)))
816 if (TEST_MESSAGE_TYPE_PING == ntohs (message->type))
817 comm_handle_ping (cls, peer, message);
819 if (TEST_MESSAGE_TYPE_PONG == ntohs (message->type))
820 comm_handle_pong (cls, peer, message);
825 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
827 struct BenchmarkPeer *me = cls;
829 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, &transport_recv_cb,
830 &comm_connect_cb, &comm_disconnect_cb);
832 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
837 transport_disconnect_adapter (void *cls, void *op_result)
839 struct BenchmarkPeer *me = cls;
841 GNUNET_TRANSPORT_disconnect (me->th);
846 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
850 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
851 (GNUNET_YES == test_core) ? "CORE" : "TRANSPORT");
852 for (c_m = 0; c_m < num_masters; c_m++)
854 if (GNUNET_YES == test_core)
855 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
856 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
857 &core_disconnect_adapter, &mps[c_m]);
860 mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
861 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
862 &transport_disconnect_adapter, &mps[c_m]);
866 for (c_s = 0; c_s < num_slaves; c_s++)
868 if (GNUNET_YES == test_core)
869 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
870 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
871 &core_disconnect_adapter, &sps[c_s]);
874 sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
875 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
876 &transport_disconnect_adapter, &sps[c_s]);
882 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
883 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
884 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
885 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
887 struct BenchmarkPeer *me = cls;
891 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
892 for (c_a = 0; c_a < ats_count; c_a++)
894 /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("%c %03u: %s %s %u\n"),
895 (GNUNET_YES == p->master) ? 'M' : 'S',
897 GNUNET_i2s (&address->peer),
898 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
899 ntohl(ats[c_a].value));*/
902 if ((GNUNET_YES == me->master)
903 && (0 == memcmp (&address->peer, &me->destination->id,
904 sizeof(struct GNUNET_PeerIdentity))))
906 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Bandwidth for master %u: %lu %lu\n",
907 me->no, (long unsigned int ) ntohl (bandwidth_in.value__),
908 (long unsigned int ) ntohl (bandwidth_in.value__));
911 store_information (&bp->id, address, address_active, bandwidth_in,
912 bandwidth_out, ats, ats_count);
914 GNUNET_free(peer_id);
918 ats_perf_connect_adapter (void *cls,
919 const struct GNUNET_CONFIGURATION_Handle *cfg)
921 struct BenchmarkPeer *me = cls;
923 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg,
924 &ats_performance_info_cb, me);
925 if (NULL == me->ats_perf_handle)
926 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
927 "Failed to create ATS performance handle \n");
928 return me->ats_perf_handle;
932 ats_perf_disconnect_adapter (void *cls, void *op_result)
934 struct BenchmarkPeer *me = cls;
936 GNUNET_ATS_performance_done (me->ats_perf_handle);
937 me->ats_perf_handle = NULL;
941 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
942 void *ca_result, const char *emsg)
944 static int op_done = 0;
946 if ((NULL != emsg) || (NULL == ca_result))
948 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
950 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
951 GNUNET_SCHEDULER_cancel (shutdown_task);
952 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
956 if (op_done == (num_masters + num_slaves))
958 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
959 state.connected_ATS_service = GNUNET_YES;
960 GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
965 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
970 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
971 for (c_m = 0; c_m < num_masters; c_m++)
973 mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, mps[c_m].peer,
974 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
975 &ats_perf_disconnect_adapter, &mps[c_m]);
979 for (c_s = 0; c_s < num_slaves; c_s++)
981 sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, sps[c_s].peer,
982 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
983 &ats_perf_disconnect_adapter, &sps[c_s]);
989 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
990 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
992 struct BenchmarkPeer *p = cb_cls;
995 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
997 p->id = *pinfo->result.id;
998 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
999 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
1000 GNUNET_i2s (&p->id));
1002 GNUNET_TESTBED_operation_done (op);
1003 p->peer_id_op = NULL;
1006 if (done == num_slaves + num_masters)
1008 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1009 "Retrieved all peer ID, connect to ATS\n");
1010 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
1015 * Signature of a main function for a testcase.
1017 * @param cls closure
1018 * @param num_peers number of peers in 'peers'
1019 * @param peers_ handle to peers run in the testbed
1020 * @param links_succeeded the number of overlay link connection attempts that
1022 * @param links_failed the number of overlay link connection attempts that
1026 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
1027 struct GNUNET_TESTBED_Peer **peers_, unsigned int links_succeeded,
1028 unsigned int links_failed)
1032 GNUNET_assert(NULL == cls);
1033 GNUNET_assert(num_masters + num_slaves == num_peers);
1034 GNUNET_assert(NULL != peers_);
1036 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1037 _("Benchmarking solver `%s' on preference `%s' with %u master and %u slave peers\n"),
1038 solver, pref_str, num_masters, num_slaves);
1040 shutdown_task = GNUNET_SCHEDULER_add_delayed (
1041 GNUNET_TIME_relative_multiply (TEST_TIMEOUT, num_masters + num_slaves),
1042 &do_shutdown, NULL );
1044 /* Setup master peers */
1045 for (c_m = 0; c_m < num_masters; c_m++)
1047 GNUNET_assert(NULL != peers_[c_m]);
1048 mps[c_m].peer = peers_[c_m];
1050 mps[c_m].master = GNUNET_YES;
1051 mps[c_m].pref_partner = &sps[c_m];
1052 mps[c_m].pref_value = TEST_ATS_PREFRENCE_START;
1054 GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1055 mps[c_m].num_partners = num_slaves;
1056 /* Initialize partners */
1057 for (c_s = 0; c_s < num_slaves; c_s++)
1059 mps[c_m].partners[c_s].me = &mps[c_m];
1060 mps[c_m].partners[c_s].dest = &sps[c_s];
1062 mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (mps[c_m].peer,
1063 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &mps[c_m]);
1066 /* Setup slave peers */
1067 for (c_s = 0; c_s < num_slaves; c_s++)
1069 GNUNET_assert(NULL != peers_[c_s + num_masters]);
1070 sps[c_s].peer = peers_[c_s + num_masters];
1071 sps[c_s].no = c_s + num_masters;
1072 sps[c_s].master = GNUNET_NO;
1074 GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1075 sps[c_s].num_partners = num_masters;
1076 /* Initialize partners */
1077 for (c_m = 0; c_m < num_masters; c_m++)
1079 sps[c_s].partners[c_m].me = &sps[c_s];
1080 sps[c_s].partners[c_m].dest = &mps[c_m];
1082 sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (sps[c_s].peer,
1083 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &sps[c_s]);
1088 main (int argc, char *argv[])
1096 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1101 /* figure out testname */
1102 tmp = strstr (argv[0], TESTNAME_PREFIX);
1105 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1106 return GNUNET_SYSERR;
1108 tmp += strlen (TESTNAME_PREFIX);
1109 solver = GNUNET_strdup (tmp);
1110 if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
1112 tmp_sep = strchr (solver, '_');
1113 if (NULL == tmp_sep)
1115 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1116 GNUNET_free(solver);
1117 return GNUNET_SYSERR;
1120 comm_name = GNUNET_strdup (&tmp_sep[1]);
1121 tmp_sep = strchr (comm_name, '_');
1122 if (NULL == tmp_sep)
1124 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
1125 GNUNET_free(solver);
1126 return GNUNET_SYSERR;
1129 for (c = 0; c <= strlen (comm_name); c++)
1130 comm_name[c] = toupper (comm_name[c]);
1131 if (0 == strcmp (comm_name, "CORE"))
1132 test_core = GNUNET_YES;
1133 else if (0 == strcmp (comm_name, "TRANSPORT"))
1134 test_core = GNUNET_NO;
1137 GNUNET_free (comm_name);
1138 GNUNET_free (solver);
1139 return GNUNET_SYSERR;
1142 pref_str = GNUNET_strdup(tmp_sep + 1);
1144 GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
1146 GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
1148 for (c = 0; c <= strlen (pref_str); c++)
1149 pref_str[c] = toupper (pref_str[c]);
1152 if (0 != strcmp (pref_str, "NONE"))
1154 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
1156 if (0 == strcmp (pref_str, prefs[c]))
1165 /* abuse terminator to indicate no pref */
1166 pref_val = GNUNET_ATS_PREFERENCE_END;
1170 fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
1171 GNUNET_free(solver);
1172 GNUNET_free(pref_str);
1173 GNUNET_free (comm_name);
1177 for (c = 0; c < (argc - 1); c++)
1179 if (0 == strcmp (argv[c], "-d"))
1184 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
1185 fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
1189 perf_duration = BENCHMARK_DURATION;
1191 fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
1193 for (c = 0; c < (argc - 1); c++)
1195 if (0 == strcmp (argv[c], "-s"))
1200 if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
1201 && (num_slaves >= 1))
1202 fprintf (stderr, "Starting %u slave peers\n", num_slaves);
1204 num_slaves = DEFAULT_SLAVES_NUM;
1207 num_slaves = DEFAULT_SLAVES_NUM;
1209 for (c = 0; c < (argc - 1); c++)
1211 if (0 == strcmp (argv[c], "-m"))
1216 if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
1217 && (num_masters >= 2))
1218 fprintf (stderr, "Starting %u master peers\n", num_masters);
1220 num_masters = DEFAULT_MASTERS_NUM;
1223 num_masters = DEFAULT_MASTERS_NUM;
1225 logging = GNUNET_NO;
1226 for (c = 0; c < argc; c++)
1228 if (0 == strcmp (argv[c], "-l"))
1229 logging = GNUNET_YES;
1232 GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
1234 if (num_slaves < num_masters)
1236 fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
1237 GNUNET_free(test_name);
1238 GNUNET_free(solver);
1239 GNUNET_free(pref_str);
1240 GNUNET_free (comm_name);
1241 return GNUNET_SYSERR;
1244 state.connected_ATS_service = GNUNET_NO;
1245 state.connected_COMM_service = GNUNET_NO;
1246 state.connected_PEERS = GNUNET_NO;
1247 state.benchmarking = GNUNET_NO;
1248 state.connected_PEERS = GNUNET_NO;
1250 mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
1251 sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
1253 /* Start topology */
1254 uint64_t event_mask;
1256 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1257 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1258 (void) GNUNET_TESTBED_test_run ("perf-ats", conf_name,
1259 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
1262 GNUNET_free(solver);
1263 GNUNET_free(pref_str);
1264 GNUNET_free(conf_name);
1265 GNUNET_free(test_name);
1266 GNUNET_free(testname);
1267 GNUNET_free (comm_name);
1274 /* end of file perf_ats.c */