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