2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file ats/perf_ats_mlp
22 * @brief performance test for the MLP solver
23 * @author Christian Grothoff
24 * @author Matthias Wachs
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_ats_service.h"
31 #include "gnunet_ats_plugin.h"
32 #include "test_ats_api_common.h"
34 #define PEERS_START 100
44 struct GNUNET_PeerIdentity id;
46 struct ATS_Address *head;
47 struct ATS_Address *tail;
51 static int opt_numeric;
53 static int opt_update_percent;
54 static int opt_update_quantity;
56 static int N_peers_start;
57 static int N_peers_end;
63 struct GNUNET_STATISTICS_Handle * stats;
68 struct GAS_MLP_Handle *mlp;
71 * Hashmap containing addresses
73 struct GNUNET_CONTAINER_MultiHashMap * addresses;
76 struct GNUNET_ATS_Information ats[2];
78 struct PerfPeer *peers;
85 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
89 if (NULL != addresses)
91 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
92 GNUNET_CONTAINER_multihashmap_destroy (addresses);
104 GAS_normalization_stop ();
109 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
115 normalized_property_changed_cb (void *cls, struct ATS_Address *peer,
116 uint32_t type, double prop_rel)
122 get_property_cb (void *cls, const struct ATS_Address *address)
124 return GAS_normalization_get_properties ((struct ATS_Address *) address);
127 static const double *
128 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
130 return GAS_normalization_get_preferences (id);
134 perf_create_peer (int cp)
136 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
137 &peers[cp].id.hashPubKey);
138 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp,
139 GNUNET_i2s (&peers[cp].id));
142 static struct ATS_Address *
143 perf_create_address (int cp, int ca)
145 struct ATS_Address *a;
146 a = create_address (&peers[cp].id, "Test 1", "test 1", strlen ("test 1") + 1,
148 GNUNET_CONTAINER_DLL_insert(peers[cp].head, peers[cp].tail, a);
149 GNUNET_CONTAINER_multihashmap_put (addresses, &peers[cp].id.hashPubKey, a,
150 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
155 address_initial_update (void *solver,
156 struct GNUNET_CONTAINER_MultiHashMap * addresses,
157 struct ATS_Address *address)
159 GAS_mlp_address_property_changed (mlp, address, GNUNET_ATS_QUALITY_NET_DELAY,
161 (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
164 GAS_mlp_address_property_changed (mlp, address,
165 GNUNET_ATS_QUALITY_NET_DISTANCE, 10,
166 (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
171 update_single_addresses (struct ATS_Address *cur)
176 r_type = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
180 r_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
182 "Updating peer `%s' address %p type %s val %u\n",
183 GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DELAY", r_val);
184 GAS_mlp_address_property_changed (mlp, cur, GNUNET_ATS_QUALITY_NET_DELAY,
185 r_val, (double) (100 + r_val / 100));
188 r_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
191 "Updating peer `%s' address %p type %s val %u\n",
192 GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DISTANCE", r_val);
193 GAS_mlp_address_property_changed (mlp, cur, GNUNET_ATS_QUALITY_NET_DISTANCE,
194 r_val, (double) (100 + r_val) / 100);
199 GAS_mlp_address_inuse_changed (mlp, cur, GNUNET_YES);
204 update_addresses (unsigned int cp, unsigned int ca, unsigned int up_q)
206 struct ATS_Address *cur;
212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
213 "Updating addresses %u addresses per peer \n", up_q);
216 for (c_peer = 0; c_peer < cp; c_peer++)
218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Updating peer `%s'\n",
219 GNUNET_i2s (&peers[c_peer].id));
220 for (c_select = 0; c_select < ca; c_select++)
223 while (c_select < opt_update_quantity)
225 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, ca);
234 for (cur = peers[c_peer].head; NULL != cur; cur = cur->next)
237 update_single_addresses (cur);
246 check (void *cls, char * const *args, const char *cfgfile,
247 const struct GNUNET_CONFIGURATION_Handle *cfg)
250 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
251 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
252 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
255 struct ATS_Address * cur_addr;
260 int full_mip_presolv;
261 struct GNUNET_TIME_Relative full_build_dur;
262 struct GNUNET_TIME_Relative full_lp_dur;
263 struct GNUNET_TIME_Relative full_mip_dur;
267 int update_lp_presolv;
268 int update_mip_presolv;
269 struct GNUNET_TIME_Relative update_build_dur;
270 struct GNUNET_TIME_Relative update_lp_dur;
271 struct GNUNET_TIME_Relative update_mip_dur;
273 stats = GNUNET_STATISTICS_create ("ats", cfg);
280 GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL );
282 if (GNUNET_ATS_NetworkTypeCount
283 != load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount))
290 GNUNET_assert(N_peers_end >= N_peers_start);
291 GNUNET_assert(N_address >= 0);
293 fprintf (stderr, "Solving problem for %u..%u peers with %u addresses\n",
294 N_peers_start, N_peers_end, N_address);
296 count_p = N_peers_end;
298 peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
299 /* Setup address hashmap */
300 addresses = GNUNET_CONTAINER_multihashmap_create (N_address, GNUNET_NO);
302 /* Init MLP solver */
303 mlp = GAS_mlp_init (cfg, stats, addresses, quotas, quotas_out, quotas_in,
304 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL,
305 &get_preferences_cb, NULL, &get_property_cb, NULL );
312 mlp->mlp_auto_solve = GNUNET_NO;
313 mlp->write_mip_mps = opt_dump;
314 mlp->write_mip_sol = opt_dump;
316 for (cp = 0; cp < count_p; cp++)
317 perf_create_peer (cp);
319 if (GNUNET_YES == opt_numeric)
321 "#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");
323 for (cp = 0; cp < count_p; cp++)
325 for (ca = 0; ca < count_a; ca++)
327 cur_addr = perf_create_address (cp, ca);
329 GAS_mlp_address_add (mlp, cur_addr, GNUNET_ATS_NET_UNSPECIFIED);
330 address_initial_update (mlp, addresses, cur_addr);
331 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
332 "Adding address for peer %u address %u: \n", cp, ca);
334 GAS_mlp_get_preferred_address (mlp, &peers[cp].id);
336 if (cp + 1 >= N_peers_start)
339 update_mip_dur = GNUNET_TIME_UNIT_FOREVER_REL;
340 update_lp_dur = GNUNET_TIME_UNIT_FOREVER_REL;
341 update_build_dur = GNUNET_TIME_UNIT_FOREVER_REL;
342 update_mip_presolv = GNUNET_SYSERR;
343 update_lp_presolv = GNUNET_SYSERR;
344 update_mip_res = GNUNET_SYSERR;
345 update_lp_res = GNUNET_SYSERR;
346 /* Solve the full problem */
347 GAS_mlp_solve_problem (mlp);
348 full_lp_res = mlp->ps.lp_res;
349 full_mip_res = mlp->ps.mip_res;
350 full_lp_presolv = mlp->ps.lp_presolv;
351 full_mip_presolv = mlp->ps.mip_presolv;
352 full_build_dur = mlp->ps.build_dur;
353 full_lp_dur = mlp->ps.lp_dur;
354 full_mip_dur = mlp->ps.mip_dur;
356 /* Update and solve the problem */
357 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
359 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
360 "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
361 update_addresses (cp + 1, ca, opt_update_quantity);
362 GAS_mlp_solve_problem (mlp);
363 GAS_mlp_solve_problem (mlp);
364 update_lp_res = mlp->ps.lp_res;
365 update_mip_res = mlp->ps.mip_res;
366 update_lp_presolv = mlp->ps.lp_presolv;
367 update_mip_presolv = mlp->ps.mip_presolv;
368 update_build_dur = mlp->ps.build_dur;
369 update_lp_dur = mlp->ps.lp_dur;
370 update_mip_dur = mlp->ps.mip_dur;
373 if (GNUNET_NO == opt_numeric)
376 "Rebuild: %03u peers each %02u addresses; rebuild: LP/MIP state [%3s/%3s] presolv [%3s/%3s], (build/LP/MIP in us) %04llu / %04llu / %04llu\n",
377 cp + 1, ca, (GNUNET_OK == full_lp_res) ? "OK" : "FAIL",
378 (GNUNET_OK == full_mip_res) ? "OK" : "FAIL",
379 (GLP_YES == full_lp_presolv) ? "YES" : "NO",
380 (GNUNET_OK == full_mip_presolv) ? "YES" : "NO",
381 (unsigned long long) full_build_dur.rel_value_us,
382 (unsigned long long) full_lp_dur.rel_value_us,
383 (unsigned long long) full_mip_dur.rel_value_us);
384 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
386 "Update: %03u peers each %02u addresses; rebuild: LP/MIP state [%3s/%3s] presolv [%3s/%3s], (build/LP/MIP in us) %04llu / %04llu / %04llu\n",
387 cp + 1, ca, (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_us,
392 (unsigned long long) update_lp_dur.rel_value_us,
393 (unsigned long long) update_mip_dur.rel_value_us);
397 fprintf (stderr, "Rebuild;%u;%u;%s;%s;%s;%s;%04llu;%04llu;%04llu\n",
398 cp + 1, ca, (GNUNET_OK == full_lp_res) ? "OK" : "FAIL",
399 (GNUNET_OK == full_mip_res) ? "OK" : "FAIL",
400 (GLP_YES == full_lp_presolv) ? "YES" : "NO",
401 (GNUNET_OK == full_mip_presolv) ? "YES" : "NO",
402 (unsigned long long) full_build_dur.rel_value_us,
403 (unsigned long long) full_lp_dur.rel_value_us,
404 (unsigned long long) full_mip_dur.rel_value_us);
405 if ((0 < opt_update_quantity) || (0 < opt_update_percent))
406 fprintf (stderr, "Update;%u;%u;%s;%s;%s;%s;%04llu;%04llu;%04llu\n",
407 cp + 1, ca, (GNUNET_OK == update_lp_res) ? "OK" : "FAIL",
408 (GNUNET_OK == update_mip_res) ? "OK" : "FAIL",
409 (GLP_YES == update_lp_presolv) ? "YES" : "NO",
410 (GNUNET_OK == update_mip_presolv) ? "YES" : "NO",
411 (unsigned long long) update_build_dur.rel_value_us,
412 (unsigned long long) update_lp_dur.rel_value_us,
413 (unsigned long long) update_mip_dur.rel_value_us);
418 struct ATS_Address *cur;
419 struct ATS_Address *next;
420 for (cp = 0; cp < count_p; cp++)
422 for (cur = peers[cp].head; cur != NULL ; cur = next)
424 GAS_mlp_address_delete (mlp, cur, GNUNET_NO);
426 GNUNET_CONTAINER_DLL_remove(peers[cp].head, peers[cp].tail, cur);
436 main (int argc, char *argv[])
439 char *src_filename = GNUNET_strdup (__FILE__);
440 char *test_filename = GNUNET_strdup (argv[0]);
444 static char * const argv2[] = { "perf_ats_mlp", "-c", "perf_ats_mlp.conf",
445 "-L", "WARNING", NULL };
447 opt_dump = GNUNET_NO;
448 opt_update_quantity = 0;
449 opt_update_percent = 0;
455 /* extract command line arguments */
456 for (c = 0; c < argc; c++)
458 if ((0 == strcmp (argv[c], "-z")) && (c < (argc - 1)))
460 if (0 != atoi (argv[c + 1]))
462 N_peers_start = atoi (argv[c + 1]);
465 if ((0 == strcmp (argv[c], "-x")) && (c < (argc - 1)))
467 if (0 != atoi (argv[c + 1]))
469 N_peers_end = atoi (argv[c + 1]);
472 if ((0 == strcmp (argv[c], "-c")) && (c < (argc - 1)))
474 if (0 != atoi (argv[c + 1]))
476 N_address = atoi (argv[c + 1]);
479 if ((0 == strcmp (argv[c], "-n")))
481 opt_numeric = GNUNET_YES;
483 if ((0 == strcmp (argv[c], "-d")))
485 opt_dump = GNUNET_YES;
487 if ((0 == strcmp (argv[c], "-p")) && (c < (argc - 1)))
489 if (0 != atoi (argv[c + 1]))
491 /* Update a fix "p"ercentage of addresses */
492 opt_update_percent = atoi (argv[c + 1]);
493 if ((0 <= opt_update_percent) && (100 <= opt_update_percent))
495 fprintf (stderr, _("Percentage has to be: 0 <= p <= 100 ") );
500 if ((0 == strcmp (argv[c], "-q")) && (c < (argc - 1)))
502 if (0 != atoi (argv[c + 1]))
504 /* Update a fix "q"uantity of addresses */
505 opt_update_quantity = atoi (argv[c + 1]);
506 if (0 >= opt_update_quantity)
508 fprintf (stderr, _("Quantity has to be: p => 0 ") );
515 if ((0 == N_peers_start) && (0 == N_peers_end))
517 N_peers_start = PEERS_START;
518 N_peers_end = PEERS_END;
521 N_address = ADDRESSES;
523 if (opt_update_quantity > N_address)
526 _("Trying to update more addresses than we have per peer! (%u vs %u)"),
527 opt_update_quantity, N_address);
531 /* Extract test name */
532 if (NULL == (sep = (strstr (src_filename,".c"))))
539 if (NULL != (sep = strstr (test_filename, ".exe")))
542 if (NULL == (solver = strstr (test_filename, src_filename)))
547 solver += strlen (src_filename) +1;
549 if (0 == strcmp(solver, "proportional"))
551 config_file = "perf_ats_solver_proportional.conf";
553 else if (0 == strcmp(solver, "mlp"))
555 config_file = "perf_ats_solver_mlp.conf";
557 else if ((0 == strcmp(solver, "ril")))
559 config_file = "perf_ats_solver_ril.conf";
564 GNUNET_free (src_filename);
565 GNUNET_free (test_filename);
569 GNUNET_free (src_filename);
570 GNUNET_free (test_filename);
572 fprintf (stderr, "Using cfg file `%s'\n",config_file);
575 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
576 GNUNET_GETOPT_OPTION_END };
578 GNUNET_PROGRAM_run ((sizeof(argv2) / sizeof(char *)) - 1, argv2,
579 "perf_ats", "nohelp", options, &check, NULL );
584 /* end of file perf_ats_mlp.c */