aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_os_load.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_os_load.c')
-rw-r--r--src/util/test_os_load.c182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/util/test_os_load.c b/src/util/test_os_load.c
new file mode 100644
index 000000000..2f82f60c7
--- /dev/null
+++ b/src/util/test_os_load.c
@@ -0,0 +1,182 @@
1/*
2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 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 2, 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 util/test_os_load.c
22 * @brief testcase for util/os_load.c
23 */
24#include "platform.h"
25#include "gnunet_common.h"
26#include "gnunet_configuration_lib.h"
27#include "gnunet_crypto_lib.h"
28#include "gnunet_disk_lib.h"
29#include "gnunet_os_lib.h"
30#include "gnunet_time_lib.h"
31
32#define VERBOSE 0
33
34static int
35testcpu ()
36{
37 static long k;
38 int ret;
39 struct GNUNET_TIME_Absolute start;
40 struct GNUNET_CONFIGURATION_Handle *cfg;
41
42 fprintf (stderr, "CPU load test, this may take a while.");
43 cfg = GNUNET_CONFIGURATION_create ();
44 GNUNET_assert (cfg != NULL);
45 /* need to run each phase for more than 10s since
46 statuscalls only refreshes that often... */
47 GNUNET_CONFIGURATION_set_value_number (cfg, "LOAD", "MAXCPULOAD", 100);
48 GNUNET_OS_load_cpu_get (cfg);
49 start = GNUNET_TIME_absolute_get ();
50 while ((GNUNET_TIME_absolute_get_duration (start).value < 120 * 1000) &&
51 (0 != GNUNET_OS_load_cpu_get (cfg)))
52 sleep (1);
53 start = GNUNET_TIME_absolute_get ();
54 ret = GNUNET_OS_load_cpu_get (cfg);
55 if (ret > 10)
56 {
57 fprintf (stderr,
58 "\nWARNING: base load too high (%d) to run CPU load test.\n",
59 ret);
60 GNUNET_CONFIGURATION_destroy (cfg);
61 return 0;
62 }
63 if (ret == -1)
64 {
65 fprintf (stderr, "\nWARNING: CPU load determination not supported.\n");
66 GNUNET_CONFIGURATION_destroy (cfg);
67 return 0;
68 }
69 while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000)
70 {
71 k++; /* do some processing to drive load up */
72 if (ret < GNUNET_OS_load_cpu_get (cfg))
73 break;
74 }
75 if (ret >= GNUNET_OS_load_cpu_get (cfg))
76 {
77 fprintf (stderr,
78 "\nbusy loop failed to increase CPU load: %d >= %d.",
79 ret, GNUNET_OS_load_cpu_get (cfg));
80 ret = 1;
81 }
82 else
83 {
84#if VERBOSE
85 fprintf (stderr,
86 "\nbusy loop increased CPU load: %d < %d.",
87 ret, GNUNET_OS_load_cpu_get (cfg));
88#endif
89 ret = 0;
90 }
91 fprintf (stderr, "\n");
92
93
94 GNUNET_CONFIGURATION_destroy (cfg);
95 return ret;
96}
97
98static int
99testdisk ()
100{
101 int ret;
102 int fd;
103 char buf[65536];
104 struct GNUNET_TIME_Absolute start;
105 struct GNUNET_CONFIGURATION_Handle *cfg;
106
107 fprintf (stderr, "IO load test, this may take a while.");
108 cfg = GNUNET_CONFIGURATION_create ();
109 GNUNET_assert (cfg != NULL);
110 /* need to run each phase for more than 10s since
111 statuscalls only refreshes that often... */
112 GNUNET_CONFIGURATION_set_value_number (cfg, "LOAD", "MAXIOLOAD", 100);
113 GNUNET_OS_load_disk_get (cfg);
114 start = GNUNET_TIME_absolute_get ();
115 while ((GNUNET_TIME_absolute_get_duration (start).value < 12 * 1000) &&
116 (0 != GNUNET_OS_load_disk_get (cfg)))
117 sleep (1);
118 start = GNUNET_TIME_absolute_get ();
119 ret = GNUNET_OS_load_disk_get (cfg);
120 if (ret > 10)
121 {
122 fprintf (stderr,
123 "WARNING: base load too high (%d) to run IO load test.\n",
124 ret);
125 GNUNET_CONFIGURATION_destroy (cfg);
126 return 0;
127 }
128 if (ret == -1)
129 {
130 fprintf (stderr, "WARNING: IO load determination not supported.\n");
131 GNUNET_CONFIGURATION_destroy (cfg);
132 return 0;
133 }
134 memset (buf, 42, sizeof (buf));
135 fd =
136 GNUNET_DISK_file_open (".loadfile", O_WRONLY | O_CREAT,
137 S_IRUSR | S_IWUSR);
138 GNUNET_assert (fd != -1);
139 while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000)
140 {
141 LSEEK (fd, GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
142 1024 * 1024 * 1024), SEEK_SET);
143 GNUNET_assert (sizeof (buf) == WRITE (fd, buf, sizeof (buf)));
144 fsync (fd);
145 if (ret < GNUNET_OS_load_disk_get (cfg))
146 break;
147 }
148 GNUNET_break (0 == CLOSE (fd));
149 GNUNET_break (0 == UNLINK (".loadfile"));
150 if (ret >= GNUNET_OS_load_disk_get (cfg))
151 {
152 fprintf (stderr,
153 "\nbusy loop failed to increase IO load: %d >= %d.",
154 ret, GNUNET_OS_load_disk_get (cfg));
155 ret = 1;
156 }
157 else
158 {
159#if VERBOSE
160 fprintf (stderr,
161 "\nbusy loop increased disk load: %d < %d.",
162 ret, GNUNET_OS_load_disk_get (cfg));
163#endif
164 ret = 0;
165 }
166 fprintf (stderr, "\n");
167 GNUNET_CONFIGURATION_destroy (cfg);
168 return 0;
169}
170
171int
172main (int argc, char *argv[])
173{
174 int errCnt = 0;
175
176 GNUNET_log_setup ("test-os-load", "WARNING", NULL);
177 if (0 != testcpu ())
178 errCnt++;
179 if (0 != testdisk ())
180 errCnt++;
181 return errCnt;
182}