performance monitoring test
[oweals/gnunet.git] / src / ats / test_ats_api_performance_monitor.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010,2011 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/test_ats_api_performance_monitor.c
22  * @brief test performance API's address monitor feature
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "ats.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
34
35 /**
36  * Statistics handle
37  */
38 struct GNUNET_STATISTICS_Handle *stats;
39
40 /**
41  * Configuration handle
42  */
43 struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 /**
46  * ATS scheduling handle
47  */
48 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
49
50 /**
51  * ATS performance handle
52  */
53 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
54
55 struct GNUNET_ATS_AddressListHandle* phal;
56
57 static int ret;
58
59 struct Address
60 {
61   char *plugin;
62   size_t plugin_len;
63
64   void *addr;
65   size_t addr_len;
66
67   struct GNUNET_ATS_Information *ats;
68   int ats_count;
69
70   void *session;
71 };
72
73 struct PeerContext
74 {
75   struct GNUNET_PeerIdentity id;
76
77   struct Address *addr;
78 };
79
80 static struct PeerContext p[2];
81
82 static struct Address p0_addresses[2];
83 static struct Address p1_addresses[2];
84
85 struct GNUNET_HELLO_Address p0_ha[2];
86 struct GNUNET_HELLO_Address p1_ha[2];
87 struct GNUNET_HELLO_Address *s_ha[2];
88
89 static void
90 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
91
92 static void
93 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
94
95
96 static void
97 ats_perf_cb (void *cls,
98             const struct GNUNET_HELLO_Address *address,
99             int address_active,
100             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
101             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
102             const struct GNUNET_ATS_Information *ats,
103             uint32_t ats_count)
104 {
105   static int peer0 = GNUNET_NO;
106   static int peer1 = GNUNET_NO;
107   if ((GNUNET_NO == peer0) && (0 == memcmp (address, &p[0].id, sizeof (p[0].id))))
108   {
109     peer0 = GNUNET_YES;
110   }
111   if ((GNUNET_NO == peer0) && (0 == memcmp (address, &p[1].id, sizeof (p[1].id))))
112   {
113     peer1 = GNUNET_YES;
114   }
115   if ((peer0 == GNUNET_YES) && (peer1 = GNUNET_YES))
116   {
117     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
118         "Done\n");
119     GNUNET_SCHEDULER_add_now (&end, NULL);
120   }
121 }
122
123
124 static int
125 stat_cb(void *cls, const char *subsystem,
126         const char *name, uint64_t value,
127         int is_persistent)
128 {
129
130   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
131       subsystem,name, value);
132   if (4 == value)
133   {
134     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
135         "All addresses added\n");
136   }
137
138   return GNUNET_OK;
139
140 }
141
142 static void
143 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
144                     struct Session *session,
145                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
146                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
147                     const struct GNUNET_ATS_Information *atsi,
148                     uint32_t ats_count)
149 {
150   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
151   GNUNET_SCHEDULER_add_now (&end_badly, NULL);
152   return;
153 }
154
155
156 static void
157 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 {
159   die_task = GNUNET_SCHEDULER_NO_TASK;
160   end ( NULL, NULL);
161   ret = GNUNET_SYSERR;
162 }
163
164 static void
165 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
168   if (die_task != GNUNET_SCHEDULER_NO_TASK )
169   {
170     GNUNET_SCHEDULER_cancel (die_task);
171     die_task = GNUNET_SCHEDULER_NO_TASK;
172   }
173
174   if (NULL != sched_ats)
175   {
176     GNUNET_ATS_scheduling_done (sched_ats);
177     sched_ats = NULL;
178   }
179
180   if (NULL != perf_ats)
181   {
182     GNUNET_ATS_performance_done (perf_ats);
183     perf_ats = NULL;
184   }
185
186   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
187   if (NULL != stats)
188   {
189     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
190     stats = NULL;
191   }
192
193
194   GNUNET_free_non_null(p0_addresses[0].addr);
195   GNUNET_free_non_null(p0_addresses[1].addr);
196   GNUNET_free_non_null(p1_addresses[0].addr);
197   GNUNET_free_non_null(p1_addresses[1].addr);
198
199   ret = 0;
200 }
201
202
203
204 static void
205 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
206     struct GNUNET_TESTING_Peer *peer)
207 {
208   ret = 1;
209   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
210   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
211
212   if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL)))
213   {
214     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215         "Failed to connect to performance API\n");
216     GNUNET_SCHEDULER_add_now (end_badly, NULL);
217   }
218
219   stats = GNUNET_STATISTICS_create ("ats", cfg);
220   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
221
222   /* set up peer 0 */
223   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
224       &p[0].id.hashPubKey);
225
226   p0_addresses[0].plugin = "test";
227   p0_addresses[0].session = NULL;
228   p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
229   p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
230
231   p0_ha[0].address = p0_addresses[0].addr;
232   p0_ha[0].address_length = p0_addresses[0].addr_len;
233   p0_ha[0].peer = p[0].id;
234   p0_ha[0].transport_name = p0_addresses[0].plugin;
235
236   p0_addresses[1].plugin = "test";
237   p0_addresses[1].session = NULL;
238   p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
239   p0_addresses[1].addr_len = strlen (p0_addresses[1].addr) + 1;
240
241   p0_ha[1].address = p0_addresses[1].addr;
242   p0_ha[1].address_length = p0_addresses[1].addr_len;
243   p0_ha[1].peer = p[0].id;
244   p0_ha[1].transport_name = p0_addresses[1].plugin;
245
246   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
247       GNUNET_i2s (&p[0].id));
248
249   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
250       &p[1].id.hashPubKey);
251
252   p1_addresses[0].plugin = "test";
253   p1_addresses[0].session = NULL;
254   p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
255   p1_addresses[0].addr_len = strlen (p1_addresses[0].addr) + 1;
256
257   p1_ha[0].address = p1_addresses[0].addr;
258   p1_ha[0].address_length = p1_addresses[0].addr_len;
259   p1_ha[0].peer = p[1].id;
260   p1_ha[0].transport_name = p1_addresses[0].plugin;
261
262   p1_addresses[1].plugin = "test";
263   p1_addresses[1].session = NULL;
264   p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
265   p1_addresses[1].addr_len = strlen (p1_addresses[1].addr) + 1;
266
267   p1_ha[1].address = p1_addresses[1].addr;
268   p1_ha[1].address_length = p1_addresses[1].addr_len;
269   p1_ha[1].peer = p[1].id;
270   p1_ha[1].transport_name = p1_addresses[1].plugin;
271
272   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
273       GNUNET_i2s (&p[1].id));
274
275   /* Add addresses */
276   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL );
277   if (sched_ats == NULL )
278   {
279     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
280     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
281     return;
282   }
283
284   GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
285   GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
286
287   GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
288   GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
289 }
290
291 int
292 main (int argc, char *argv[])
293 {
294   if (0
295       != GNUNET_TESTING_peer_run ("test_ats_api_performance",
296           "test_ats_api.conf", &run, NULL ))
297     return 1;
298   return ret;
299 }
300
301 /* end of file test_ats_api_performance_monitor.c */