improvements
[oweals/gnunet.git] / src / transport / perf_transport_ats.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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/per_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 static int ret = 0;
51
52 void solve_mlp(int presolve)
53 {
54   int result, solution;
55   glp_iocp opt_mlp;
56
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 void solve_lp(int presolve)
67 {
68   int result;
69   int 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 /* Modify quality constraint */
84 void modify_qm(int start, int length, int values_to_change)
85 {
86   //int * ind = GNUNET_malloc (length * sizeof (int));
87   //double *val = GNUNET_malloc (length * sizeof (double));
88   int ind[1000];
89   double val[1000];
90
91   int res = 0;
92   int c = start, c2=1;
93   while (c<=(start+values_to_change))
94   {
95     res = glp_get_mat_row(prob, c, ind, val);
96
97     printf("%i %i \n", c, res);
98     for (c2=0; c2<res; c2++)
99     {
100             printf("%i = %f \n", ind[c2], val[c2]);
101     }
102     c++;
103   }
104   //glp_set_mat_row(prob, start, length, ind, val);
105 }
106
107
108
109 void bench_simplex_optimization(char * file, int executions)
110 {
111   int c;
112   int res;
113
114   prob = glp_create_prob();
115   res = glp_read_lp(prob, NULL, file);
116   if (res != 0)
117   {
118     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119         "Problem file `%s' not found\n",  file);
120     ret = 1;
121     return;
122   }
123
124   solve_lp(GNUNET_YES);
125
126   for (c=0; c<executions;c++)
127   {
128     start = GNUNET_TIME_absolute_get();
129     solve_lp(GNUNET_NO);
130     end = GNUNET_TIME_absolute_get();
131
132     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
133
134     sim_with_opt_avg += exec_time[c];
135     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136         "Simplex /w optimization iterations %i: %llu \n",  c, exec_time[c]);
137   }
138
139   glp_delete_prob(prob);
140 }
141
142
143 void bench_simplex_no_optimization(char * file, int executions)
144 {
145   int c;
146   int res;
147
148   prob = glp_create_prob();
149   res = glp_read_lp(prob, NULL, file);
150   if (res != 0)
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153         "Problem file `%s' not found\n",  file);
154     ret = 1;
155     return;
156   }
157
158   for (c=0; c<executions;c++)
159   {
160     start = GNUNET_TIME_absolute_get();
161     solve_lp(GNUNET_YES);
162     end = GNUNET_TIME_absolute_get();
163
164     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
165
166     sim_no_opt_avg += exec_time[c];
167     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168         "Simplex iterations %i: %llu \n",
169         c, exec_time[c]);
170   }
171
172   glp_delete_prob(prob);
173 }
174
175 void bench_mlp_no_optimization(char * file, int executions)
176 {
177   int c;
178   int res;
179
180   prob = glp_create_prob();
181   res = glp_read_lp(prob, NULL, file);
182   if (res != 0)
183   {
184     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185         "Problem file `%s' not found\n",  file);
186     ret = 1;
187     return;
188   }
189   for (c=0; c<executions;c++)
190   {
191       start = GNUNET_TIME_absolute_get();
192       solve_lp(GNUNET_YES);
193       solve_mlp (GNUNET_NO);
194       end = GNUNET_TIME_absolute_get();
195
196       exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
197
198       mlp_no_opt_avg += exec_time[c];
199       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200           "MLP iterations no optimization %i: %llu \n",
201           c, exec_time[c]);
202   }
203
204   glp_delete_prob(prob);
205 }
206
207
208 void bench_mlp_with_optimization(char * file, int executions, int changes)
209 {
210   int c;
211   int res;
212
213   prob = glp_create_prob();
214   res = glp_read_lp(prob, NULL, file);
215   if (res != 0)
216   {
217     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218         "Problem file `%s' not found\n",  file);
219     ret = 1;
220     return;
221   }
222
223   solve_lp(GNUNET_YES);
224
225   for (c=0; c<executions;c++)
226   {
227     start = GNUNET_TIME_absolute_get();
228     //modify_qm(906, 0, 0);
229     solve_lp(GNUNET_NO);
230     solve_mlp (GNUNET_NO);
231     end = GNUNET_TIME_absolute_get();
232
233     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
234
235     mlp_with_opt_avg += exec_time[c];
236     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237         "MLP /w optimization iterations %i: %llu \n",
238         c, exec_time[c]);
239   }
240
241   glp_delete_prob(prob);
242 }
243
244 #if 0
245 void modify_cr (int start, int length, int count)
246 {
247   //int * ind = GNUNET_malloc (length * sizeof (int));
248   //double *val = GNUNET_malloc (length * sizeof (double));
249   int ind[500];
250   double val[500];
251   int res = 0;
252   int c = start, c2=1;
253   while (c<=(start+count))
254   {
255     res = glp_get_mat_row(prob, c, ind, val);
256
257     printf("row index: %i non-zero elements: %i \n", c, res);
258     for (c2=1; c2<=res; c2++)
259     {
260             printf("%i = %f ", ind[c2], val[c2]);
261     }
262     c++;
263     printf ("\n----\n");
264   }
265   //glp_set_mat_row(prob, start, length, ind, val);
266 }
267 #endif
268 #endif
269
270 int main (int argc, char *argv[])
271 {
272
273   GNUNET_log_setup ("perf-transport-ats",
274 #if VERBOSE
275                     "DEBUG",
276 #else
277                     "INFO",
278 #endif
279                     NULL);
280
281 #if !HAVE_LIBGLPK
282         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, exiting testcase\n");
283         return 0;
284 #else
285
286   int nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
287   if (nullfd < 0)
288     return GNUNET_SYSERR;
289   if (dup2 (nullfd, 1) < 0)
290   {
291     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
292     (void) CLOSE (nullfd);
293     return GNUNET_SYSERR;
294   }
295
296
297   char * file = "ats_mlp_p100_m400.problem";
298
299   bench_simplex_no_optimization (file, executions);
300   bench_simplex_optimization (file, executions);
301   bench_mlp_no_optimization (file, executions);
302   bench_mlp_with_optimization (file, executions, 0);
303
304   if (ret != 0)
305     return ret;
306
307   // -> 400 addresses
308   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
309       "Simplex, no optimization, average per address: %f\n",
310       ((double) sim_no_opt_avg / EXECS) / 400);
311   GAUGER ("TRANSPORT","GLPK simplex  no optimization",
312       ((double) sim_no_opt_avg  / EXECS) / 400, "ms/address");
313
314   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
315       "Simplex, with optimization, average per address: %f\n",
316       ((double) sim_with_opt_avg / EXECS) / 400);
317   GAUGER ("TRANSPORT",
318       "GLPK simplex, 100 peers 400 addresses with optimization",
319       ((double) sim_with_opt_avg  / EXECS) / 400, "ms/address");
320
321   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
322       "MLP no optimization average per address: %f\n",
323       ((double) mlp_no_opt_avg  / EXECS) / 400);
324   GAUGER ("TRANSPORT","GLPK MLP 100 peers 400 addresses no optimization",
325       ((double) mlp_no_opt_avg  / EXECS) / 400, "ms/address");
326
327   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
328       "MLP optimization average per address: %f\n",
329       ((double) mlp_with_opt_avg/ EXECS) / 400);
330   GAUGER ("TRANSPORT",
331       "GLPK MLP 100 peers 400 addresses with optimization",
332       ((double) mlp_with_opt_avg  / EXECS) / 400, "ms/address");
333   (void) CLOSE (nullfd);
334
335 #endif
336   return ret;
337 }
338
339 /* end of per_transport_ats.c*/
340