adapting testcases to use new API
[oweals/gnunet.git] / src / transport / perf_transport_ats.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 testing/perf_transport_ats.c
22  * @brief testcase for ats functionality
23  */
24 #include "platform.h"
25 #include "gnunet_time_lib.h"
26 #include "gauger.h"
27 #if HAVE_LIBGLPK
28 #include <glpk.h>
29 #endif
30
31 #define VERBOSE GNUNET_NO
32
33 #define EXECS 5
34
35
36 #if HAVE_LIBGLPK
37 static int executions = EXECS;
38 static uint64_t exec_time[EXECS];
39
40 static uint64_t sim_no_opt_avg;
41 static uint64_t sim_with_opt_avg;
42 static uint64_t mlp_no_opt_avg;
43 static uint64_t mlp_with_opt_avg;
44
45 static glp_prob * prob;
46
47 static struct GNUNET_TIME_Absolute start;
48 static struct GNUNET_TIME_Absolute end;
49
50
51 static void 
52 solve_mlp(int presolve)
53 {
54   int result, solution;
55   
56   glp_iocp opt_mlp;
57   glp_init_iocp(&opt_mlp);
58   opt_mlp.msg_lev = GLP_MSG_OFF;
59   opt_mlp.presolve = GLP_OFF;
60   
61   result = glp_intopt (prob, &opt_mlp);
62   solution =  glp_mip_status (prob);
63   GNUNET_assert ((solution == 5) && (result==0));
64 }
65
66 static void 
67 solve_lp(int presolve)
68 {
69   int result, solution;
70   
71   glp_smcp opt_lp;
72   glp_init_smcp(&opt_lp);
73   
74   opt_lp.msg_lev = GLP_MSG_OFF;
75   if (presolve==GNUNET_YES) opt_lp.presolve = GLP_ON;
76   else opt_lp.presolve = GLP_OFF;
77   
78   result = glp_simplex(prob, &opt_lp);
79   solution =  glp_get_status (prob);
80   GNUNET_assert ((solution == 5) && (result==0));
81 }
82
83 #if 0
84 /* Modify quality constraint */
85 static void 
86 modify_qm(int start, int length, int values_to_change)
87 {
88   //int * ind = GNUNET_malloc (length * sizeof (int));
89   //double *val = GNUNET_malloc (length * sizeof (double));
90   int ind[1000];
91   double val[1000];
92   
93   int res = 0;
94   int c = start, c2=1;
95   while (c<=(start+values_to_change))
96     {
97       res = glp_get_mat_row(prob, c, ind, val);
98       
99       printf("%i %i \n", c, res);
100       for (c2=0; c2<res; c2++)
101         {
102           printf("%i = %f \n", ind[c2], val[c2]);
103         }
104       
105       c++;
106     }
107   //glp_set_mat_row(prob, start, length, ind, val);
108 }
109 #endif
110
111
112 static void
113 bench_simplex_optimization(char * file, int executions)
114 {
115   
116   int c;
117   prob = glp_create_prob();
118   glp_read_lp(prob, NULL, file);
119   
120   solve_lp(GNUNET_YES);
121   
122   for (c=0; c<executions;c++)
123     {
124       start = GNUNET_TIME_absolute_get();
125       solve_lp(GNUNET_NO);
126       end = GNUNET_TIME_absolute_get();
127       
128       exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
129       
130       sim_with_opt_avg += exec_time[c];
131       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
132                   "Simplex /w optimization iterations %i: %llu \n", 
133                   c, exec_time[c]);
134     }
135   
136   glp_delete_prob(prob);
137 }
138
139
140 static void
141 bench_simplex_no_optimization(char * file, int executions)
142 {
143   
144   int c;
145   prob = glp_create_prob();
146   glp_read_lp(prob, NULL, file);
147   
148   for (c=0; c<executions;c++)
149     {
150       start = GNUNET_TIME_absolute_get();
151       solve_lp(GNUNET_YES);
152       end = GNUNET_TIME_absolute_get();
153       
154       exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
155       
156       sim_no_opt_avg += exec_time[c];
157       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
158                   "Simplex iterations %i: %llu \n",  
159                   c, exec_time[c]);
160     }
161   
162   glp_delete_prob(prob);
163 }
164
165
166 static void
167 bench_mlp_no_optimization(char * file, int executions)
168 {
169   
170   int c;
171   prob = glp_create_prob();
172   glp_read_lp(prob, NULL, file);
173   
174   for (c=0; c<executions;c++)
175     {
176       start = GNUNET_TIME_absolute_get();
177       solve_lp(GNUNET_YES);
178       solve_mlp (GNUNET_NO);
179       end = GNUNET_TIME_absolute_get();
180       
181       exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
182       
183       mlp_no_opt_avg += exec_time[c];
184       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP iterations no optimization %i: %llu \n",  c, exec_time[c]);
185     }
186   
187   glp_delete_prob(prob);
188 }
189
190
191 static void 
192 bench_mlp_with_optimization(char * file, int executions, int changes)
193 {
194   int c;
195   prob = glp_create_prob();
196   glp_read_lp(prob, NULL, file);
197   
198   solve_lp(GNUNET_YES);
199   
200   for (c=0; c<executions;c++)
201     {
202       start = GNUNET_TIME_absolute_get();
203       // modify_qm(906, 0, 0);
204       solve_lp(GNUNET_NO);
205       solve_mlp (GNUNET_NO);
206       end = GNUNET_TIME_absolute_get();
207       
208       exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
209       
210       mlp_with_opt_avg += exec_time[c];
211       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP /w optimization iterations %i: %llu \n",  c, exec_time[c]);
212     }
213   
214   glp_delete_prob(prob);
215 }
216
217 #if 0
218 static void 
219 modify_cr (int start, int length, int count)
220 {
221   //int * ind = GNUNET_malloc (length * sizeof (int));
222   //double *val = GNUNET_malloc (length * sizeof (double));
223   int ind[500];
224   double val[500];
225   int res = 0;
226   int c = start, c2=1;
227   while (c<=(start+count))
228     {
229       res = glp_get_mat_row(prob, c, ind, val);
230       
231       printf("row index: %i non-zero elements: %i \n", c, res);
232       for (c2=1; c2<=res; c2++)
233         {
234           printf("%i = %f ", ind[c2], val[c2]);
235         }
236       c++;
237       printf ("\n----\n");
238     }
239   //glp_set_mat_row(prob, start, length, ind, val);
240 }
241 #endif
242 #endif
243
244
245 int
246 main (int argc, char *argv[])
247 {
248   int ret = 0;
249   GNUNET_log_setup ("perf-transport-ats",
250 #if VERBOSE
251                     "DEBUG",
252 #else
253                     "INFO",
254 #endif
255                     NULL);
256   
257 #if !HAVE_LIBGLPK
258   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259               "GLPK not installed, exiting testcase\n");
260   return 0;
261 #else
262   
263   char * file = "ats_mlp_p100_m400.problem";
264   // char * file = "mlps/ats_mlp_p500_m2000.problem";
265   bench_simplex_no_optimization (file, executions);
266   bench_simplex_optimization (file, executions);
267   bench_mlp_no_optimization (file, executions);
268   bench_mlp_with_optimization (file, executions, 0);
269   
270   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
271               "Simplex no optimization average: %llu\n", 
272               sim_no_opt_avg  / EXECS);
273   // -> 400 addresses
274   GAUGER ("GLPK",
275           "GLPK simplex  no optimization", 
276           (sim_no_opt_avg  / EXECS) / 400, 
277           "ms/address");
278   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
279               "Simplex, no optimization, average per peer: %llu\n",
280               (sim_with_opt_avg / EXECS) / 400);
281   GAUGER ("GLPK",
282           "GLPK simplex with optimization",
283           (sim_with_opt_avg  / EXECS) / 400,
284           "ms/address");
285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286               "MLP no optimization average: %llu\n", 
287               (mlp_no_opt_avg  / EXECS) / 400);
288   GAUGER ("GLPK",
289           "GLPK MLP no optimization", 
290           (mlp_no_opt_avg  / EXECS) / 400,
291           "ms/address");
292   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP optimization average: %llu\n", 
293               (mlp_with_opt_avg/ EXECS) / 400);
294   GAUGER ("GLPK",
295           "GLPK MLP with optimization", 
296           (mlp_with_opt_avg  / EXECS) / 400, "ms/address");
297
298 #endif
299   return ret;
300 }
301
302 /* end of per_transport_ats.c*/