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