(no commit message)
[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
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
112   prob = glp_create_prob();
113   glp_read_lp(prob, NULL, file);
114
115   solve_lp(GNUNET_YES);
116
117   for (c=0; c<executions;c++)
118   {
119     start = GNUNET_TIME_absolute_get();
120     solve_lp(GNUNET_NO);
121     end = GNUNET_TIME_absolute_get();
122
123     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
124
125     sim_with_opt_avg += exec_time[c];
126     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127         "Simplex /w optimization iterations %i: %llu \n",  c, exec_time[c]);
128   }
129
130   glp_delete_prob(prob);
131 }
132
133
134 void bench_simplex_no_optimization(char * file, int executions)
135 {
136   int c;
137
138   prob = glp_create_prob();
139   glp_read_lp(prob, NULL, file);
140
141   for (c=0; c<executions;c++)
142   {
143     start = GNUNET_TIME_absolute_get();
144     solve_lp(GNUNET_YES);
145     end = GNUNET_TIME_absolute_get();
146
147     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
148
149     sim_no_opt_avg += exec_time[c];
150     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151         "Simplex iterations %i: %llu \n",
152         c, exec_time[c]);
153   }
154
155   glp_delete_prob(prob);
156 }
157
158 void bench_mlp_no_optimization(char * file, int executions)
159 {
160   int c;
161
162   prob = glp_create_prob();
163   glp_read_lp(prob, NULL, file);
164
165   for (c=0; c<executions;c++)
166   {
167       start = GNUNET_TIME_absolute_get();
168       solve_lp(GNUNET_YES);
169       solve_mlp (GNUNET_NO);
170       end = GNUNET_TIME_absolute_get();
171
172       exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
173
174       mlp_no_opt_avg += exec_time[c];
175       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176           "MLP iterations no optimization %i: %llu \n",
177           c, exec_time[c]);
178   }
179
180   glp_delete_prob(prob);
181 }
182
183
184 void bench_mlp_with_optimization(char * file, int executions, int changes)
185 {
186   int c;
187   prob = glp_create_prob();
188   glp_read_lp(prob, NULL, file);
189
190   solve_lp(GNUNET_YES);
191
192   for (c=0; c<executions;c++)
193   {
194     start = GNUNET_TIME_absolute_get();
195     //modify_qm(906, 0, 0);
196     solve_lp(GNUNET_NO);
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_with_opt_avg += exec_time[c];
203     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204         "MLP /w optimization iterations %i: %llu \n",
205         c, exec_time[c]);
206   }
207
208   glp_delete_prob(prob);
209 }
210
211 #if 0
212 void modify_cr (int start, int length, int count)
213 {
214   //int * ind = GNUNET_malloc (length * sizeof (int));
215   //double *val = GNUNET_malloc (length * sizeof (double));
216   int ind[500];
217   double val[500];
218   int res = 0;
219   int c = start, c2=1;
220   while (c<=(start+count))
221   {
222     res = glp_get_mat_row(prob, c, ind, val);
223
224     printf("row index: %i non-zero elements: %i \n", c, res);
225     for (c2=1; c2<=res; c2++)
226     {
227             printf("%i = %f ", ind[c2], val[c2]);
228     }
229     c++;
230     printf ("\n----\n");
231   }
232   //glp_set_mat_row(prob, start, length, ind, val);
233 }
234 #endif
235 #endif
236
237 int main (int argc, char *argv[])
238 {
239   int ret = 0;
240   GNUNET_log_setup ("perf-transport-ats",
241 #if VERBOSE
242                     "DEBUG",
243 #else
244                     "INFO",
245 #endif
246                     NULL);
247
248 #if !HAVE_LIBGLPK
249         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, exiting testcase\n");
250         return 0;
251 #else
252
253   int nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
254   if (nullfd < 0)
255     return GNUNET_SYSERR;
256   if (dup2 (nullfd, 1) < 0)
257   {
258     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
259     (void) CLOSE (nullfd);
260     return GNUNET_SYSERR;
261   }
262
263
264   char * file = "ats_mlp_p100_m400.problem";
265
266   bench_simplex_no_optimization (file, executions);
267   bench_simplex_optimization (file, executions);
268   bench_mlp_no_optimization (file, executions);
269   bench_mlp_with_optimization (file, executions, 0);
270
271   // -> 400 addresses
272   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273       "Simplex, no optimization, average per address: %f\n",
274       ((double) sim_no_opt_avg / EXECS) / 400);
275   GAUGER ("TRANSPORT","GLPK simplex  no optimization",
276       ((double) sim_no_opt_avg  / EXECS) / 400, "ms/address");
277
278   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
279       "Simplex, with optimization, average per address: %f\n",
280       ((double) sim_with_opt_avg / EXECS) / 400);
281   GAUGER ("TRANSPORT",
282       "GLPK simplex, 100 peers 400 addresses with optimization",
283       ((double) sim_with_opt_avg  / EXECS) / 400, "ms/address");
284
285   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286       "MLP no optimization average per address: %f\n",
287       ((double) mlp_no_opt_avg  / EXECS) / 400);
288   GAUGER ("TRANSPORT","GLPK MLP 100 peers 400 addresses no optimization",
289       ((double) mlp_no_opt_avg  / EXECS) / 400, "ms/address");
290
291   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292       "MLP optimization average per address: %f\n",
293       ((double) mlp_with_opt_avg/ EXECS) / 400);
294   GAUGER ("TRANSPORT",
295       "GLPK MLP 100 peers 400 addresses with optimization",
296       ((double) mlp_with_opt_avg  / EXECS) / 400, "ms/address");
297   (void) CLOSE (nullfd);
298
299 #endif
300   return ret;
301 }
302
303 /* end of per_transport_ats.c*/
304