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