aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_os_load.c
blob: e45c4ac36f5fdc262ef65531c47432f15a6e9513 (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
/*
     This file is part of GNUnet.
     (C) 2003, 2004, 2005, 2006, 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 2, 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 util/test_os_load.c
 * @brief testcase for util/os_load.c
 */
#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_configuration_lib.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_disk_lib.h"
#include "gnunet_os_lib.h"
#include "gnunet_time_lib.h"

#define VERBOSE 0

static int
testcpu ()
{
  static long k;
  int ret;
  struct GNUNET_TIME_Absolute start;
  struct GNUNET_CONFIGURATION_Handle *cfg;

  fprintf (stderr, "CPU load test, this may take a while.");
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_assert (cfg != NULL);
  /* need to run each phase for more than 10s since
     statuscalls only refreshes that often... */
  GNUNET_CONFIGURATION_set_value_number (cfg, "LOAD", "MAXCPULOAD", 100);
  GNUNET_OS_load_cpu_get (cfg);
  start = GNUNET_TIME_absolute_get ();
  while ((GNUNET_TIME_absolute_get_duration (start).value < 120 * 1000) &&
         (0 != GNUNET_OS_load_cpu_get (cfg)))
    sleep (1);
  start = GNUNET_TIME_absolute_get ();
  ret = GNUNET_OS_load_cpu_get (cfg);
  if (ret > 10)
    {
      fprintf (stderr,
               "\nWARNING: base load too high (%d) to run CPU load test.\n",
               ret);
      GNUNET_CONFIGURATION_destroy (cfg);
      return 0;
    }
  if (ret == -1)
    {
      fprintf (stderr, "\nWARNING: CPU load determination not supported.\n");
      GNUNET_CONFIGURATION_destroy (cfg);
      return 0;
    }
  while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000)
    {
      k++;                      /* do some processing to drive load up */
      if (ret < GNUNET_OS_load_cpu_get (cfg))
        break;
    }
  if (ret >= GNUNET_OS_load_cpu_get (cfg))
    {
      fprintf (stderr,
               "\nbusy loop failed to increase CPU load: %d >= %d.",
               ret, GNUNET_OS_load_cpu_get (cfg));
    }
  else
    {
#if VERBOSE
      fprintf (stderr,
               "\nbusy loop increased CPU load: %d < %d.",
               ret, GNUNET_OS_load_cpu_get (cfg));
#endif
    }
  fprintf (stderr, "\n");


  GNUNET_CONFIGURATION_destroy (cfg);
  return 0;
}

static int
testdisk ()
{
  int ret;
  struct GNUNET_DISK_FileHandle *fh;
  char buf[65536];
  struct GNUNET_TIME_Absolute start;
  struct GNUNET_CONFIGURATION_Handle *cfg;

  fprintf (stderr, "IO load test, this may take a while.");
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_assert (cfg != NULL);
  /* need to run each phase for more than 10s since
     statuscalls only refreshes that often... */
  GNUNET_CONFIGURATION_set_value_number (cfg, "LOAD", "MAXIOLOAD", 100);
  GNUNET_OS_load_disk_get (cfg);
  start = GNUNET_TIME_absolute_get ();
  while ((GNUNET_TIME_absolute_get_duration (start).value < 12 * 1000) &&
         (0 != GNUNET_OS_load_disk_get (cfg)))
    sleep (1);
  start = GNUNET_TIME_absolute_get ();
  ret = GNUNET_OS_load_disk_get (cfg);
  if (ret > 10)
    {
      fprintf (stderr,
               "WARNING: base load too high (%d) to run IO load test.\n",
               ret);
      GNUNET_CONFIGURATION_destroy (cfg);
      return 0;
    }
  if (ret == -1)
    {
      fprintf (stderr, "WARNING: IO load determination not supported.\n");
      GNUNET_CONFIGURATION_destroy (cfg);
      return 0;
    }
  memset (buf, 42, sizeof (buf));
  fh = GNUNET_DISK_file_open (".loadfile", GNUNET_DISK_OPEN_WRITE
                              | GNUNET_DISK_OPEN_CREATE,
                              GNUNET_DISK_PERM_USER_READ |
                              GNUNET_DISK_PERM_USER_WRITE);
  GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
  while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000)
    {
      GNUNET_DISK_file_seek (fh,
                             GNUNET_CRYPTO_random_u64
                             (GNUNET_CRYPTO_QUALITY_WEAK, 1024 * 1024 * 1024),
                             GNUNET_DISK_SEEK_SET);
      GNUNET_assert (sizeof (buf) ==
                     GNUNET_DISK_file_write (fh, buf, sizeof (buf)));
      GNUNET_DISK_file_sync (fh);
      if (ret < GNUNET_OS_load_disk_get (cfg))
        break;
    }
  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
  GNUNET_break (0 == UNLINK (".loadfile"));
  if (ret >= GNUNET_OS_load_disk_get (cfg))
    {
      fprintf (stderr,
               "\nbusy loop failed to increase IO load: %d >= %d.",
               ret, GNUNET_OS_load_disk_get (cfg));
      ret = 0;
    }
  else
    {
#if VERBOSE
      fprintf (stderr,
               "\nbusy loop increased disk load: %d < %d.",
               ret, GNUNET_OS_load_disk_get (cfg));
#endif
      ret = 0;
    }
  fprintf (stderr, "\n");
  GNUNET_CONFIGURATION_destroy (cfg);
  return ret;
}

int
main (int argc, char *argv[])
{
  int errCnt = 0;

  GNUNET_log_setup ("test-os-load", "WARNING", NULL);
  if (0 != testcpu ())
    errCnt++;
  if (0 != testdisk ())
    errCnt++;
  return errCnt;
}