missing file
[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 "gnunet-service-ats_normalization.h"
60 #include "test_ats_api_common.h"
61
62 #define PEERS_START 100
63 #define PEERS_END       100
64
65 #define ADDRESSES 10
66
67 int count_p;
68 int count_a;
69
70
71 struct PerfPeer
72 {
73         struct GNUNET_PeerIdentity id;
74
75         struct ATS_Address *head;
76         struct ATS_Address *tail;
77 };
78
79 static int ret;
80 static int opt_numeric;
81 static int opt_dump;
82 static int opt_update_percent;
83 static int opt_update_quantity;
84
85 static int N_peers_start;
86 static int N_peers_end;
87 static int N_address;
88
89 /**
90  * Statistics handle
91  */
92 struct GNUNET_STATISTICS_Handle * stats;
93
94 /**
95  * MLP solver handle
96  */
97 struct GAS_MLP_Handle *mlp;
98
99 /**
100  * Hashmap containing addresses
101  */
102 struct GNUNET_CONTAINER_MultiHashMap * addresses;
103
104 #define ATS_COUNT 2
105 struct GNUNET_ATS_Information ats[2];
106
107 struct PerfPeer *peers;
108
109 static void
110 end_now (int res)
111 {
112   if (NULL != stats)
113   {
114           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
115           stats = NULL;
116   }
117   /*
118   if (NULL != addresses)
119   {
120                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
121                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
122                 addresses = NULL ;
123   }*/
124   if (NULL != peers)
125         {
126                 GNUNET_free (peers);
127         }
128   if (NULL != mlp)
129   {
130                 GAS_mlp_done (mlp);
131                 mlp = NULL;
132   }
133   GAS_normalization_stop();
134         ret = res;
135 }
136
137
138 static void
139 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
140 {
141
142 }
143
144 static void
145 perf_create_peer (int cp)
146 {
147         GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &peers[cp].id.hashPubKey);
148         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp, GNUNET_i2s (&peers[cp].id));
149 }
150
151 static struct ATS_Address *
152 perf_create_address (int cp, int ca)
153 {
154         struct ATS_Address *a;
155         a = create_address (&peers[cp].id, "Test 1", "test 1", strlen ("test 1") + 1, 0);
156         GNUNET_CONTAINER_DLL_insert (peers[cp].head, peers[cp].tail, a);
157         GNUNET_CONTAINER_multihashmap_put (addresses, &peers[cp].id.hashPubKey, a, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
158         return a;
159 }
160
161 static void
162 address_initial_update (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
163 {
164         ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
165         ats[0].value = htonl (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 100));
166
167         ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
168         ats[1].value = htonl (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 10));
169
170         GAS_mlp_address_update (mlp, addresses, address, 0, GNUNET_YES, ats, 2);
171 }
172
173
174 static void
175 update_single_addresses (struct ATS_Address *cur)
176 {
177         int r_type;
178         int r_val;
179
180         r_type = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
181         switch (r_type) {
182                 case 0:
183                         r_val = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 100);
184                         ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
185                         ats[0].value = htonl (r_val);
186                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' address %p type %s val %u\n",
187                                         GNUNET_i2s (&cur->peer), cur,
188                                         "GNUNET_ATS_QUALITY_NET_DELAY", r_val);
189                         break;
190                 case 1:
191                         r_val = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 10);
192                         ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
193                         ats[0].value = htonl (r_val);
194                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s' address %p type %s val %u\n",
195                                         GNUNET_i2s (&cur->peer), cur,
196                                         "GNUNET_ATS_QUALITY_NET_DISTANCE", r_val);
197                         break;
198                 default:
199                         break;
200         }
201         GAS_mlp_address_update (mlp, addresses, cur, 0, GNUNET_YES, ats, 1);
202 }
203
204 static void
205 update_addresses (unsigned int cp, unsigned int ca, unsigned int up_q)
206 {
207         struct ATS_Address *cur;
208         int c_peer;
209         int c_select;
210         int c_addr;
211         int r;
212
213         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating addresses %u addresses per peer \n", up_q);
214         unsigned int m [ca];
215
216         for (c_peer = 0; c_peer < cp; c_peer++)
217         {
218                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s'\n", GNUNET_i2s (&peers[c_peer].id));
219                         for (c_select = 0; c_select < ca; c_select++)
220                                 m[c_select] = 0;
221                         c_select = 0;
222                         while (c_select < opt_update_quantity)
223                         {
224                                         r = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, ca);
225                                         if (0 == m[r])
226                                         {
227                                                 m[r] = 1;
228                                                 c_select++;
229                                         }
230                         }
231
232                         c_addr = 0;
233                         for (cur = peers[c_peer].head; NULL != cur; cur = cur->next)
234                         {
235                                         if (1 == m[c_addr])
236                                                         update_single_addresses (cur);
237                                         c_addr ++;
238                         }
239         }
240 }
241
242
243 static void
244 check (void *cls, char *const *args, const char *cfgfile,
245        const struct GNUNET_CONFIGURATION_Handle *cfg)
246 {
247   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
248   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
249   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
250         int cp;
251         int ca;
252         struct ATS_Address * cur_addr;
253
254         int full_lp_res;
255         int full_mip_res;
256         int full_lp_presolv;
257         int full_mip_presolv;
258         struct GNUNET_TIME_Relative full_build_dur;
259         struct GNUNET_TIME_Relative full_lp_dur;
260         struct GNUNET_TIME_Relative full_mip_dur;
261
262         int update_lp_res;
263         int update_mip_res;
264         int update_lp_presolv;
265         int update_mip_presolv;
266         struct GNUNET_TIME_Relative update_build_dur;
267         struct GNUNET_TIME_Relative update_lp_dur;
268         struct GNUNET_TIME_Relative update_mip_dur;
269
270   stats = GNUNET_STATISTICS_create("ats", cfg);
271   if (NULL == stats)
272   {
273         GNUNET_break (0);
274     end_now (1);
275     return;
276   }
277   GAS_normalization_start (NULL, NULL);
278   /* Load quotas */
279   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
280                         GNUNET_ATS_NetworkTypeCount))
281   {
282         GNUNET_break (0);
283       end_now (1);
284       return;
285   }
286
287   GNUNET_assert (N_peers_end >= N_peers_start);
288   GNUNET_assert (N_address >= 0);
289
290   fprintf (stderr, "Solving problem for %u..%u peers with %u addresses\n",
291                 N_peers_start, N_peers_end, N_address);
292
293   count_p = N_peers_end;
294   count_a = N_address;
295   peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
296   /* Setup address hashmap */
297   addresses = GNUNET_CONTAINER_multihashmap_create (N_address, GNUNET_NO);
298
299   /* Init MLP solver */
300   mlp  = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
301                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
302   if (NULL == mlp)
303   {
304         GNUNET_break (0);
305       end_now (1);
306       return;
307   }
308   mlp->mlp_auto_solve = GNUNET_NO;
309   mlp->write_mip_mps = opt_dump;
310   mlp->write_mip_sol = opt_dump;
311
312         for (cp = 0; cp < count_p; cp++)
313                         perf_create_peer (cp);
314
315         if (GNUNET_YES == opt_numeric)
316                 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");
317
318         for (cp = 0; cp < count_p; cp++)
319         {
320                         for (ca = 0; ca < count_a; ca++)
321                         {
322                                         cur_addr = perf_create_address(cp, ca);
323                                         /* add address */
324                                         GAS_mlp_address_add (mlp, addresses, cur_addr, GNUNET_ATS_NET_UNSPECIFIED);
325                                         address_initial_update (mlp, addresses, cur_addr);
326                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address for peer %u address %u: \n", cp, ca);
327                         }
328                         GAS_mlp_get_preferred_address( mlp, addresses, &peers[cp].id);
329                         /* solve */
330                         if (cp + 1 >= N_peers_start)
331                         {
332                                 /* Solve the full problem */
333                                 GAS_mlp_solve_problem (mlp, addresses);
334                                 full_lp_res = mlp->ps.lp_res;
335                                 full_mip_res = mlp->ps.mip_res;
336                                 full_lp_presolv = mlp->ps.lp_presolv;
337                                 full_mip_presolv = mlp->ps.mip_presolv;
338                                 full_build_dur = mlp->ps.build_dur;
339                                 full_lp_dur = mlp->ps.lp_dur;
340                                 full_mip_dur = mlp->ps.mip_dur;
341
342                                 /* Update and solve the problem */
343                                 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
344                                 {
345                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
346                                         update_addresses (cp + 1, ca, opt_update_quantity);
347                                         GAS_mlp_solve_problem (mlp, addresses);
348                                         GAS_mlp_solve_problem (mlp, addresses);
349                                         update_lp_res = mlp->ps.lp_res;
350                                         update_mip_res = mlp->ps.mip_res;
351                                         update_lp_presolv = mlp->ps.lp_presolv;
352                                         update_mip_presolv = mlp->ps.mip_presolv;
353                                         update_build_dur = mlp->ps.build_dur;
354                                         update_lp_dur = mlp->ps.lp_dur;
355                                         update_mip_dur = mlp->ps.mip_dur;
356
357                                 }
358                                 if (GNUNET_NO == opt_numeric)
359                                 {
360                                 fprintf (stderr, "Rebuild: %03u peers each %02u addresses; rebuild: LP/MIP state [%3s/%3s] presolv [%3s/%3s], (build/LP/MIP in ms) %04llu / %04llu / %04llu\n",
361                                                 cp + 1, ca,
362                                                 (GNUNET_OK == full_lp_res) ? "OK" : "FAIL",
363                                                 (GNUNET_OK == full_mip_res) ? "OK" : "FAIL",
364                                                 (GLP_YES == full_lp_presolv) ? "YES" : "NO",
365                                                 (GNUNET_OK == full_mip_presolv) ? "YES" : "NO",
366                                                 (unsigned long long) full_build_dur.rel_value, (unsigned long long) full_lp_dur.rel_value, (unsigned long long) full_mip_dur.rel_value);
367                                 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
368                                         fprintf (stderr, "Update : %03u peers each %02u addresses; rebuild: LP/MIP state [%3s/%3s] presolv [%3s/%3s], (build/LP/MIP in ms) %04llu / %04llu / %04llu\n",
369                                                 cp + 1, ca,
370                                                 (GNUNET_OK == update_lp_res) ? "OK" : "FAIL",
371                                                 (GNUNET_OK == update_mip_res) ? "OK" : "FAIL",
372                                                 (GLP_YES == update_lp_presolv) ? "YES" : "NO",
373                                                 (GNUNET_OK == update_mip_presolv) ? "YES" : "NO",
374                                                 (unsigned long long) update_build_dur.rel_value, (unsigned long long) update_lp_dur.rel_value, (unsigned long long) update_mip_dur.rel_value);
375                                 }
376                                 else
377                                 {
378                                                 fprintf (stderr, "Rebuild;%u;%u;%s;%s;%s;%s;%04llu;%04llu;%04llu\n",
379                                                                 cp + 1, ca,
380                                                                 (GNUNET_OK == full_lp_res) ? "OK" : "FAIL",
381                                                                 (GNUNET_OK == full_mip_res) ? "OK" : "FAIL",
382                                                                 (GLP_YES == full_lp_presolv) ? "YES" : "NO",
383                                                                 (GNUNET_OK == full_mip_presolv) ? "YES" : "NO",
384                                                                 (unsigned long long) full_build_dur.rel_value, (unsigned long long) full_lp_dur.rel_value, (unsigned long long) full_mip_dur.rel_value);
385                                                 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
386                                                         fprintf (stderr, "Update;%u;%u;%s;%s;%s;%s;%04llu;%04llu;%04llu\n",
387                                                                         cp + 1, ca,
388                                                                         (GNUNET_OK == update_lp_res) ? "OK" : "FAIL",
389                                                                         (GNUNET_OK == update_mip_res) ? "OK" : "FAIL",
390                                                                         (GLP_YES == update_lp_presolv) ? "YES" : "NO",
391                                                                         (GNUNET_OK == update_mip_presolv) ? "YES" : "NO",
392                                                                         (unsigned long long) update_build_dur.rel_value, (unsigned long long) update_lp_dur.rel_value, (unsigned long long) update_mip_dur.rel_value);
393                                 }
394                         }
395         }
396
397
398         struct ATS_Address *cur;
399         struct ATS_Address *next;
400         for (cp = 0; cp < count_p; cp++)
401         {
402                         for (cur = peers[cp].head; cur != NULL; cur = next)
403                         {
404                                         GAS_mlp_address_delete (mlp, addresses, cur, GNUNET_NO);
405                                         next = cur->next;
406                                         GNUNET_CONTAINER_DLL_remove (peers[cp].head, peers[cp].tail, cur);
407                                         GNUNET_free (cur);
408                         }
409
410         }
411         GNUNET_free (peers);
412
413 }
414
415
416 int
417 main (int argc, char *argv[])
418 {
419
420   static char *const argv2[] = { "perf_ats_mlp",
421     "-c",
422     "test_ats_mlp.conf",
423     "-L", "WARNING",
424     NULL
425   };
426
427   opt_dump = GNUNET_NO;
428   opt_update_quantity = 0;
429   opt_update_percent = 0;
430
431   N_peers_start = 0;
432   N_peers_end = 0;
433   N_address = 0;
434   int c;
435   for (c = 0; c < argc; c++)
436   {
437                 if ((0 == strcmp (argv[c], "-z")) && (c < (argc - 1)))
438                 {
439                                 if (0 != atoi(argv[c+1]))
440                                 {
441                                                 N_peers_start = atoi(argv[c+1]);
442                                 }
443                 }
444                 if ((0 == strcmp (argv[c], "-x")) && (c < (argc - 1)))
445                 {
446                                 if (0 != atoi(argv[c+1]))
447                                 {
448                                                 N_peers_end = atoi(argv[c+1]);
449                                 }
450                 }
451                 if ((0 == strcmp (argv[c], "-c")) && (c < (argc - 1)))
452                 {
453                                 if (0 != atoi(argv[c+1]))
454                                 {
455                                                 N_address = atoi(argv[c+1]);
456                                 }
457                 }
458                 if ((0 == strcmp (argv[c], "-n")))
459                 {
460                                 opt_numeric = GNUNET_YES;
461                 }
462                 if ((0 == strcmp (argv[c], "-d")))
463                 {
464                                 opt_dump = GNUNET_YES;
465                 }
466                 if ((0 == strcmp (argv[c], "-p")) && (c < (argc - 1)))
467                 {
468                                 if (0 != atoi(argv[c+1]))
469                                 {
470                                         /* Update a fix "p"ercentage of addresses */
471                                                 opt_update_percent = atoi(argv[c+1]);
472                                                 if ((0 <= opt_update_percent) && (100 <= opt_update_percent))
473                                                 {
474                                                         fprintf (stderr, _("Percentage has to be: 0 <= p <= 100 "));
475                                                         exit (1);
476                                                 }
477                                 }
478                 }
479                 if ((0 == strcmp (argv[c], "-q")) && (c < (argc - 1)))
480                 {
481                                 if (0 != atoi(argv[c+1]))
482                                 {
483                                         /* Update a fix "q"uantity of addresses */
484                                                 opt_update_quantity = atoi(argv[c+1]);
485                                                 if (0 >= opt_update_quantity)
486                                                 {
487                                                         fprintf (stderr, _("Quantity has to be:  p => 0 "));
488                                                         exit (1);
489                                                 }
490                                 }
491                 }
492   }
493
494   if ((0 == N_peers_start) && (0 == N_peers_end))
495   {
496                 N_peers_start = PEERS_START;
497                 N_peers_end = PEERS_END;
498   }
499   if (0 == N_address)
500                 N_address = ADDRESSES;
501
502   if (opt_update_quantity > N_address)
503   {
504                 fprintf (stderr, _("Trying to update more addresses than we have per peer! (%u vs %u)"), opt_update_quantity, N_address);
505                 exit (1);
506   }
507
508   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
509     GNUNET_GETOPT_OPTION_END
510   };
511
512
513   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
514                       "perf_ats_mlp", "nohelp", options,
515                       &check, NULL);
516
517
518   return ret;
519 }
520
521 /* end of file perf_ats_mlp.c */