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 API's address monitor feature
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, 10)
32 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34 static struct GNUNET_SCHEDULER_Task * die_task;
39 struct GNUNET_STATISTICS_Handle *stats;
42 * Configuration handle
44 struct GNUNET_CONFIGURATION_Handle *cfg;
47 * ATS scheduling handle
49 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
52 * ATS performance handle
54 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
56 struct GNUNET_ATS_AddressListHandle* phal;
68 struct GNUNET_ATS_Information *ats;
76 struct GNUNET_PeerIdentity id;
81 static struct PeerContext p[2];
83 static struct Address p0_addresses[2];
84 static struct Address p1_addresses[2];
86 struct GNUNET_HELLO_Address p0_ha[2];
87 struct GNUNET_HELLO_Address p1_ha[2];
88 struct GNUNET_HELLO_Address *s_ha[2];
91 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
94 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
98 ats_perf_cb (void *cls,
99 const struct GNUNET_HELLO_Address *address,
101 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
102 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
103 const struct GNUNET_ATS_Information *ats,
106 static int peer0 = GNUNET_NO;
107 static int peer1 = GNUNET_NO;
108 static int done = GNUNET_NO;
113 if ((GNUNET_NO == peer0) && (0 == memcmp (address, &p[0].id, sizeof (p[0].id))))
117 if ((GNUNET_NO == peer0) && (0 == memcmp (address, &p[1].id, sizeof (p[1].id))))
121 if ((peer0 == GNUNET_YES) && (peer1 = GNUNET_YES) && (GNUNET_NO == done))
123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
126 GNUNET_SCHEDULER_add_now (&end, NULL);
133 stat_cb(void *cls, const char *subsystem,
134 const char *name, uint64_t value,
138 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
139 subsystem,name, value);
142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
143 "All addresses added\n");
151 address_suggest_cb (void *cls,
152 const struct GNUNET_PeerIdentity *peer,
153 const struct GNUNET_HELLO_Address *address,
154 struct Session *session,
155 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
156 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
157 const struct GNUNET_ATS_Information *atsi,
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
162 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
168 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
176 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
179 if (die_task != NULL )
181 GNUNET_SCHEDULER_cancel (die_task);
185 if (NULL != sched_ats)
187 GNUNET_ATS_scheduling_done (sched_ats);
191 if (NULL != perf_ats)
193 GNUNET_ATS_performance_done (perf_ats);
197 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
200 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
205 GNUNET_free_non_null(p0_addresses[0].addr);
206 GNUNET_free_non_null(p0_addresses[1].addr);
207 GNUNET_free_non_null(p1_addresses[0].addr);
208 GNUNET_free_non_null(p1_addresses[1].addr);
214 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
215 struct GNUNET_TESTING_Peer *peer)
218 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
219 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
221 if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL)))
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
224 "Failed to connect to performance API\n");
225 GNUNET_SCHEDULER_add_now (end_badly, NULL);
229 stats = GNUNET_STATISTICS_create ("ats", cfg);
230 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
233 memset (&p[0].id, '1', sizeof (p[0].id));
234 p0_addresses[0].plugin = "test";
235 p0_addresses[0].session = NULL;
236 p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
237 p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
239 p0_ha[0].address = p0_addresses[0].addr;
240 p0_ha[0].address_length = p0_addresses[0].addr_len;
241 p0_ha[0].peer = p[0].id;
242 p0_ha[0].transport_name = p0_addresses[0].plugin;
244 p0_addresses[1].plugin = "test";
245 p0_addresses[1].session = NULL;
246 p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
247 p0_addresses[1].addr_len = strlen (p0_addresses[1].addr) + 1;
249 p0_ha[1].address = p0_addresses[1].addr;
250 p0_ha[1].address_length = p0_addresses[1].addr_len;
251 p0_ha[1].peer = p[0].id;
252 p0_ha[1].transport_name = p0_addresses[1].plugin;
254 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
255 GNUNET_i2s (&p[0].id));
257 memset (&p[1].id, '2', sizeof (p[1].id));
258 p1_addresses[0].plugin = "test";
259 p1_addresses[0].session = NULL;
260 p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
261 p1_addresses[0].addr_len = strlen (p1_addresses[0].addr) + 1;
263 p1_ha[0].address = p1_addresses[0].addr;
264 p1_ha[0].address_length = p1_addresses[0].addr_len;
265 p1_ha[0].peer = p[1].id;
266 p1_ha[0].transport_name = p1_addresses[0].plugin;
268 p1_addresses[1].plugin = "test";
269 p1_addresses[1].session = NULL;
270 p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
271 p1_addresses[1].addr_len = strlen (p1_addresses[1].addr) + 1;
273 p1_ha[1].address = p1_addresses[1].addr;
274 p1_ha[1].address_length = p1_addresses[1].addr_len;
275 p1_ha[1].peer = p[1].id;
276 p1_ha[1].transport_name = p1_addresses[1].plugin;
278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
279 GNUNET_i2s (&p[1].id));
282 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL );
283 if (sched_ats == NULL )
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
286 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
290 GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
291 GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
293 GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
294 GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
298 main (int argc, char *argv[])
301 != GNUNET_TESTING_peer_run ("test_ats_api_performance",
302 "test_ats_api.conf", &run, NULL ))
307 /* end of file test_ats_api_performance_monitor.c */