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