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