remove send on connect
[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 transport/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 #include <glpk.h>
28
29 #define VERBOSE GNUNET_NO
30
31 #define EXECS 5
32
33 static int ret = 0;
34
35 static int executions = EXECS;
36 static uint64_t exec_time[EXECS];
37
38 static uint64_t sim_no_opt_avg;
39 static uint64_t sim_with_opt_avg;
40 static uint64_t mlp_no_opt_avg;
41 static uint64_t mlp_with_opt_avg;
42
43 static glp_prob * prob;
44
45 static struct GNUNET_TIME_Absolute start;
46 static struct GNUNET_TIME_Absolute end;
47
48 static void 
49 solve_mlp(int presolve)
50 {
51   int result, solution;
52   glp_iocp opt_mlp;
53
54   glp_init_iocp(&opt_mlp);
55   opt_mlp.msg_lev = GLP_MSG_OFF;
56   opt_mlp.presolve = GLP_OFF;
57
58   result = glp_intopt (prob, &opt_mlp);
59   solution =  glp_mip_status (prob);
60   GNUNET_assert ((solution == 5) && (result==0));
61 }
62
63 static void
64 solve_lp(int presolve)
65 {
66   int result;
67   int solution;
68
69   glp_smcp opt_lp;
70   glp_init_smcp(&opt_lp);
71
72   opt_lp.msg_lev = GLP_MSG_OFF;
73   if (presolve==GNUNET_YES) opt_lp.presolve = GLP_ON;
74   else opt_lp.presolve = GLP_OFF;
75
76   result = glp_simplex(prob, &opt_lp);
77   solution =  glp_get_status (prob);
78   GNUNET_assert ((solution == 5) && (result==0));
79 }
80
81 #if 0
82 /* Modify quality constraint */
83 static void 
84 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 #endif
107
108
109
110 static void 
111 bench_simplex_optimization(char * file, int executions)
112 {
113   int c;
114   int res;
115
116   prob = glp_create_prob();
117   res = glp_read_lp(prob, NULL, file);
118   if (res != 0)
119   {
120     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121         "Problem file `%s' not found\n",  file);
122     ret = 1;
123     return;
124   }
125
126   solve_lp(GNUNET_YES);
127
128   for (c=0; c<executions;c++)
129   {
130     start = GNUNET_TIME_absolute_get();
131     solve_lp(GNUNET_NO);
132     end = GNUNET_TIME_absolute_get();
133
134     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
135
136     sim_with_opt_avg += exec_time[c];
137     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138         "Simplex /w optimization iterations %i: %llu \n",  c, exec_time[c]);
139   }
140
141   glp_delete_prob(prob);
142 }
143
144
145 static void
146 bench_simplex_no_optimization(char * file, int executions)
147 {
148   int c;
149   int res;
150
151   prob = glp_create_prob();
152   res = glp_read_lp(prob, NULL, file);
153   if (res != 0)
154   {
155     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156         "Problem file `%s' not found\n",  file);
157     ret = 1;
158     return;
159   }
160
161   for (c=0; c<executions;c++)
162   {
163     start = GNUNET_TIME_absolute_get();
164     solve_lp(GNUNET_YES);
165     end = GNUNET_TIME_absolute_get();
166
167     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
168
169     sim_no_opt_avg += exec_time[c];
170     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171         "Simplex iterations %i: %llu \n",
172         c, exec_time[c]);
173   }
174
175   glp_delete_prob(prob);
176 }
177
178 static void
179 bench_mlp_no_optimization(char * file, int executions)
180 {
181   int c;
182   int res;
183
184   prob = glp_create_prob();
185   res = glp_read_lp(prob, NULL, file);
186   if (res != 0)
187   {
188     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189         "Problem file `%s' not found\n",  file);
190     ret = 1;
191     return;
192   }
193   for (c=0; c<executions;c++)
194   {
195       start = GNUNET_TIME_absolute_get();
196       solve_lp(GNUNET_YES);
197       solve_mlp (GNUNET_NO);
198       end = GNUNET_TIME_absolute_get();
199
200       exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
201
202       mlp_no_opt_avg += exec_time[c];
203       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204           "MLP iterations no optimization %i: %llu \n",
205           c, exec_time[c]);
206   }
207
208   glp_delete_prob(prob);
209 }
210
211
212 static void 
213 bench_mlp_with_optimization(char * file, int executions, int changes)
214 {
215   int c;
216   int res;
217
218   prob = glp_create_prob();
219   res = glp_read_lp(prob, NULL, file);
220   if (res != 0)
221   {
222     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223         "Problem file `%s' not found\n",  file);
224     ret = 1;
225     return;
226   }
227
228   solve_lp(GNUNET_YES);
229
230   for (c=0; c<executions;c++)
231   {
232     start = GNUNET_TIME_absolute_get();
233     //modify_qm(906, 0, 0);
234     solve_lp(GNUNET_NO);
235     solve_mlp (GNUNET_NO);
236     end = GNUNET_TIME_absolute_get();
237
238     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
239
240     mlp_with_opt_avg += exec_time[c];
241     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242         "MLP /w optimization iterations %i: %llu \n",
243         c, exec_time[c]);
244   }
245
246   glp_delete_prob(prob);
247 }
248
249 #if 0
250 void modify_cr (int start, int length, int count)
251 {
252   //int * ind = GNUNET_malloc (length * sizeof (int));
253   //double *val = GNUNET_malloc (length * sizeof (double));
254   int ind[500];
255   double val[500];
256   int res = 0;
257   int c = start, c2=1;
258   while (c<=(start+count))
259   {
260     res = glp_get_mat_row(prob, c, ind, val);
261
262     printf("row index: %i non-zero elements: %i \n", c, res);
263     for (c2=1; c2<=res; c2++)
264     {
265             printf("%i = %f ", ind[c2], val[c2]);
266     }
267     c++;
268     printf ("\n----\n");
269   }
270   //glp_set_mat_row(prob, start, length, ind, val);
271 }
272 #endif
273
274
275 int main (int argc, char *argv[])
276 {
277
278   GNUNET_log_setup ("perf-transport-ats",
279 #if VERBOSE
280                     "DEBUG",
281 #else
282                     "INFO",
283 #endif
284                     NULL);
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   return ret;
334 }
335
336 /* end of perf_transport_ats.c*/
337