performance analysis
[oweals/gnunet.git] / src / ats / perf_ats_mlp.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/perf_ats_mlp
22  * @brief performance test for the MLP solver
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25
26  */
27
28 /*
29      This file is part of GNUnet.
30      (C) 2010,2011 Christian Grothoff (and other contributing authors)
31
32      GNUnet is free software; you can redistribute it and/or modify
33      it under the terms of the GNU General Public License as published
34      by the Free Software Foundation; either version 3, or (at your
35      option) any later version.
36
37      GNUnet is distributed in the hope that it will be useful, but
38      WITHOUT ANY WARRANTY; without even the implied warranty of
39      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
40      General Public License for more details.
41
42      You should have received a copy of the GNU General Public License
43      along with GNUnet; see the file COPYING.  If not, write to the
44      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
45      Boston, MA 02111-1307, USA.
46 */
47 /**
48  * @file ats/test_ats_mlp.c
49  * @brief basic test for the MLP solver
50  * @author Christian Grothoff
51  * @author Matthias Wachs
52
53  */
54 #include "platform.h"
55 #include "gnunet_util_lib.h"
56 #include "gnunet_statistics_service.h"
57 #include "gnunet_ats_service.h"
58 #include "gnunet-service-ats_addresses_mlp.h"
59 #include "test_ats_api_common.h"
60
61 #define PEERS_START 100
62 #define PEERS_END       100
63
64 #define ADDRESSES 10
65
66 int count_p;
67 int count_a;
68
69
70 struct PerfPeer
71 {
72         struct GNUNET_PeerIdentity id;
73
74         struct ATS_Address *head;
75         struct ATS_Address *tail;
76 };
77
78 static int ret;
79
80 static int N_peers_start;
81 static int N_peers_end;
82 static int N_address;
83
84 /**
85  * Statistics handle
86  */
87 struct GNUNET_STATISTICS_Handle * stats;
88
89 /**
90  * MLP solver handle
91  */
92 struct GAS_MLP_Handle *mlp;
93
94 /**
95  * Hashmap containing addresses
96  */
97 struct GNUNET_CONTAINER_MultiHashMap * addresses;
98
99 struct PerfPeer *peers;
100
101 static void
102 end_now (int res)
103 {
104   if (NULL != stats)
105   {
106           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
107           stats = NULL;
108   }
109   /*
110   if (NULL != addresses)
111   {
112                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
113                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
114                 addresses = NULL ;
115   }*/
116   if (NULL != peers)
117         {
118                 GNUNET_free (peers);
119         }
120   if (NULL != mlp)
121   {
122                 GAS_mlp_done (mlp);
123                 mlp = NULL;
124   }
125
126         ret = res;
127 }
128
129
130 static void
131 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
132 {
133
134 }
135
136 static void
137 perf_create_peer (int cp)
138 {
139         GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &peers[cp].id.hashPubKey);
140         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp, GNUNET_i2s (&peers[cp].id));
141 }
142
143 static struct ATS_Address *
144 perf_create_address (int cp, int ca)
145 {
146         struct ATS_Address *a;
147         a = create_address (&peers[cp].id, "Test 1", "test 1", strlen ("test 1") + 1, 0);
148         GNUNET_CONTAINER_DLL_insert (peers[cp].head, peers[cp].tail, a);
149         GNUNET_CONTAINER_multihashmap_put (addresses, &peers[cp].id.hashPubKey, a, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
150         return a;
151 }
152
153
154
155
156
157 static void
158 check (void *cls, char *const *args, const char *cfgfile,
159        const struct GNUNET_CONFIGURATION_Handle *cfg)
160 {
161   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
162   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
163   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
164         int cp;
165         int ca;
166         struct ATS_Address * cur_addr;
167
168   stats = GNUNET_STATISTICS_create("ats", cfg);
169   if (NULL == stats)
170   {
171         GNUNET_break (0);
172     end_now (1);
173     return;
174   }
175
176   /* Load quotas */
177   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
178                         GNUNET_ATS_NetworkTypeCount))
179   {
180         GNUNET_break (0);
181       end_now (1);
182       return;
183   }
184
185   GNUNET_assert (N_peers_end >= N_peers_start);
186   GNUNET_assert (N_address >= 0);
187
188   if ((0 == N_peers_start) && (0 == N_peers_end))
189   {
190                 N_peers_start = PEERS_START;
191                 N_peers_end = PEERS_END;
192   }
193   if (0 == N_address)
194                 N_address = ADDRESSES;
195
196   fprintf (stderr, "Solving problem for %u..%u peers with %u addresses\n",
197                 N_peers_start, N_peers_end, N_address);
198
199   count_p = N_peers_end;
200   count_a = N_address;
201   peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
202   /* Setup address hashmap */
203   addresses = GNUNET_CONTAINER_multihashmap_create (N_address, GNUNET_NO);
204
205   /* Init MLP solver */
206   mlp  = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
207                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
208   if (NULL == mlp)
209   {
210         GNUNET_break (0);
211       end_now (1);
212       return;
213   }
214   mlp->mlp_auto_solve = GNUNET_NO;
215
216         for (cp = 0; cp < count_p; cp++)
217                         perf_create_peer (cp);
218
219         for (cp = 0; cp < count_p; cp++)
220         {
221                         for (ca = 0; ca < count_a; ca++)
222                         {
223                                         cur_addr = perf_create_address(cp, ca);
224                                         /* add address */
225                                         GAS_mlp_address_add (mlp, addresses, cur_addr);
226                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer %u address %u: \n", cp, ca);
227                         }
228                         GAS_mlp_get_preferred_address( mlp, addresses, &peers[cp].id);
229                         /* solve */
230                         if (cp + 1 >= N_peers_start)
231                         {
232
233                                 GAS_mlp_solve_problem (mlp, addresses);
234                                 fprintf (stderr, "%u peers each %u addresses; state [%s/%s], (build/LP/MIP in ms): %04llu %04llu %04llu; presolv LP/MIP [%s/%s]; size (cols x rows, nonzero elements): [%u x %u] = %u\n",
235                                                         cp + 1, ca,
236                                                         (GNUNET_OK == mlp->ps.lp_res) ? "OK" : "FAIL",
237                                                         (GNUNET_OK == mlp->ps.mip_res) ? "OK" : "FAIL",
238                                                         (unsigned long long) mlp->ps.build_dur.rel_value,
239                                                         (unsigned long long) mlp->ps.lp_dur.rel_value,
240                                                         (unsigned long long) mlp->ps.mip_dur.rel_value,
241                                                         (GLP_YES == mlp->ps.lp_presolv) ? "YES" : "NO",
242                                                         (GNUNET_OK == mlp->ps.mip_presolv) ? "YES" : "NO",
243                                                         mlp->ps.p_cols, mlp->ps.p_rows, mlp->ps.p_elements);
244                         }
245
246         }
247
248
249         struct ATS_Address *cur;
250         struct ATS_Address *next;
251         for (cp = 0; cp < count_p; cp++)
252         {
253                         for (cur = peers[cp].head; cur != NULL; cur = next)
254                         {
255                                         GAS_mlp_address_delete (mlp, addresses, cur, GNUNET_NO);
256                                         next = cur->next;
257                                         GNUNET_CONTAINER_DLL_remove (peers[cp].head, peers[cp].tail, cur);
258                                         GNUNET_free (cur);
259                         }
260
261         }
262         GNUNET_free (peers);
263
264 }
265
266
267 int
268 main (int argc, char *argv[])
269 {
270
271   static char *const argv2[] = { "perf_ats_mlp",
272     "-c",
273     "test_ats_mlp.conf",
274     "-L", "WARNING",
275     NULL
276   };
277
278   N_peers_start = 0;
279   N_peers_end = 0;
280   N_address = 0;
281   int c;
282   for (c = 0; c < argc; c++)
283   {
284                 if ((0 == strcmp (argv[c], "-q")) && (c < argc))
285                 {
286                                 if (0 != atoi(argv[c+1]))
287                                 {
288                                                 N_peers_start = atoi(argv[c+1]);
289                                 }
290                 }
291                 if ((0 == strcmp (argv[c], "-w")) && (c < argc))
292                 {
293                                 if (0 != atoi(argv[c+1]))
294                                 {
295                                                 N_peers_end = atoi(argv[c+1]);
296                                 }
297                 }
298                 if ((0 == strcmp (argv[c], "-a")) && (c < argc))
299                 {
300                                 if (0 != atoi(argv[c+1]))
301                                 {
302                                                 N_address = atoi(argv[c+1]);
303                                 }
304                 }
305   }
306
307   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
308     GNUNET_GETOPT_OPTION_END
309   };
310
311
312   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
313                       "perf_ats_mlp", "nohelp", options,
314                       &check, NULL);
315
316
317   return ret;
318 }
319
320 /* end of file perf_ats_mlp.c */