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