changes
[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 static int opt_numeric;
80 static int opt_dump;
81
82 static int N_peers_start;
83 static int N_peers_end;
84 static int N_address;
85
86 /**
87  * Statistics handle
88  */
89 struct GNUNET_STATISTICS_Handle * stats;
90
91 /**
92  * MLP solver handle
93  */
94 struct GAS_MLP_Handle *mlp;
95
96 /**
97  * Hashmap containing addresses
98  */
99 struct GNUNET_CONTAINER_MultiHashMap * addresses;
100
101 struct PerfPeer *peers;
102
103 static void
104 end_now (int res)
105 {
106   if (NULL != stats)
107   {
108           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
109           stats = NULL;
110   }
111   /*
112   if (NULL != addresses)
113   {
114                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
115                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
116                 addresses = NULL ;
117   }*/
118   if (NULL != peers)
119         {
120                 GNUNET_free (peers);
121         }
122   if (NULL != mlp)
123   {
124                 GAS_mlp_done (mlp);
125                 mlp = NULL;
126   }
127
128         ret = res;
129 }
130
131
132 static void
133 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
134 {
135
136 }
137
138 static void
139 perf_create_peer (int cp)
140 {
141         GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &peers[cp].id.hashPubKey);
142         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp, GNUNET_i2s (&peers[cp].id));
143 }
144
145 static struct ATS_Address *
146 perf_create_address (int cp, int ca)
147 {
148         struct ATS_Address *a;
149         a = create_address (&peers[cp].id, "Test 1", "test 1", strlen ("test 1") + 1, 0);
150         GNUNET_CONTAINER_DLL_insert (peers[cp].head, peers[cp].tail, a);
151         GNUNET_CONTAINER_multihashmap_put (addresses, &peers[cp].id.hashPubKey, a, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
152         return a;
153 }
154
155
156
157
158
159 static void
160 check (void *cls, char *const *args, const char *cfgfile,
161        const struct GNUNET_CONFIGURATION_Handle *cfg)
162 {
163   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
164   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
165   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
166         int cp;
167         int ca;
168         struct ATS_Address * cur_addr;
169
170   stats = GNUNET_STATISTICS_create("ats", cfg);
171   if (NULL == stats)
172   {
173         GNUNET_break (0);
174     end_now (1);
175     return;
176   }
177
178   /* Load quotas */
179   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
180                         GNUNET_ATS_NetworkTypeCount))
181   {
182         GNUNET_break (0);
183       end_now (1);
184       return;
185   }
186
187   GNUNET_assert (N_peers_end >= N_peers_start);
188   GNUNET_assert (N_address >= 0);
189
190   if ((0 == N_peers_start) && (0 == N_peers_end))
191   {
192                 N_peers_start = PEERS_START;
193                 N_peers_end = PEERS_END;
194   }
195   if (0 == N_address)
196                 N_address = ADDRESSES;
197
198   fprintf (stderr, "Solving problem for %u..%u peers with %u addresses\n",
199                 N_peers_start, N_peers_end, N_address);
200
201   count_p = N_peers_end;
202   count_a = N_address;
203   peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
204   /* Setup address hashmap */
205   addresses = GNUNET_CONTAINER_multihashmap_create (N_address, GNUNET_NO);
206
207   /* Init MLP solver */
208   mlp  = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
209                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
210   if (NULL == mlp)
211   {
212         GNUNET_break (0);
213       end_now (1);
214       return;
215   }
216   mlp->mlp_auto_solve = GNUNET_NO;
217   mlp->write_mip_mps = opt_dump;
218   mlp->write_mip_sol = opt_dump;
219
220         for (cp = 0; cp < count_p; cp++)
221                         perf_create_peer (cp);
222
223         if (GNUNET_YES == opt_numeric)
224                 fprintf (stderr, "#peers;#addresses per peer;LP/MIP state;presolv;exec build in ms;exec LP in ms; exec MIP in ms;#cols;#rows;#nonzero elements\n");
225
226         for (cp = 0; cp < count_p; cp++)
227         {
228                         for (ca = 0; ca < count_a; ca++)
229                         {
230                                         cur_addr = perf_create_address(cp, ca);
231                                         /* add address */
232                                         GAS_mlp_address_add (mlp, addresses, cur_addr);
233                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer %u address %u: \n", cp, ca);
234                         }
235                         GAS_mlp_get_preferred_address( mlp, addresses, &peers[cp].id);
236                         /* solve */
237                         if (cp + 1 >= N_peers_start)
238                         {
239
240                                 GAS_mlp_solve_problem (mlp, addresses);
241                                 if (GNUNET_NO == opt_numeric)
242                                         fprintf (stderr, "%u peers each %u addresses;  LP/MIP state [%s/%s] presolv [%s/%s], (build/LP/MIP in ms): %04llu %04llu %04llu; size (cols x rows, nonzero elements): [%u x %u] = %u\n",
243                                                         cp + 1, ca,
244                                                         (GNUNET_OK == mlp->ps.lp_res) ? "OK" : "FAIL",
245                                                         (GNUNET_OK == mlp->ps.mip_res) ? "OK" : "FAIL",
246                                                         (GLP_YES == mlp->ps.lp_presolv) ? "YES" : "NO",
247                                                         (GNUNET_OK == mlp->ps.mip_presolv) ? "YES" : "NO",
248                                                         (unsigned long long) mlp->ps.build_dur.rel_value,
249                                                         (unsigned long long) mlp->ps.lp_dur.rel_value,
250                                                         (unsigned long long) mlp->ps.mip_dur.rel_value,
251                                                         mlp->ps.p_cols, mlp->ps.p_rows, mlp->ps.p_elements);
252                                 else
253                                         fprintf (stderr, "%u;%u;%s;%s;%s;%s;%04llu;%04llu;%04llu;%u;%u;%u\n",
254                                                         cp + 1, ca,
255                                                         (GNUNET_OK == mlp->ps.lp_res) ? "OK" : "FAIL",
256                                                         (GNUNET_OK == mlp->ps.mip_res) ? "OK" : "FAIL",
257                                                         (GLP_YES == mlp->ps.lp_presolv) ? "YES" : "NO",
258                                                         (GNUNET_OK == mlp->ps.mip_presolv) ? "YES" : "NO",
259                                                         (unsigned long long) mlp->ps.build_dur.rel_value,
260                                                         (unsigned long long) mlp->ps.lp_dur.rel_value,
261                                                         (unsigned long long) mlp->ps.mip_dur.rel_value,
262                                                         mlp->ps.p_cols, mlp->ps.p_rows, mlp->ps.p_elements);
263                         }
264
265         }
266
267
268         struct ATS_Address *cur;
269         struct ATS_Address *next;
270         for (cp = 0; cp < count_p; cp++)
271         {
272                         for (cur = peers[cp].head; cur != NULL; cur = next)
273                         {
274                                         GAS_mlp_address_delete (mlp, addresses, cur, GNUNET_NO);
275                                         next = cur->next;
276                                         GNUNET_CONTAINER_DLL_remove (peers[cp].head, peers[cp].tail, cur);
277                                         GNUNET_free (cur);
278                         }
279
280         }
281         GNUNET_free (peers);
282
283 }
284
285
286 int
287 main (int argc, char *argv[])
288 {
289
290   static char *const argv2[] = { "perf_ats_mlp",
291     "-c",
292     "test_ats_mlp.conf",
293     "-L", "WARNING",
294     NULL
295   };
296
297   opt_dump = GNUNET_NO;
298
299   N_peers_start = 0;
300   N_peers_end = 0;
301   N_address = 0;
302   int c;
303   for (c = 0; c < argc; c++)
304   {
305                 if ((0 == strcmp (argv[c], "-q")) && (c < argc))
306                 {
307                                 if (0 != atoi(argv[c+1]))
308                                 {
309                                                 N_peers_start = atoi(argv[c+1]);
310                                 }
311                 }
312                 if ((0 == strcmp (argv[c], "-w")) && (c < argc))
313                 {
314                                 if (0 != atoi(argv[c+1]))
315                                 {
316                                                 N_peers_end = atoi(argv[c+1]);
317                                 }
318                 }
319                 if ((0 == strcmp (argv[c], "-a")) && (c < argc))
320                 {
321                                 if (0 != atoi(argv[c+1]))
322                                 {
323                                                 N_address = atoi(argv[c+1]);
324                                 }
325                 }
326                 if ((0 == strcmp (argv[c], "-n")))
327                 {
328                                 opt_numeric = GNUNET_YES;
329                 }
330                 if ((0 == strcmp (argv[c], "-d")))
331                 {
332                                 opt_dump = GNUNET_YES;
333                 }
334   }
335
336   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
337     GNUNET_GETOPT_OPTION_END
338   };
339
340
341   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
342                       "perf_ats_mlp", "nohelp", options,
343                       &check, NULL);
344
345
346   return ret;
347 }
348
349 /* end of file perf_ats_mlp.c */