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 monitoring
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, 20)
32 #define SHUTDOWN_CORRECT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
33
34 #define ATS_COUNT 3
35
36 static GNUNET_SCHEDULER_TaskIdentifier die_task;
37 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
38
39 struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 static struct GNUNET_ATS_SchedulingHandle *sh;
42
43 static struct GNUNET_ATS_PerformanceHandle *ph;
44
45 static struct GNUNET_ATS_PerformanceMonitorHandle *phm;
46
47 static struct GNUNET_HELLO_Address addr;
48
49 static struct GNUNET_ATS_Information atsi[ATS_COUNT];
50
51 static int ret;
52
53 static void cleanup_addresses ()
54 {
55         GNUNET_ATS_address_destroyed (sh, &addr, NULL);
56 }
57
58 static void setup_addresses ()
59 {
60         memset (&addr.peer,'\0', sizeof (addr.peer));
61         addr.transport_name = "test";
62         addr.address = NULL;
63         addr.address_length = 0;
64         atsi[0].type = htonl(GNUNET_ATS_NETWORK_TYPE);
65         atsi[0].value = htonl(GNUNET_ATS_NET_LAN);
66
67         atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
68         atsi[1].value = htonl(100);
69
70         atsi[2].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
71         atsi[2].value = htonl(5);
72
73         GNUNET_ATS_address_add (sh, &addr, NULL, atsi, ATS_COUNT);
74 }
75
76
77 static void
78 end_now (int res)
79 {
80         if (GNUNET_SCHEDULER_NO_TASK != stage_task)
81         {
82                         GNUNET_SCHEDULER_cancel (stage_task);
83                         stage_task = GNUNET_SCHEDULER_NO_TASK;
84         }
85         if (GNUNET_SCHEDULER_NO_TASK != die_task)
86         {
87                         GNUNET_SCHEDULER_cancel (die_task);
88                         die_task = GNUNET_SCHEDULER_NO_TASK;
89         }
90   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
91
92         cleanup_addresses ();
93
94         if (NULL != phm)
95         {
96                 GNUNET_ATS_performance_monitor_stop (phm);
97                 phm = NULL;
98         }
99
100         if (NULL != ph)
101         {
102                 GNUNET_ATS_performance_done (ph);
103                 ph = NULL;
104         }
105
106         if (NULL != sh)
107         {
108                 GNUNET_ATS_scheduling_done (sh);
109                 sh = NULL;
110         }
111         ret = res;
112 }
113
114 static void
115 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   die_task = GNUNET_SCHEDULER_NO_TASK;
118   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout\n");
119   end_now (1);
120 }
121
122 static void
123 next_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 {
125         static int stage_counter = 0;
126
127         stage_task = GNUNET_SCHEDULER_NO_TASK;
128         if (0 == stage_counter)
129         {
130                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stop performance monitoring\n");
131
132                 GNUNET_ATS_performance_monitor_stop (phm);
133                 phm = NULL;
134
135                 stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
136                 stage_counter++;
137                 return;
138         }
139         else
140         {
141                         end_now (0);
142         }
143 }
144
145 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
146 {
147   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Success\n");
148   end_now (1);
149 }
150
151
152 static void
153 perf_mon_cb (void *cls,
154                                                 struct GNUNET_PeerIdentity *peer,
155                                                 struct GNUNET_ATS_Information *ats,
156                                                 uint32_t ats_count)
157 {
158         static int stage_counter = 0;
159         int c1;
160         int c2;
161         int c3;
162
163         if (0 == stage_counter)
164         {
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))
169                 {
170                                 GNUNET_break (0);
171                                 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
172                                 return;
173                 }
174                 c3 = 0;
175                 for (c1 = 0; c1 < ats_count; c1++)
176                 {
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++)
179                                 {
180                                         if (ats[c1].type == atsi[c2].type)
181                                         {
182                                                 if (ats[c1].value == atsi[c2].value)
183                                                 {
184                                                                 c3++;
185                                                 }
186                                                 else
187                                                 {
188                                                                 GNUNET_break (0);
189                                                 }
190                                         }
191                                 }
192                 }
193
194                 if (ATS_COUNT != c3)
195                 {
196                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
197                         GNUNET_break (0);
198                         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
199                         return;
200                 }
201                 stage_counter ++;
202                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information \n", c3);
203
204                 atsi[0].type = htonl(GNUNET_ATS_NETWORK_TYPE);
205                 atsi[0].value = htonl(GNUNET_ATS_NET_WAN);
206
207                 atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
208                 atsi[1].value = htonl(1000);
209
210                 atsi[2].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
211                 atsi[2].value = htonl(50);
212
213                 GNUNET_ATS_address_update (sh, &addr, NULL, atsi, ATS_COUNT);
214         }
215         else if (1 == stage_counter)
216         {
217                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received updated callback for peer `%s' with %u information\n",
218                                         GNUNET_i2s (&addr.peer), ats_count);
219
220                         if ((0 != memcmp (peer, &addr.peer, sizeof (addr.peer))) ||
221                                         (ats_count < ATS_COUNT))
222                         {
223                                         GNUNET_break (0);
224                                         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
225                                         return;
226                         }
227                         c3 = 0;
228                         for (c1 = 0; c1 < ats_count; c1++)
229                         {
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++)
232                                         {
233                                                 if (ats[c1].type == atsi[c2].type)
234                                                 {
235                                                         if (ats[c1].value == atsi[c2].value)
236                                                         {
237                                                                         c3++;
238                                                         }
239                                                         else
240                                                         {
241                                                                         GNUNET_break (0);
242                                                         }
243                                                 }
244                                         }
245                         }
246
247                         if (ATS_COUNT != c3)
248                         {
249                           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
250                                 GNUNET_break (0);
251                                 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
252                                 return;
253                         }
254                         stage_counter ++;
255                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information, shutdown... \n", c3);
256                         GNUNET_SCHEDULER_add_now (&end, NULL);
257                         return;
258         }
259 }
260
261
262 static void
263 run (void *cls, 
264      const struct GNUNET_CONFIGURATION_Handle *mycfg,
265      struct GNUNET_TESTING_Peer *peer)
266 {
267   ret = 1;
268   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
269   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
270
271   sh = GNUNET_ATS_scheduling_init (cfg, NULL, NULL);
272   GNUNET_assert (NULL != sh);
273
274   setup_addresses ();
275
276   ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
277   GNUNET_assert (NULL != ph);
278
279   phm = GNUNET_ATS_performance_monitor_start (ph, &perf_mon_cb, &ret);
280   GNUNET_assert (NULL != phm);
281
282   stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
283 }
284
285
286 int
287 main (int argc, char *argv[])
288 {
289   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance_monitor",
290                                     "test_ats_api.conf",
291                                     &run, NULL))
292     return 1;
293   return ret;
294 }
295
296 /* end of file test_ats_api_performance_monitor.c */