(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 make
54 }
55
56 void solve_lp(int presolve)
57 {
58   int result;
59   int solution;
60
61   glp_smcp opt_lp;
62   glp_init_smcp(&opt_lp);
63
64   opt_lp.msg_lev = GLP_MSG_OFF;
65   if (presolve==GNUNET_YES) opt_lp.presolve = GLP_ON;
66   else opt_lp.presolve = GLP_OFF;
67
68   result = glp_simplex(prob, &opt_lp);
69   solution =  glp_get_status (prob);
70   GNUNET_assert ((solution == 5) && (result==0));
71 }
72
73 /* Modify quality constraint */
74 void modify_qm(int start, int length, int values_to_change)
75 {
76   //int * ind = GNUNET_malloc (length * sizeof (int));
77   //double *val = GNUNET_malloc (length * sizeof (double));
78   int ind[1000];
79   double val[1000];
80
81   int res = 0;
82   int c = start, c2=1;
83   while (c<=(start+values_to_change))
84   {
85     res = glp_get_mat_row(prob, c, ind, val);
86
87     printf("%i %i \n", c, res);
88     for (c2=0; c2<res; c2++)
89     {
90             printf("%i = %f \n", ind[c2], val[c2]);
91     }
92     c++;
93   }
94   //glp_set_mat_row(prob, start, length, ind, val);
95 }
96
97
98
99 void bench_simplex_optimization(char * file, int executions)
100 {
101   int c;
102
103   prob = glp_create_prob();
104   glp_read_lp(prob, NULL, file);
105
106   solve_lp(GNUNET_YES);
107
108   for (c=0; c<executions;c++)
109   {
110     start = GNUNET_TIME_absolute_get();
111     solve_lp(GNUNET_NO);
112     end = GNUNET_TIME_absolute_get();
113
114     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
115
116     sim_with_opt_avg += exec_time[c];
117     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118         "Simplex /w optimization iterations %i: %llu \n",  c, exec_time[c]);
119   }
120
121   glp_delete_prob(prob);
122 }
123
124
125 void bench_simplex_no_optimization(char * file, int executions)
126 {
127   int c;
128
129   prob = glp_create_prob();
130   glp_read_lp(prob, NULL, file);
131
132   for (c=0; c<executions;c++)
133   {
134     start = GNUNET_TIME_absolute_get();
135     solve_lp(GNUNET_YES);
136     end = GNUNET_TIME_absolute_get();
137
138     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
139
140     sim_no_opt_avg += exec_time[c];
141     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142         "Simplex iterations %i: %llu \n",
143         c, exec_time[c]);
144   }
145
146   glp_delete_prob(prob);
147 }
148
149 void bench_mlp_no_optimization(char * file, int executions)
150 {
151   int c;
152
153   prob = glp_create_prob();
154   glp_read_lp(prob, NULL, file);
155
156   for (c=0; c<executions;c++)
157   {
158       start = GNUNET_TIME_absolute_get();
159       solve_lp(GNUNET_YES);
160       solve_mlp (GNUNET_NO);
161       end = GNUNET_TIME_absolute_get();
162
163       exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
164
165       mlp_no_opt_avg += exec_time[c];
166       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167           "MLP iterations no optimization %i: %llu \n",
168           c, exec_time[c]);
169   }
170
171   glp_delete_prob(prob);
172 }
173
174
175 void bench_mlp_with_optimization(char * file, int executions, int changes)
176 {
177   int c;
178   prob = glp_create_prob();
179   glp_read_lp(prob, NULL, file);
180
181   solve_lp(GNUNET_YES);
182
183   for (c=0; c<executions;c++)
184   {
185     start = GNUNET_TIME_absolute_get();
186     //modify_qm(906, 0, 0);
187     solve_lp(GNUNET_NO);
188     solve_mlp (GNUNET_NO);
189     end = GNUNET_TIME_absolute_get();
190
191     exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;
192
193     mlp_with_opt_avg += exec_time[c];
194     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195         "MLP /w optimization iterations %i: %llu \n",
196         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 ("perf-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   int nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
245   if (nullfd < 0)
246     return GNUNET_SYSERR;
247   if (dup2 (nullfd, 1) < 0)
248   {
249     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
250     (void) CLOSE (nullfd);
251     return GNUNET_SYSERR;
252   }
253
254
255   char * file = "ats_mlp_p100_m400.problem";
256
257   bench_simplex_no_optimization (file, executions);
258   bench_simplex_optimization (file, executions);
259   bench_mlp_no_optimization (file, executions);
260   bench_mlp_with_optimization (file, executions, 0);
261
262   // -> 400 addresses
263   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264       "Simplex, no optimization, average per address: %f\n",
265       ((double) sim_no_opt_avg / EXECS) / 400);
266   GAUGER ("TRANSPORT","GLPK simplex  no optimization",
267       ((double) sim_no_opt_avg  / EXECS) / 400, "ms/address");
268
269   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
270       "Simplex, with optimization, average per address: %f\n",
271       ((double) sim_with_opt_avg / EXECS) / 400);
272   GAUGER ("TRANSPORT",
273       "GLPK simplex, 100 peers 400 addresses with optimization",
274       ((double) sim_with_opt_avg  / EXECS) / 400, "ms/address");
275
276   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
277       "MLP no optimization average per address: %f\n",
278       ((double) mlp_no_opt_avg  / EXECS) / 400);
279   GAUGER ("TRANSPORT","GLPK MLP 100 peers 400 addresses no optimization",
280       ((double) mlp_no_opt_avg  / EXECS) / 400, "ms/address");
281
282   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283       "MLP optimization average per address: %f\n",
284       ((double) mlp_with_opt_avg/ EXECS) / 400);
285   GAUGER ("TRANSPORT",
286       "GLPK MLP 100 peers 400 addresses with optimization",
287       ((double) mlp_with_opt_avg  / EXECS) / 400, "ms/address");
288
289 #endif
290   (void) CLOSE (nullfd);
291   return ret;
292 }
293
294 /* end of per_transport_ats.c*/
295