changed gauger calls
[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
55         glp_iocp opt_mlp;
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, 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 /* Modify quality constraint */
82 void modify_qm(int start, int length, int values_to_change)
83 {
84         //int * ind = GNUNET_malloc (length * sizeof (int));
85         //double *val = GNUNET_malloc (length * sizeof (double));
86         int ind[1000];
87         double val[1000];
88
89         int res = 0;
90         int c = start, c2=1;
91         while (c<=(start+values_to_change))
92         {
93                 res = glp_get_mat_row(prob, c, ind, val);
94
95                 printf("%i %i \n", c, res);
96                 for (c2=0; c2<res; c2++)
97                 {
98                         printf("%i = %f \n", ind[c2], val[c2]);
99                 }
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
111         int c;
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, "Simplex /w optimization iterations %i: %llu \n",  c, exec_time[c]);
127         }
128
129         glp_delete_prob(prob);
130 }
131
132
133 void bench_simplex_no_optimization(char * file, int executions)
134 {
135
136         int c;
137         prob = glp_create_prob();
138         glp_read_lp(prob, NULL, file);
139
140         for (c=0; c<executions;c++)
141         {
142                 start = GNUNET_TIME_absolute_get();
143                 solve_lp(GNUNET_YES);
144                 end = GNUNET_TIME_absolute_get();
145
146                 exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
147
148                 sim_no_opt_avg += exec_time[c];
149                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Simplex iterations %i: %llu \n",  c, exec_time[c]);
150         }
151
152         glp_delete_prob(prob);
153 }
154
155 void bench_mlp_no_optimization(char * file, int executions)
156 {
157
158         int c;
159         prob = glp_create_prob();
160         glp_read_lp(prob, NULL, file);
161
162         for (c=0; c<executions;c++)
163         {
164                 start = GNUNET_TIME_absolute_get();
165                 solve_lp(GNUNET_YES);
166                 solve_mlp (GNUNET_NO);
167                 end = GNUNET_TIME_absolute_get();
168
169                 exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
170
171                 mlp_no_opt_avg += exec_time[c];
172                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP iterations no optimization %i: %llu \n",  c, exec_time[c]);
173         }
174
175         glp_delete_prob(prob);
176 }
177
178
179 void bench_mlp_with_optimization(char * file, int executions, int changes)
180 {
181         int c;
182         prob = glp_create_prob();
183         glp_read_lp(prob, NULL, file);
184
185         solve_lp(GNUNET_YES);
186
187         for (c=0; c<executions;c++)
188         {
189                 start = GNUNET_TIME_absolute_get();
190                 //modify_qm(906, 0, 0);
191                 solve_lp(GNUNET_NO);
192                 solve_mlp (GNUNET_NO);
193                 end = GNUNET_TIME_absolute_get();
194
195                 exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
196
197                 mlp_with_opt_avg += exec_time[c];
198                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP /w optimization iterations %i: %llu \n",  c, exec_time[c]);
199         }
200
201         glp_delete_prob(prob);
202 }
203
204 #if 0
205 void modify_cr (int start, int length, int count)
206 {
207         //int * ind = GNUNET_malloc (length * sizeof (int));
208         //double *val = GNUNET_malloc (length * sizeof (double));
209         int ind[500];
210         double val[500];
211         int res = 0;
212         int c = start, c2=1;
213         while (c<=(start+count))
214         {
215                 res = glp_get_mat_row(prob, c, ind, val);
216
217                 printf("row index: %i non-zero elements: %i \n", c, res);
218                 for (c2=1; c2<=res; c2++)
219                 {
220                         printf("%i = %f ", ind[c2], val[c2]);
221                 }
222                 c++;
223                 printf ("\n----\n");
224         }
225         //glp_set_mat_row(prob, start, length, ind, val);
226 }
227 #endif
228 #endif
229
230 int main (int argc, char *argv[])
231 {
232         int ret = 0;
233         GNUNET_log_setup ("perf-transport-ats",
234 #if VERBOSE
235                     "DEBUG",
236 #else
237                     "INFO",
238 #endif
239                     NULL);
240
241 #if !HAVE_LIBGLPK
242         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, exiting testcase\n");
243         return 0;
244 #else
245
246   char * file = "ats_mlp_p100_m400.problem";
247  // char * file = "mlps/ats_mlp_p500_m2000.problem";
248   bench_simplex_no_optimization (file, executions);
249   bench_simplex_optimization (file, executions);
250   bench_mlp_no_optimization (file, executions);
251   bench_mlp_with_optimization (file, executions, 0);
252
253   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Simplex no optimization average: %llu\n", sim_no_opt_avg  / EXECS);
254   // -> 400 addresses
255   GAUGER ("TRANSPORT","GLPK simplex  no optimization", (sim_no_opt_avg  / EXECS) / 400, "ms/address");
256   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Simplex, no optimization, average per peer: %llu\n", (sim_with_opt_avg / EXECS) / 400);
257   GAUGER ("TRANSPORT","GLPK simplex, 100 peers 400 addresses with optimization", (sim_with_opt_avg  / EXECS) / 400, "ms/address");
258   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP no optimization average: %llu\n", (mlp_no_opt_avg  / EXECS) / 400);
259   GAUGER ("TRANSPORT","GLPK MLP 100 peers 400 addresses no optimization", (mlp_no_opt_avg  / EXECS) / 400, "ms/address");
260   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP optimization average: %llu\n", (mlp_with_opt_avg/ EXECS) / 400);
261   GAUGER ("TRANSPORT","GLPK MLP 100 peers 400 addresses with optimization", (mlp_with_opt_avg  / EXECS) / 400, "ms/address");
262
263 #endif
264   return ret;
265 }
266
267 /* end of per_transport_ats.c*/