6f1a1a921fbbb08c6eef421cd96141251114cf8f
[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_solver.c
22  * @brief generic performance test for ATS solvers
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_normalization.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_ats_plugin.h"
33 #include "test_ats_api_common.h"
34
35 #define DEFAULT_UPDATE_PERCENTAGE       20
36 #define DEFAULT_PEERS_START     10
37 #define DEFAULT_PEERS_END       10
38 #define DEFAULT_ADDRESSES       10
39 #define DEFAULT_ATS_COUNT       2
40
41 #define GNUPLOT_PROP_TEMPLATE "#!/usr/bin/gnuplot \n" \
42 "set datafile separator ';' \n" \
43 "set title \"Execution time Proportional solver  \" \n" \
44 "set xlabel \"Number of peers\" \n" \
45 "set ylabel \"Execution time in us\" \n" \
46 "set grid \n"
47
48 #define GNUPLOT_PROP_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
49 "set datafile separator ';' \n" \
50 "set title \"Execution time Proportional solver with updated problem\" \n" \
51 "set xlabel \"Number of peers\" \n" \
52 "set ylabel \"Execution time in us\" \n" \
53 "set grid \n"
54
55 #define GNUPLOT_MLP_TEMPLATE "#!/usr/bin/gnuplot \n" \
56 "set datafile separator ';' \n" \
57 "set title \"Execution time MLP solver \" \n" \
58 "set xlabel \"Number of peers\" \n" \
59 "set ylabel \"Execution time in us\" \n" \
60 "set grid \n"
61
62 #define GNUPLOT_MLP_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
63 "set datafile separator ';' \n" \
64 "set title \"Execution time MLP solver with updated problem\" \n" \
65 "set xlabel \"Number of peers\" \n" \
66 "set ylabel \"Execution time in us\" \n" \
67 "set grid \n"
68
69 #define GNUPLOT_RIL_TEMPLATE "#!/usr/bin/gnuplot \n" \
70 "set datafile separator ';' \n" \
71 "set title \"Execution time RIL solver \" \n" \
72 "set xlabel \"Number of peers\" \n" \
73 "set ylabel \"Execution time in us\" \n" \
74 "set grid \n"
75
76 #define GNUPLOT_RIL_UPDATE_TEMPLATE "#!/usr/bin/gnuplot \n" \
77 "set datafile separator ';' \n" \
78 "set title \"Execution time RIL solver with updated problem\" \n" \
79 "set xlabel \"Number of peers\" \n" \
80 "set ylabel \"Execution time in us\" \n" \
81 "set grid \n"
82
83 /**
84  * Handle for ATS address component
85  */
86 struct PerfHandle
87 {
88   /**
89    * Performance peers
90    */
91   struct PerfPeer *peers;
92
93   /**
94    *  Solver handle
95    */
96   void *solver;
97
98   /**
99    * Statistics stat;
100    */
101   struct GNUNET_STATISTICS_Handle *stat;
102
103   /**
104    * A multihashmap to store all addresses
105    */
106   struct GNUNET_CONTAINER_MultiPeerMap *addresses;
107
108   /**
109    * Solver functions
110    * */
111   struct GNUNET_ATS_PluginEnvironment env;
112
113   /**
114    * Array for results for each iteration with length iterations
115    */
116   struct Iteration *iterations_results;
117
118   /**
119    * Array to store averaged result with length #peers
120    */
121   struct Result *averaged_result;
122
123   struct Result *current_result;
124
125   int current_p;
126   int current_a;
127
128   /**
129    * Solver description as string
130    */
131   char *ats_string;
132
133   /**
134    * Configured ATS solver
135    */
136   int ats_mode;
137
138   /**
139    * #peers to start benchmarking with
140    */
141   int N_peers_start;
142
143   /**
144    * #peers to end benchmarking with
145    */
146   int N_peers_end;
147
148   /**
149    * #addresses to benchmarking with
150    */
151   int N_address;
152
153   /**
154    * Percentage of peers to update
155    */
156   int opt_update_percent;
157
158   /**
159    * Create gnuplot file
160    */
161   int create_plot;
162
163   /**
164    * Measure updates
165    */
166   int measure_updates;
167
168   /**
169    * Number of iterations
170    */
171   int iterations;
172
173   /**
174    * Current iteration
175    */
176   int current_iteration;
177
178   /**
179    * Is a bulk operation running?
180    */
181   int bulk_running;
182
183   /**
184    * Is a bulk operation running?
185    */
186   int expecting_solution;
187 };
188
189 struct Iteration
190 {
191   struct Result *result_head;
192
193   struct Result *result_tail;
194 };
195
196 struct Result
197 {
198   struct Result *prev;
199   struct Result *next;
200
201   int peers;
202   int addresses;
203   int update;
204
205   enum GAS_Solver_Additional_Information info;
206
207   struct GNUNET_TIME_Relative d_setup;
208   struct GNUNET_TIME_Relative d_lp;
209   struct GNUNET_TIME_Relative d_mlp;
210   struct GNUNET_TIME_Relative d_total;
211
212   struct GNUNET_TIME_Absolute s_setup;
213   struct GNUNET_TIME_Absolute s_lp;
214   struct GNUNET_TIME_Absolute s_mlp;
215   struct GNUNET_TIME_Absolute s_total;
216
217   struct GNUNET_TIME_Absolute e_setup;
218   struct GNUNET_TIME_Absolute e_lp;
219   struct GNUNET_TIME_Absolute e_mlp;
220   struct GNUNET_TIME_Absolute e_total;
221 };
222
223 struct PerfPeer
224 {
225   struct GNUNET_PeerIdentity id;
226
227   struct ATS_Address *head;
228   struct ATS_Address *tail;
229 };
230
231 static struct PerfHandle ph;
232
233 /**
234  * Return value
235  */
236 static int ret;
237
238
239 /**
240  * ATS information
241  */
242 //static struct GNUNET_ATS_Information ats[2];
243
244
245 static void
246 end_now (int res)
247 {
248   if (NULL != ph.stat)
249   {
250     GNUNET_STATISTICS_destroy (ph.stat, GNUNET_NO);
251     ph.stat = NULL;
252   }
253   /*
254    if (NULL != addresses)
255    {
256    GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
257    GNUNET_CONTAINER_multihashmap_destroy (addresses);
258    addresses = NULL ;
259    }*/
260   if (NULL != ph.peers)
261   {
262     GNUNET_free(ph.peers);
263   }
264
265   GAS_normalization_stop ();
266   ret = res;
267 }
268
269
270 static void
271 perf_create_peer (int cp)
272 {
273
274   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
275       &ph.peers[cp].id, sizeof (struct GNUNET_PeerIdentity));
276   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating peer #%u: %s \n", cp,
277       GNUNET_i2s (&ph.peers[cp].id));
278 }
279
280
281
282 static void
283 perf_update_address (struct ATS_Address *cur)
284 {
285   int r_type;
286   int r_val;
287
288   r_type = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
289   switch (r_type)
290   {
291   case 0:
292     r_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
293     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
294         "Updating peer `%s' address %p type %s val %u\n",
295         GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DELAY", r_val);
296     ph.env.sf.s_address_update_property (ph.solver, cur,
297         GNUNET_ATS_QUALITY_NET_DELAY,
298         r_val, (double) (100 + r_val / 100));
299     break;
300   case 1:
301     r_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
302
303     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
304         "Updating peer `%s' address %p type %s val %u\n",
305         GNUNET_i2s (&cur->peer), cur, "GNUNET_ATS_QUALITY_NET_DISTANCE", r_val);
306     ph.env.sf.s_address_update_property (ph.solver, cur,
307         GNUNET_ATS_QUALITY_NET_DISTANCE,
308         r_val, (double) (100 + r_val) / 100);
309     break;
310   default:
311     break;
312   }
313   ph.env.sf.s_address_update_inuse (ph.solver, cur, GNUNET_YES);
314 }
315
316
317 static void
318 bandwidth_changed_cb (void *cls,
319                       struct ATS_Address *address)
320 {
321   if ( (0 == ntohl (address->assigned_bw_out.value__)) &&
322        (0 == ntohl (address->assigned_bw_in.value__)) )
323     return;
324
325   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326               "Bandwidth changed addresses %s %p to %u Bps out / %u Bps in\n",
327               GNUNET_i2s (&address->peer),
328               address,
329               (unsigned int) ntohl (address->assigned_bw_out.value__),
330               (unsigned int) ntohl (address->assigned_bw_in.value__));
331   if (GNUNET_YES == ph.bulk_running)
332     GNUNET_break (0);
333   return;
334 }
335
336
337 const double *
338 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
339 {
340   return GAS_normalization_get_preferences_by_peer (id);
341 }
342
343
344 const double *
345 get_property_cb (void *cls, const struct ATS_Address *address)
346 {
347   return GAS_normalization_get_properties ((struct ATS_Address *) address);
348 }
349
350 static void
351 normalized_property_changed_cb (void *cls, struct ATS_Address *peer,
352     uint32_t type, double prop_rel)
353 {
354   /* TODO */
355 }
356
357 static void
358 perf_address_initial_update (void *solver,
359     struct GNUNET_CONTAINER_MultiPeerMap * addresses,
360     struct ATS_Address *address)
361 {
362   ph.env.sf.s_address_update_property (solver, address, GNUNET_ATS_QUALITY_NET_DELAY,
363       100,
364       (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
365           / 100);
366
367   ph.env.sf.s_address_update_property (solver, address,
368       GNUNET_ATS_QUALITY_NET_DISTANCE, 10,
369       (double) (100 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100))
370           / 100);
371 }
372
373 static void
374 perf_update_all_addresses (unsigned int cp, unsigned int ca, unsigned int percentage_peers)
375 {
376   struct ATS_Address *cur_address;
377   int c_peer;
378   int c_select;
379   int c_cur_p;
380   int c_cur_a;
381   int r;
382   int count;
383   unsigned int m[cp];
384
385   count = cp * ((double) percentage_peers / 100);
386   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
387       "Updating %u of %u peers \n", count, cp);
388
389   for (c_peer = 0; c_peer < cp; c_peer++)
390     m[c_peer] = 0;
391
392   c_select = 0;
393
394   while (c_select < count)
395   {
396     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, cp);
397     if (0 == m[r])
398     {
399       m[r] = 1;
400       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
401           "Updating peer [%u] \n", r);
402       c_select++;
403     }
404   }
405   for (c_cur_p = 0; c_cur_p < cp; c_cur_p++)
406   {
407     if (1 == m[c_cur_p])
408     {
409       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, ca);
410       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
411           "Updating peer [%u] address [%u]\n", c_cur_p, r);
412
413       c_cur_a = 0;
414       for (cur_address = ph.peers[c_cur_p].head; NULL != cur_address; cur_address = cur_address->next)
415       {
416         if (c_cur_a == r)
417           perf_update_address (cur_address);
418
419         c_cur_a ++;
420       }
421     }
422   }
423 }
424
425
426 static struct ATS_Address *
427 perf_create_address (int cp, int ca)
428 {
429   struct ATS_Address *a;
430   a = create_address (&ph.peers[cp].id,
431       "Test 1", "test 1", strlen ("test 1") + 1, 0);
432   GNUNET_CONTAINER_DLL_insert (ph.peers[cp].head, ph.peers[cp].tail, a);
433   GNUNET_CONTAINER_multipeermap_put (ph.addresses, &ph.peers[cp].id, a,
434       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
435   return a;
436 }
437
438 static void
439 solver_info_cb (void *cls,
440     enum GAS_Solver_Operation op,
441     enum GAS_Solver_Status stat,
442     enum GAS_Solver_Additional_Information add)
443 {
444   char *add_info;
445   switch (add) {
446     case GAS_INFO_NONE:
447       add_info = "GAS_INFO_NONE";
448       break;
449     case GAS_INFO_FULL:
450       add_info = "GAS_INFO_MLP_FULL";
451       break;
452     case GAS_INFO_UPDATED:
453       add_info = "GAS_INFO_MLP_UPDATED";
454       break;
455     case GAS_INFO_PROP_ALL:
456       add_info = "GAS_INFO_PROP_ALL";
457       break;
458     case GAS_INFO_PROP_SINGLE:
459       add_info = "GAS_INFO_PROP_SINGLE";
460       break;
461     default:
462       add_info = "INVALID";
463       break;
464   }
465
466   struct Result *tmp;
467   switch (op)
468   {
469     case GAS_OP_SOLVE_START:
470       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
471           "Solver notifies `%s' with result `%s' `%s' in iteration %u \n", "GAS_OP_SOLVE_START",
472           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info, ph.current_iteration);
473       if (GNUNET_NO == ph.expecting_solution)
474       {
475         /* We do not expect a solution at the moment */
476         GNUNET_break (0);
477         return;
478       }
479
480       if ((GAS_STAT_SUCCESS == stat) && (NULL == ph.current_result))
481       {
482         /* Create new result */
483         tmp = GNUNET_new (struct Result);
484         ph.current_result = tmp;
485         GNUNET_CONTAINER_DLL_insert_tail(ph.iterations_results[ph.current_iteration-1].result_head,
486             ph.iterations_results[ph.current_iteration-1].result_tail, tmp);
487         ph.current_result->addresses = ph.current_a;
488         ph.current_result->peers = ph.current_p;
489         ph.current_result->s_total = GNUNET_TIME_absolute_get();
490         ph.current_result->d_total = GNUNET_TIME_UNIT_FOREVER_REL;
491         ph.current_result->d_setup = GNUNET_TIME_UNIT_FOREVER_REL;
492         ph.current_result->d_lp = GNUNET_TIME_UNIT_FOREVER_REL;
493         ph.current_result->d_mlp = GNUNET_TIME_UNIT_FOREVER_REL;
494         ph.current_result->info = add;
495         if (add == GAS_INFO_UPDATED)
496           ph.current_result->update = GNUNET_YES;
497         else
498           ph.current_result->update = GNUNET_NO;
499       }
500       return;
501     case GAS_OP_SOLVE_STOP:
502       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
503           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
504           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL", add_info);
505       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
506       {
507         /* We do not expect a solution at the moment */
508         GNUNET_break (0);
509         return;
510       }
511       if (NULL != ph.current_result)
512       {
513         /* Finalize result */
514         ph.current_result->e_total = GNUNET_TIME_absolute_get ();
515         ph.current_result->d_total = GNUNET_TIME_absolute_get_difference (
516             ph.current_result->s_total, ph.current_result->e_total);
517       }
518       ph.current_result = NULL;
519       return;
520
521     case GAS_OP_SOLVE_SETUP_START:
522       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
523           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
524           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
525       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
526       {
527         GNUNET_break(0);
528         return;
529       }
530       ph.current_result->s_setup = GNUNET_TIME_absolute_get ();
531       return;
532
533     case GAS_OP_SOLVE_SETUP_STOP:
534       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
535           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
536           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
537       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
538       {
539         GNUNET_break(0);
540         return;
541       }
542       ph.current_result->e_setup = GNUNET_TIME_absolute_get ();
543       ph.current_result->d_setup = GNUNET_TIME_absolute_get_difference (
544           ph.current_result->s_setup, ph.current_result->e_setup);
545       return;
546
547     case GAS_OP_SOLVE_MLP_LP_START:
548       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
549           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
550           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
551       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
552       {
553         GNUNET_break(0);
554         return;
555       }
556       ph.current_result->s_lp = GNUNET_TIME_absolute_get ();
557       return;
558     case GAS_OP_SOLVE_MLP_LP_STOP:
559       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
560           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
561           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
562       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
563       {
564         GNUNET_break(0);
565         return;
566       }
567       ph.current_result->e_lp = GNUNET_TIME_absolute_get ();
568       ph.current_result->d_lp = GNUNET_TIME_absolute_get_difference (
569           ph.current_result->s_lp, ph.current_result->e_lp);
570       return;
571
572     case GAS_OP_SOLVE_MLP_MLP_START:
573       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
574           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
575           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
576       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
577       {
578         GNUNET_break(0);
579         return;
580       }
581       ph.current_result->s_mlp = GNUNET_TIME_absolute_get ();
582       return;
583     case GAS_OP_SOLVE_MLP_MLP_STOP:
584       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
585           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
586           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
587       if ((GNUNET_NO == ph.expecting_solution) || (NULL == ph.current_result))
588       {
589         GNUNET_break(0);
590         return;
591       }
592       ph.current_result->e_mlp = GNUNET_TIME_absolute_get ();
593       ph.current_result->d_mlp = GNUNET_TIME_absolute_get_difference (
594       ph.current_result->s_mlp, ph.current_result->e_mlp);
595       return;
596     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
597       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
598           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
599           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
600       return;
601     case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
602       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
603           "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
604           (GAS_STAT_SUCCESS == stat) ? "SUCCESS" : "FAIL");
605       return;
606     default:
607       break;
608     }
609 }
610
611 static void
612 write_gnuplot_script (char * data_fn, int full)
613 {
614   struct GNUNET_DISK_FileHandle *f;
615   char * gfn;
616   char *data;
617   char *template;
618
619   /* Write header */
620   switch (ph.ats_mode) {
621     case MODE_PROPORTIONAL:
622       if (GNUNET_YES == full)
623         template = GNUPLOT_PROP_TEMPLATE;
624       else
625         template = GNUPLOT_PROP_UPDATE_TEMPLATE;
626       break;
627     case MODE_MLP:
628       if (GNUNET_YES == full)
629         template = GNUPLOT_MLP_TEMPLATE;
630       else
631         template = GNUPLOT_MLP_UPDATE_TEMPLATE;
632       break;
633     case MODE_RIL:
634       if (GNUNET_YES == full)
635         template = GNUPLOT_RIL_TEMPLATE;
636       else
637         template = GNUPLOT_RIL_UPDATE_TEMPLATE;
638       break;
639     default:
640       GNUNET_break (0);
641       return;
642   }
643
644   if (GNUNET_YES == full)
645     GNUNET_asprintf (&gfn, "perf_%s_full_%u_%u_%u.gnuplot", ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
646   else
647     GNUNET_asprintf (&gfn, "perf_%s_update_%u_%u_%u.gnuplot", ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address);
648
649   f = GNUNET_DISK_file_open (gfn,
650       GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
651       GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
652   if (NULL == f)
653   {
654     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot open gnuplot file `%s'\n", gfn);
655     GNUNET_free (gfn);
656     return;
657   }
658
659   if (GNUNET_SYSERR == GNUNET_DISK_file_write(f, template, strlen(template)))
660     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to plot file `%s'\n", gfn);
661
662   data = NULL;
663   if (MODE_PROPORTIONAL == ph.ats_mode)
664   {
665     GNUNET_asprintf (&data, "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
666                            "pause -1",
667                            data_fn, 3);
668   }
669   else if (MODE_MLP == ph.ats_mode)
670   {
671     GNUNET_asprintf (&data, "plot '%s' using 1:%u with lines title 'Total time to solve',\\\n" \
672                             "'%s' using 1:%u with lines title 'Time to setup',\\\n"
673                             "'%s' using 1:%u with lines title 'Time to solve LP',\\\n"
674                             "'%s' using 1:%u with lines title 'Total time to solve MLP'\n" \
675                             "pause -1",
676                            data_fn, 3,
677                            data_fn, 4,
678                            data_fn, 5,
679                            data_fn, 6);
680   }
681   else if (MODE_RIL == ph.ats_mode)
682   {
683     GNUNET_asprintf (&data,
684                      "plot '%s' using 1:%u with lines title 'Total time to solve'\n" \
685                      "pause -1",
686                      data_fn, 3);
687   }
688
689   if ((NULL != data) &&
690       (GNUNET_SYSERR == GNUNET_DISK_file_write (f, data, strlen(data))))
691     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
692                 "Cannot write data to plot file `%s'\n",
693                 gfn);
694   GNUNET_free_non_null (data);
695
696   if (GNUNET_SYSERR == GNUNET_DISK_file_close(f))
697     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
698                 "Cannot close gnuplot file `%s'\n",
699                 gfn);
700   else
701     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
702                 "Data successfully written to plot file `%s'\n",
703                 gfn);
704   GNUNET_free (gfn);
705
706 }
707
708 /**
709  * Evaluate results for a specific iteration
710  *
711  * @param iteration the iteration to evaluate
712  */
713
714 static void
715 evaluate (int iteration)
716 {
717   struct GNUNET_DISK_FileHandle *f_full;
718   struct GNUNET_DISK_FileHandle *f_update;
719   char * data_fn_full;
720   char * data_fn_update;
721   char * data;
722   struct Result *cur;
723   struct Result *next;
724   char * str_d_total;
725   char * str_d_setup;
726   char * str_d_lp;
727   char * str_d_mlp;
728
729   f_full = NULL;
730   f_update = NULL;
731
732   data_fn_full = NULL;
733
734   if (ph.create_plot)
735   {
736     GNUNET_asprintf (&data_fn_full,
737                      "perf_%s_full_%u_%u_%u.data",
738                      ph.ats_string,
739                      ph.N_peers_start,
740                      ph.N_peers_end,
741                      ph.N_address);
742     f_full = GNUNET_DISK_file_open (data_fn_full,
743         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
744         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
745     if (NULL == f_full)
746     {
747       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
748                   "Cannot open gnuplot file `%s'\n",
749                   data_fn_full);
750       GNUNET_free (data_fn_full);
751       return;
752     }
753     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
754     if (GNUNET_SYSERR == GNUNET_DISK_file_write(f_full, data, strlen(data)))
755       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
756                   "Cannot write data to log file `%s'\n",
757                   data_fn_full);
758     write_gnuplot_script (data_fn_full, GNUNET_YES);
759   }
760
761   data_fn_update = NULL;
762   if ((ph.create_plot) && (GNUNET_YES == ph.measure_updates))
763   {
764     GNUNET_asprintf (&data_fn_update, "perf_%s_update_%u_%u_%u.data",
765         ph.ats_string,
766         ph.N_peers_start,
767         ph.N_peers_end,
768         ph.N_address);
769     f_update = GNUNET_DISK_file_open (data_fn_update,
770         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
771         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
772     if (NULL == f_update)
773     {
774       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
775                   "Cannot open gnuplot file `%s'\n", data_fn_update);
776       GNUNET_free (data_fn_update);
777       if (NULL != f_full)
778         GNUNET_DISK_file_close (f_full);
779       GNUNET_free (data_fn_full);
780       return;
781     }
782     data = "#peers;addresses;time total in us;#time setup in us;#time lp in us;#time mlp in us;\n";
783     if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
784       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
785                   "Cannot write data to log file `%s'\n",
786                   data_fn_update);
787     write_gnuplot_script (data_fn_update, GNUNET_NO);
788   }
789
790   next = ph.iterations_results[ph.current_iteration -1].result_head;
791   while (NULL != (cur = next))
792   {
793     next = cur->next;
794     str_d_total = NULL;
795     str_d_setup = NULL;
796     str_d_lp = NULL;
797     str_d_mlp = NULL;
798
799     /* Print log */
800     ph.averaged_result[cur->peers - ph.N_peers_start].peers = cur->peers;
801     ph.averaged_result[cur->peers - ph.N_peers_start].addresses = cur->addresses;
802     ph.averaged_result[cur->peers - ph.N_peers_start].update = cur->update;
803
804     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_total.rel_value_us)
805     {
806       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ph.averaged_result[cur->peers - ph.N_peers_start].d_total.rel_value_us)
807         ph.averaged_result[cur->peers - ph.N_peers_start].d_total.rel_value_us = 0;
808       ph.averaged_result[cur->peers - ph.N_peers_start].d_total.rel_value_us += cur->d_total.rel_value_us;
809       fprintf (stderr,
810                "Total time to solve %s for %u peers %u addresses: %llu us\n",
811                (GNUNET_YES == cur->update) ? "updated" : "full",
812                cur->peers, cur->addresses, (unsigned long long) cur->d_total.rel_value_us);
813       GNUNET_asprintf(&str_d_total,
814                       "%llu",
815                       (unsigned long long) cur->d_total.rel_value_us);
816     }
817     else
818       GNUNET_asprintf(&str_d_total, "-1");
819     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_setup.rel_value_us)
820     {
821       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ph.averaged_result[cur->peers - ph.N_peers_start].d_setup.rel_value_us)
822         ph.averaged_result[cur->peers - ph.N_peers_start].d_setup.rel_value_us = 0;
823       ph.averaged_result[cur->peers - ph.N_peers_start].d_setup.rel_value_us += cur->d_setup.rel_value_us;
824       fprintf (stderr, "Total time to setup %s %u peers %u addresses: %llu us\n",
825           (GNUNET_YES == cur->update) ? "updated" : "full",
826           cur->peers, cur->addresses, (unsigned long long )cur->d_setup.rel_value_us);
827       GNUNET_asprintf(&str_d_setup, "%llu", (unsigned long long )cur->d_setup.rel_value_us);
828     }
829     else
830       GNUNET_asprintf(&str_d_setup, "-1");
831     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_lp.rel_value_us)
832     {
833       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ph.averaged_result[cur->peers - ph.N_peers_start].d_lp.rel_value_us)
834         ph.averaged_result[cur->peers - ph.N_peers_start].d_lp.rel_value_us = 0;
835       ph.averaged_result[cur->peers - ph.N_peers_start].d_lp.rel_value_us += cur->d_lp.rel_value_us;
836       fprintf (stderr,
837                "Total time to solve %s LP for %u peers %u addresses: %llu us\n",
838                (GNUNET_YES == cur->update) ? "updated" : "full",
839                cur->peers,
840                cur->addresses,
841                (unsigned long long )cur->d_lp.rel_value_us);
842       GNUNET_asprintf (&str_d_lp,
843                        "%llu",
844                        (unsigned long long )cur->d_lp.rel_value_us);
845     }
846     else
847       GNUNET_asprintf (&str_d_lp, "-1");
848     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_mlp.rel_value_us)
849     {
850       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ph.averaged_result[cur->peers - ph.N_peers_start].d_mlp.rel_value_us)
851         ph.averaged_result[cur->peers - ph.N_peers_start].d_mlp.rel_value_us = 0;
852       ph.averaged_result[cur->peers - ph.N_peers_start].d_mlp.rel_value_us += cur->d_mlp.rel_value_us;
853       fprintf (stderr, "Total time to solve %s MLP for %u peers %u addresses: %llu us\n",
854           (GNUNET_YES == cur->update) ? "updated" : "full",
855           cur->peers, cur->addresses, (unsigned long long )cur->d_mlp.rel_value_us);
856       GNUNET_asprintf (&str_d_mlp,
857                        "%llu",
858                        (unsigned long long )cur->d_mlp.rel_value_us);
859     }
860     else
861       GNUNET_asprintf (&str_d_mlp, "-1");
862
863     data = NULL;
864     if (GNUNET_YES == ph.create_plot)
865     {
866
867       GNUNET_asprintf (&data,
868                        "%u;%u;%s;%s;%s;%s\n",
869                        cur->peers, cur->addresses,
870                        str_d_total,
871                        str_d_setup,
872                        str_d_lp,
873                        str_d_mlp);
874       if (cur->update == GNUNET_NO)
875       {
876         if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_full, data, strlen(data)))
877           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
878                       "Cannot write data to log file `%s'\n",
879                       data_fn_full);
880       }
881       if ((cur->update == GNUNET_YES) && (NULL != f_update))
882       {
883         if (GNUNET_SYSERR == GNUNET_DISK_file_write (f_update, data, strlen(data)))
884           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
885                       "Cannot write data to log file `%s'\n",
886                       data_fn_update);
887       }
888       GNUNET_free (data);
889     }
890     GNUNET_free_non_null (str_d_total);
891     GNUNET_free_non_null (str_d_setup);
892     GNUNET_free_non_null (str_d_lp);
893     GNUNET_free_non_null (str_d_mlp);
894
895     GNUNET_CONTAINER_DLL_remove (ph.iterations_results[ph.current_iteration-1].result_head,
896         ph.iterations_results[ph.current_iteration-1].result_tail, cur);
897     GNUNET_free (cur);
898   }
899
900   if ((NULL != f_full) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_full)))
901     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
902         data_fn_full);
903   GNUNET_free_non_null (data_fn_full);
904
905   if ((NULL != f_update) && (GNUNET_SYSERR == GNUNET_DISK_file_close (f_update)))
906     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file `%s'\n",
907         data_fn_update);
908   GNUNET_free_non_null (data_fn_update);
909 }
910
911
912 static void
913 evaluate_average (void)
914 {
915   int c;
916   for (c = 0; c <= ph.N_peers_end - ph.N_peers_start; c++)
917   {
918     struct Result *cur = &ph.averaged_result[c];
919     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_total.rel_value_us)
920       fprintf (stderr,
921          "Average total time to solve %s for %u peers %u addresses: %llu us\n",
922          (GNUNET_YES == cur->update) ? "updated" : "full",
923              cur->peers, cur->addresses,
924          (unsigned long long) cur->d_total.rel_value_us / ph.iterations);
925
926     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_setup.rel_value_us)
927       fprintf (stderr,
928          "Average total time to setup for %u peers %u addresses: %llu us\n",
929              cur->peers, cur->addresses,
930          (unsigned long long) cur->d_setup.rel_value_us / ph.iterations);
931     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_lp.rel_value_us)
932       fprintf (stderr,
933          "Average total time to solve lp %s for %u peers %u addresses: %llu us\n",
934          (GNUNET_YES == cur->update) ? "updated" : "full",
935              cur->peers, cur->addresses,
936          (unsigned long long) cur->d_lp.rel_value_us / ph.iterations);
937     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != cur->d_mlp.rel_value_us)
938       fprintf (stderr,
939          "Average total time to solve mlp %s for %u peers %u addresses: %llu us\n",
940          (GNUNET_YES == cur->update) ? "updated" : "full",
941              cur->peers, cur->addresses,
942          (unsigned long long) cur->d_mlp.rel_value_us / ph.iterations);
943   }
944
945 }
946
947 static void
948 perf_run (void)
949 {
950   struct ATS_Address *cur;
951   struct ATS_Address *next;
952   int cp;
953   int ca;
954   int count_p = ph.N_peers_end;
955   int count_a = ph.N_address;
956   struct ATS_Address * cur_addr;
957
958
959   ph.peers = GNUNET_malloc ((count_p) * sizeof (struct PerfPeer));
960   for (cp = 0; cp < count_p; cp++)
961     perf_create_peer (cp);
962   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
963       "Iteration %u of %u, added %u peers\n", ph.current_iteration, ph.iterations, cp);
964
965   for (cp = 0; cp < count_p; cp++)
966   {
967     if (GNUNET_NO == ph.bulk_running)
968     {
969       ph.bulk_running = GNUNET_YES;
970       ph.env.sf.s_bulk_start (ph.solver);
971     }
972     ph.current_p = cp + 1;
973     for (ca = 0; ca < count_a; ca++)
974     {
975       cur_addr = perf_create_address (cp, ca);
976       /* Add address */
977       ph.env.sf.s_add (ph.solver, cur_addr, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_ATS_NetworkTypeCount));
978       ph.current_a = ca + 1;
979       perf_address_initial_update (ph.solver, ph.addresses, cur_addr);
980       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
981           "Adding address for peer %u address %u\n", cp, ca);
982     }
983     /* Notify solver about request */
984     ph.env.sf.s_get (ph.solver, &ph.peers[cp].id);
985
986     if (cp + 1 >= ph.N_peers_start)
987     {
988       /* Disable bulk to solve the problem */
989       if (GNUNET_YES == ph.bulk_running)
990       {
991         ph.expecting_solution = GNUNET_YES;
992         ph.bulk_running = GNUNET_NO;
993         ph.env.sf.s_bulk_stop (ph.solver);
994       }
995       else
996       {
997         GNUNET_break (0);
998       }
999
1000       /* Problem is solved by the solver here due to unlocking */
1001       ph.expecting_solution = GNUNET_NO;
1002
1003       /* Update the problem */
1004       if ((0 < ph.opt_update_percent) && (GNUNET_YES == ph.measure_updates))
1005       {
1006         /* Update */
1007         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1008             "Updating problem with %u peers and %u addresses\n", cp + 1, ca);
1009
1010         ph.expecting_solution = GNUNET_YES;
1011         if (GNUNET_NO == ph.bulk_running)
1012         {
1013           ph.bulk_running = GNUNET_YES;
1014           ph.env.sf.s_bulk_start (ph.solver);
1015         }
1016         perf_update_all_addresses (cp + 1, ca, ph.opt_update_percent);
1017         ph.bulk_running = GNUNET_NO;
1018         ph.env.sf.s_bulk_stop (ph.solver);
1019         /* Problem is solved by the solver here due to unlocking */
1020         ph.expecting_solution = GNUNET_NO;
1021       }
1022       GNUNET_assert (GNUNET_NO == ph.bulk_running);
1023     }
1024   }
1025
1026   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1027       "Done, cleaning up addresses\n");
1028   if (GNUNET_NO == ph.bulk_running)
1029   {
1030     ph.env.sf.s_bulk_start (ph.solver);
1031     ph.bulk_running = GNUNET_YES;
1032   }
1033
1034   for (cp = 0; cp < count_p; cp++)
1035   {
1036     for (cur = ph.peers[cp].head; cur != NULL ; cur = next)
1037     {
1038       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1039           "Deleting addresses for peer %u\n", cp);
1040       GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (ph.addresses,
1041           &ph.peers[cp].id, cur));
1042       ph.env.sf.s_del (ph.solver, cur, GNUNET_NO);
1043       next = cur->next;
1044       GNUNET_CONTAINER_DLL_remove(ph.peers[cp].head, ph.peers[cp].tail, cur);
1045       GNUNET_free(cur);
1046     }
1047
1048   }
1049   GNUNET_free(ph.peers);
1050 }
1051
1052
1053 static void
1054 run (void *cls, char * const *args, const char *cfgfile,
1055     const struct GNUNET_CONFIGURATION_Handle *cfg)
1056 {
1057   GNUNET_log_setup ("perf-ats-solver", "WARNING", NULL);
1058   char *sep;
1059   char *src_filename = GNUNET_strdup (__FILE__);
1060   char *test_filename = cls;
1061   char *solver;
1062   char *plugin;
1063   struct GNUNET_CONFIGURATION_Handle *solver_cfg;
1064   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1065   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1066   int c;
1067
1068   /* Extract test name */
1069   if (NULL == (sep  = (strstr (src_filename,".c"))))
1070   {
1071     GNUNET_free (src_filename);
1072     GNUNET_break (0);
1073     ret = 1;
1074     return ;
1075   }
1076   sep[0] = '\0';
1077
1078   if (NULL != (sep = strstr (test_filename, ".exe")))
1079     sep[0] = '\0';
1080
1081   if (NULL == (solver = strstr (test_filename, src_filename)))
1082   {
1083     GNUNET_free (src_filename);
1084     GNUNET_break (0);
1085     ret = 1;
1086     return ;
1087   }
1088   solver += strlen (src_filename) +1;
1089
1090   if (0 == strcmp(solver, "proportional"))
1091   {
1092     ph.ats_mode = MODE_PROPORTIONAL;
1093     ph.ats_string = "proportional";
1094   }
1095   else if (0 == strcmp(solver, "mlp"))
1096   {
1097     ph.ats_mode = MODE_MLP;
1098     ph.ats_string = "mlp";
1099   }
1100   else if ((0 == strcmp(solver, "ril")))
1101   {
1102     ph.ats_mode = MODE_RIL;
1103     ph.ats_string = "ril";
1104   }
1105   else
1106   {
1107     GNUNET_free (src_filename);
1108     GNUNET_break (0);
1109     ret = 1;
1110     return ;
1111   }
1112   GNUNET_free (src_filename);
1113
1114   /* Calculcate peers */
1115   if ((0 == ph.N_peers_start) && (0 == ph.N_peers_end))
1116   {
1117     ph.N_peers_start = DEFAULT_PEERS_START;
1118     ph.N_peers_end = DEFAULT_PEERS_END;
1119   }
1120   if (0 == ph.N_address)
1121     ph.N_address = DEFAULT_ADDRESSES;
1122
1123   if (ph.N_peers_start != ph.N_peers_end)
1124     fprintf (stderr, "Benchmarking solver `%s' with %u to %u peers and %u addresses in %u iterations\n",
1125         ph.ats_string, ph.N_peers_start, ph.N_peers_end, ph.N_address, ph.iterations);
1126   else
1127     fprintf (stderr, "Benchmarking solver `%s' with %u peers and %u addresses in %u iterations\n",
1128         ph.ats_string, ph.N_peers_end, ph.N_address, ph.iterations);
1129
1130   if (0 == ph.opt_update_percent)
1131     ph.opt_update_percent = DEFAULT_UPDATE_PERCENTAGE;
1132
1133   /* Load quotas */
1134   solver_cfg = GNUNET_CONFIGURATION_create();
1135   if ((NULL == solver_cfg) || (GNUNET_SYSERR == (GNUNET_CONFIGURATION_load ( solver_cfg, "perf_ats_solver.conf"))))
1136   {
1137     GNUNET_break(0);
1138     end_now (1);
1139     return;
1140   }
1141   if (GNUNET_ATS_NetworkTypeCount != load_quotas (solver_cfg,
1142       quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount))
1143   {
1144     GNUNET_break(0);
1145     end_now (1);
1146     return;
1147   }
1148
1149   /* Create array of DLL to store results for iterations */
1150   ph.iterations_results = GNUNET_malloc (sizeof (struct Iteration) * ph.iterations);
1151   ph.averaged_result = GNUNET_malloc (sizeof (struct Result) * ((ph.N_peers_end + 1) - ph.N_peers_start));
1152   for (c = 0; c <= ph.N_peers_end - ph.N_peers_start; c++)
1153   {
1154     ph.averaged_result[c].d_setup = GNUNET_TIME_UNIT_FOREVER_REL;
1155     ph.averaged_result[c].d_total = GNUNET_TIME_UNIT_FOREVER_REL;
1156     ph.averaged_result[c].d_lp = GNUNET_TIME_UNIT_FOREVER_REL;
1157     ph.averaged_result[c].d_mlp = GNUNET_TIME_UNIT_FOREVER_REL;
1158   }
1159
1160   /* Load solver */
1161   ph.env.cfg = solver_cfg;
1162   ph.stat = GNUNET_STATISTICS_create ("ats", cfg);
1163   ph.env.stats = ph.stat;
1164   ph.addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1165   ph.env.addresses = ph.addresses;
1166   ph.env.bandwidth_changed_cb = bandwidth_changed_cb;
1167   ph.env.get_preferences = &get_preferences_cb;
1168   ph.env.get_property = &get_property_cb;
1169   ph.env.network_count = GNUNET_ATS_NetworkTypeCount;
1170   ph.env.info_cb = &solver_info_cb;
1171   ph.env.info_cb_cls = NULL;
1172
1173   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1174   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1175   {
1176     ph.env.networks[c] = networks[c];
1177     ph.env.out_quota[c] = quotas_out[c];
1178     ph.env.in_quota[c] = quotas_in[c];
1179     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loading network quotas: `%s' %llu %llu \n",
1180         GNUNET_ATS_print_network_type(ph.env.networks[c]),
1181         ph.env.out_quota[c],
1182         ph.env.in_quota[c]);
1183   }
1184   GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL );
1185
1186   GNUNET_asprintf (&plugin, "libgnunet_plugin_ats_%s", ph.ats_string);
1187   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s'\n"), ph.ats_string);
1188   if  (NULL == (ph.solver = GNUNET_PLUGIN_load (plugin, &ph.env)))
1189   {
1190     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), plugin);
1191     ret = 1;
1192     return;
1193   }
1194
1195   /* Do the benchmark */
1196   for (ph.current_iteration = 1; ph.current_iteration <= ph.iterations; ph.current_iteration++)
1197   {
1198     perf_run ();
1199     evaluate (ph.current_iteration);
1200   }
1201   evaluate_average ();
1202
1203   /* Unload solver*/
1204   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Unloading solver `%s'\n"), ph.ats_string);
1205   GNUNET_PLUGIN_unload (plugin, ph.solver);
1206   GNUNET_free (plugin);
1207   GNUNET_free (ph.iterations_results);
1208   GNUNET_free (ph.averaged_result);
1209   GNUNET_CONFIGURATION_destroy (solver_cfg);
1210   GNUNET_STATISTICS_destroy (ph.stat, GNUNET_NO);
1211   ph.solver = NULL;
1212 }
1213
1214 int
1215 main (int argc, char *argv[])
1216 {
1217   /* extract command line arguments */
1218   ph.opt_update_percent = 0;
1219   ph.N_peers_start = 0;
1220   ph.N_peers_end = 0;
1221   ph.N_address = 0;
1222   ph.ats_string = NULL;
1223   ph.create_plot = GNUNET_NO;
1224   ph.measure_updates = GNUNET_NO;
1225   ph.iterations = 1;
1226
1227   static struct GNUNET_GETOPT_CommandLineOption options[] = {
1228       { 'a', "addresses", NULL,
1229           gettext_noop ("addresses to use"),
1230           1, &GNUNET_GETOPT_set_uint, &ph.N_address },
1231       { 's', "start", NULL,
1232           gettext_noop ("start with peer"),
1233           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_start },
1234       { 'e', "end", NULL,
1235           gettext_noop ("end with peer"),
1236           1, &GNUNET_GETOPT_set_uint, &ph.N_peers_end },
1237       { 'i', "iterations", NULL,
1238           gettext_noop ("number of iterations used for averaging (default: 1)"),
1239           1, &GNUNET_GETOPT_set_uint, &ph.iterations },
1240       { 'p', "percentage", NULL,
1241           gettext_noop ("update a fix percentage of addresses"),
1242           1, &GNUNET_GETOPT_set_uint, &ph.opt_update_percent },
1243       { 'g', "gnuplot", NULL,
1244           gettext_noop ("create GNUplot file"),
1245           0, &GNUNET_GETOPT_set_one, &ph.create_plot},
1246       { 'u', "update", NULL,
1247           gettext_noop ("measure updates"),
1248           0, &GNUNET_GETOPT_set_one, &ph.measure_updates},
1249       GNUNET_GETOPT_OPTION_END
1250   };
1251
1252   GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
1253
1254   return ret;
1255 }
1256
1257 /* end of file perf_ats_solver.c */