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