aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_scheduler_delay.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_scheduler_delay.c')
-rw-r--r--src/lib/util/test_scheduler_delay.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/lib/util/test_scheduler_delay.c b/src/lib/util/test_scheduler_delay.c
new file mode 100644
index 000000000..41990272a
--- /dev/null
+++ b/src/lib/util/test_scheduler_delay.c
@@ -0,0 +1,96 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file util/test_scheduler_delay.c
22 * @brief testcase for delay of scheduler, measures how
23 * precise the timers are. Expect values between 0.2 and 2 ms on
24 * modern machines.
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29
30static struct GNUNET_TIME_Absolute target;
31
32static int i;
33
34static unsigned long long cumDelta;
35
36#define INCR 47
37
38#define MAXV 5000
39
40
41/**
42 * Signature of the main function of a task.
43 *
44 * @param cls closure
45 */
46static void
47test_task (void *cls)
48{
49 struct GNUNET_TIME_Absolute now;
50
51 now = GNUNET_TIME_absolute_get ();
52 if (now.abs_value_us > target.abs_value_us)
53 cumDelta += (now.abs_value_us - target.abs_value_us);
54 else
55 cumDelta += (target.abs_value_us - now.abs_value_us);
56 target =
57 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
58 (GNUNET_TIME_UNIT_MICROSECONDS, i));
59 fprintf (stderr, "%s", ".");
60 if (i > MAXV)
61 {
62 fprintf (stderr, "%s", "\n");
63 return;
64 }
65 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
66 (GNUNET_TIME_UNIT_MICROSECONDS, i),
67 &test_task,
68 NULL);
69 i += INCR;
70}
71
72
73int
74main (int argc, char *argv[])
75{
76 GNUNET_log_setup ("test-scheduler-delay",
77 "WARNING",
78 NULL);
79 target = GNUNET_TIME_absolute_get ();
80 GNUNET_SCHEDULER_run (&test_task, NULL);
81 fprintf (stdout,
82 "Sleep precision: %llu microseconds (average delta). ",
83 cumDelta / (MAXV / INCR));
84 if (cumDelta <= 500 * MAXV / INCR)
85 fprintf (stdout, "%s", "Timer precision is excellent.\n");
86 else if (cumDelta <= 5000 * MAXV / INCR) /* 5 ms average deviation */
87 fprintf (stdout, "%s", "Timer precision is good.\n");
88 else if (cumDelta > 25000 * MAXV / INCR)
89 fprintf (stdout, "%s", "Timer precision is awful.\n");
90 else
91 fprintf (stdout, "%s", "Timer precision is acceptable.\n");
92 return 0;
93}
94
95
96/* end of test_scheduler_delay.c */