aboutsummaryrefslogtreecommitdiff
path: root/src/transport/perf_transport_ats.c
blob: 59e276d0937a48e4180a353004f4694773132ce6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/
/**
 * @file testing/per_transport_ats.c
 * @brief testcase for ats functionality
 */
#include "platform.h"
#include "gnunet_time_lib.h"
#include "gauger.h"
#if HAVE_LIBGLPK
#include <glpk.h>
#endif

#define VERBOSE GNUNET_NO

#define EXECS 5

static int ret = 0;

#if HAVE_LIBGLPK
static int executions = EXECS;
static uint64_t exec_time[EXECS];

static uint64_t sim_no_opt_avg;
static uint64_t sim_with_opt_avg;
static uint64_t mlp_no_opt_avg;
static uint64_t mlp_with_opt_avg;

static glp_prob * prob;

static struct GNUNET_TIME_Absolute start;
static struct GNUNET_TIME_Absolute end;

void solve_mlp(int presolve)
{
  int result, solution;
  glp_iocp opt_mlp;

  glp_init_iocp(&opt_mlp);
  opt_mlp.msg_lev = GLP_MSG_OFF;
  opt_mlp.presolve = GLP_OFF;

  result = glp_intopt (prob, &opt_mlp);
  solution =  glp_mip_status (prob);
  GNUNET_assert ((solution == 5) && (result==0));
}

void solve_lp(int presolve)
{
  int result;
  int solution;

  glp_smcp opt_lp;
  glp_init_smcp(&opt_lp);

  opt_lp.msg_lev = GLP_MSG_OFF;
  if (presolve==GNUNET_YES) opt_lp.presolve = GLP_ON;
  else opt_lp.presolve = GLP_OFF;

  result = glp_simplex(prob, &opt_lp);
  solution =  glp_get_status (prob);
  GNUNET_assert ((solution == 5) && (result==0));
}

/* Modify quality constraint */
void modify_qm(int start, int length, int values_to_change)
{
  //int * ind = GNUNET_malloc (length * sizeof (int));
  //double *val = GNUNET_malloc (length * sizeof (double));
  int ind[1000];
  double val[1000];

  int res = 0;
  int c = start, c2=1;
  while (c<=(start+values_to_change))
  {
    res = glp_get_mat_row(prob, c, ind, val);

    printf("%i %i \n", c, res);
    for (c2=0; c2<res; c2++)
    {
            printf("%i = %f \n", ind[c2], val[c2]);
    }
    c++;
  }
  //glp_set_mat_row(prob, start, length, ind, val);
}



void bench_simplex_optimization(char * file, int executions)
{
  int c;
  int res;

  prob = glp_create_prob();
  res = glp_read_lp(prob, NULL, file);
  if (res != 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
        "Problem file `%s' not found\n",  file);
    ret = 1;
    return;
  }

  solve_lp(GNUNET_YES);

  for (c=0; c<executions;c++)
  {
    start = GNUNET_TIME_absolute_get();
    solve_lp(GNUNET_NO);
    end = GNUNET_TIME_absolute_get();

    exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;

    sim_with_opt_avg += exec_time[c];
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
        "Simplex /w optimization iterations %i: %llu \n",  c, exec_time[c]);
  }

  glp_delete_prob(prob);
}


void bench_simplex_no_optimization(char * file, int executions)
{
  int c;
  int res;

  prob = glp_create_prob();
  res = glp_read_lp(prob, NULL, file);
  if (res != 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
        "Problem file `%s' not found\n",  file);
    ret = 1;
    return;
  }

  for (c=0; c<executions;c++)
  {
    start = GNUNET_TIME_absolute_get();
    solve_lp(GNUNET_YES);
    end = GNUNET_TIME_absolute_get();

    exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;

    sim_no_opt_avg += exec_time[c];
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
        "Simplex iterations %i: %llu \n",
        c, exec_time[c]);
  }

  glp_delete_prob(prob);
}

void bench_mlp_no_optimization(char * file, int executions)
{
  int c;
  int res;

  prob = glp_create_prob();
  res = glp_read_lp(prob, NULL, file);
  if (res != 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
        "Problem file `%s' not found\n",  file);
    ret = 1;
    return;
  }
  for (c=0; c<executions;c++)
  {
      start = GNUNET_TIME_absolute_get();
      solve_lp(GNUNET_YES);
      solve_mlp (GNUNET_NO);
      end = GNUNET_TIME_absolute_get();

      exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;

      mlp_no_opt_avg += exec_time[c];
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
          "MLP iterations no optimization %i: %llu \n",
          c, exec_time[c]);
  }

  glp_delete_prob(prob);
}


void bench_mlp_with_optimization(char * file, int executions, int changes)
{
  int c;
  int res;

  prob = glp_create_prob();
  res = glp_read_lp(prob, NULL, file);
  if (res != 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
        "Problem file `%s' not found\n",  file);
    ret = 1;
    return;
  }

  solve_lp(GNUNET_YES);

  for (c=0; c<executions;c++)
  {
    start = GNUNET_TIME_absolute_get();
    //modify_qm(906, 0, 0);
    solve_lp(GNUNET_NO);
    solve_mlp (GNUNET_NO);
    end = GNUNET_TIME_absolute_get();

    exec_time[c] = GNUNET_TIME_absolute_get_difference(start, end).rel_value;

    mlp_with_opt_avg += exec_time[c];
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
        "MLP /w optimization iterations %i: %llu \n",
        c, exec_time[c]);
  }

  glp_delete_prob(prob);
}

#if 0
void modify_cr (int start, int length, int count)
{
  //int * ind = GNUNET_malloc (length * sizeof (int));
  //double *val = GNUNET_malloc (length * sizeof (double));
  int ind[500];
  double val[500];
  int res = 0;
  int c = start, c2=1;
  while (c<=(start+count))
  {
    res = glp_get_mat_row(prob, c, ind, val);

    printf("row index: %i non-zero elements: %i \n", c, res);
    for (c2=1; c2<=res; c2++)
    {
            printf("%i = %f ", ind[c2], val[c2]);
    }
    c++;
    printf ("\n----\n");
  }
  //glp_set_mat_row(prob, start, length, ind, val);
}
#endif
#endif

int main (int argc, char *argv[])
{

  GNUNET_log_setup ("perf-transport-ats",
#if VERBOSE
                    "DEBUG",
#else
                    "INFO",
#endif
                    NULL);

#if !HAVE_LIBGLPK
	GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed, exiting testcase\n");
	return 0;
#else

  int nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
  if (nullfd < 0)
    return GNUNET_SYSERR;
  if (dup2 (nullfd, 1) < 0)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
    (void) CLOSE (nullfd);
    return GNUNET_SYSERR;
  }


  char * file = "ats_mlp_p100_m400.problem";

  bench_simplex_no_optimization (file, executions);
  bench_simplex_optimization (file, executions);
  bench_mlp_no_optimization (file, executions);
  bench_mlp_with_optimization (file, executions, 0);

  if (ret != 0)
    return ret;

  // -> 400 addresses
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      "Simplex, no optimization, average per address: %f\n",
      ((double) sim_no_opt_avg / EXECS) / 400);
  GAUGER ("TRANSPORT","GLPK simplex  no optimization",
      ((double) sim_no_opt_avg  / EXECS) / 400, "ms/address");

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      "Simplex, with optimization, average per address: %f\n",
      ((double) sim_with_opt_avg / EXECS) / 400);
  GAUGER ("TRANSPORT",
      "GLPK simplex, 100 peers 400 addresses with optimization",
      ((double) sim_with_opt_avg  / EXECS) / 400, "ms/address");

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      "MLP no optimization average per address: %f\n",
      ((double) mlp_no_opt_avg  / EXECS) / 400);
  GAUGER ("TRANSPORT","GLPK MLP 100 peers 400 addresses no optimization",
      ((double) mlp_no_opt_avg  / EXECS) / 400, "ms/address");

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      "MLP optimization average per address: %f\n",
      ((double) mlp_with_opt_avg/ EXECS) / 400);
  GAUGER ("TRANSPORT",
      "GLPK MLP 100 peers 400 addresses with optimization",
      ((double) mlp_with_opt_avg  / EXECS) / 400, "ms/address");
  (void) CLOSE (nullfd);

#endif
  return ret;
}

/* end of per_transport_ats.c*/