2 This file is part of GNUnet.
3 (C) 2010,2011 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/test_ats_api_performance_monitor.c
22 * @brief test performance monitoring
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
32 #define SHUTDOWN_CORRECT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
36 static GNUNET_SCHEDULER_TaskIdentifier die_task;
37 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
39 struct GNUNET_CONFIGURATION_Handle *cfg;
41 static struct GNUNET_ATS_SchedulingHandle *sh;
43 static struct GNUNET_ATS_PerformanceHandle *ph;
45 static struct GNUNET_ATS_PerformanceMonitorHandle *phm;
47 static struct GNUNET_HELLO_Address addr;
49 static struct GNUNET_ATS_Information atsi[ATS_COUNT];
53 static void cleanup_addresses ()
55 GNUNET_ATS_address_destroyed (sh, &addr, NULL);
58 static void setup_addresses ()
60 memset (&addr.peer,'\0', sizeof (addr.peer));
61 addr.transport_name = "test";
63 addr.address_length = 0;
64 atsi[0].type = htonl(GNUNET_ATS_NETWORK_TYPE);
65 atsi[0].value = htonl(GNUNET_ATS_NET_LAN);
67 atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
68 atsi[1].value = htonl(100);
70 atsi[2].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
71 atsi[2].value = htonl(5);
73 GNUNET_ATS_address_add (sh, &addr, NULL, atsi, ATS_COUNT);
80 if (GNUNET_SCHEDULER_NO_TASK != stage_task)
82 GNUNET_SCHEDULER_cancel (stage_task);
83 stage_task = GNUNET_SCHEDULER_NO_TASK;
85 if (GNUNET_SCHEDULER_NO_TASK != die_task)
87 GNUNET_SCHEDULER_cancel (die_task);
88 die_task = GNUNET_SCHEDULER_NO_TASK;
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
96 GNUNET_ATS_performance_monitor_stop (phm);
102 GNUNET_ATS_performance_done (ph);
108 GNUNET_ATS_scheduling_done (sh);
115 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 die_task = GNUNET_SCHEDULER_NO_TASK;
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout\n");
123 next_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
125 static int stage_counter = 0;
127 stage_task = GNUNET_SCHEDULER_NO_TASK;
128 if (0 == stage_counter)
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stop performance monitoring\n");
132 GNUNET_ATS_performance_monitor_stop (phm);
135 stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
145 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Success\n");
153 perf_mon_cb (void *cls,
154 struct GNUNET_PeerIdentity *peer,
155 struct GNUNET_ATS_Information *ats,
158 static int stage_counter = 0;
163 if (0 == stage_counter)
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received initial callback for peer `%s' with %u information\n",
166 GNUNET_i2s (&addr.peer), ats_count);
167 if ((0 != memcmp (peer, &addr.peer, sizeof (addr.peer))) ||
168 (ats_count < ATS_COUNT))
171 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
175 for (c1 = 0; c1 < ats_count; c1++)
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] %u : %u \n", c1, ntohl (ats[c1].type), ntohl (ats[c1].value));
178 for (c2 = 0; c2 < ATS_COUNT; c2++)
180 if (ats[c1].type == atsi[c2].type)
182 if (ats[c1].value == atsi[c2].value)
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
198 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information \n", c3);
204 atsi[0].type = htonl(GNUNET_ATS_NETWORK_TYPE);
205 atsi[0].value = htonl(GNUNET_ATS_NET_WAN);
207 atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
208 atsi[1].value = htonl(1000);
210 atsi[2].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
211 atsi[2].value = htonl(50);
213 GNUNET_ATS_address_update (sh, &addr, NULL, atsi, ATS_COUNT);
215 else if (1 == stage_counter)
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received updated callback for peer `%s' with %u information\n",
218 GNUNET_i2s (&addr.peer), ats_count);
220 if ((0 != memcmp (peer, &addr.peer, sizeof (addr.peer))) ||
221 (ats_count < ATS_COUNT))
224 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
228 for (c1 = 0; c1 < ats_count; c1++)
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] %u : %u \n", c1, ntohl (ats[c1].type), ntohl (ats[c1].value));
231 for (c2 = 0; c2 < ATS_COUNT; c2++)
233 if (ats[c1].type == atsi[c2].type)
235 if (ats[c1].value == atsi[c2].value)
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
251 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information, shutdown... \n", c3);
256 GNUNET_SCHEDULER_add_now (&end, NULL);
264 const struct GNUNET_CONFIGURATION_Handle *mycfg,
265 struct GNUNET_TESTING_Peer *peer)
268 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
269 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
271 sh = GNUNET_ATS_scheduling_init (cfg, NULL, NULL);
272 GNUNET_assert (NULL != sh);
276 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
277 GNUNET_assert (NULL != ph);
279 phm = GNUNET_ATS_performance_monitor_start (ph, &perf_mon_cb, &ret);
280 GNUNET_assert (NULL != phm);
282 stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
287 main (int argc, char *argv[])
289 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance_monitor",
296 /* end of file test_ats_api_performance_monitor.c */