c55f28e9b0916096c955e060fa0d63a32eb5c6e3
[oweals/gnunet.git] / src / util / test_scheduler_delay.c
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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29 static struct GNUNET_TIME_Absolute target;
30
31 static int i;
32
33 static unsigned long long cumDelta;
34
35 #define INCR 47
36
37 #define MAXV 5000
38
39
40 /**
41  * Signature of the main function of a task.
42  *
43  * @param cls closure
44  */
45 static void
46 test_task (void *cls)
47 {
48   struct GNUNET_TIME_Absolute now;
49
50   now = GNUNET_TIME_absolute_get ();
51   if (now.abs_value_us > target.abs_value_us)
52     cumDelta += (now.abs_value_us - target.abs_value_us);
53   else
54     cumDelta += (target.abs_value_us - now.abs_value_us);
55   target =
56       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
57                                         (GNUNET_TIME_UNIT_MICROSECONDS, i));
58   FPRINTF (stderr, "%s",  ".");
59   if (i > MAXV)
60   {
61     FPRINTF (stderr, "%s",  "\n");
62     return;
63   }
64   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
65                                 (GNUNET_TIME_UNIT_MICROSECONDS, i), &test_task,
66                                 NULL);
67   i += INCR;
68 }
69
70
71 int
72 main (int argc, char *argv[])
73 {
74   GNUNET_log_setup ("test-scheduler-delay", "WARNING", NULL);
75   target = GNUNET_TIME_absolute_get ();
76   GNUNET_SCHEDULER_run (&test_task, NULL);
77   FPRINTF (stdout,
78            "Sleep precision: %llu microseconds (average delta). ",
79            cumDelta / (MAXV / INCR));
80   if (cumDelta <= 500 * MAXV / INCR)
81     FPRINTF (stdout, "%s",  "Timer precision is excellent.\n");
82   else if (cumDelta <= 5000 * MAXV / INCR)        /* 5 ms average deviation */
83     FPRINTF (stdout, "%s",  "Timer precision is good.\n");
84   else if (cumDelta > 25000 * MAXV / INCR)
85     FPRINTF (stdout, "%s",  "Timer precision is awful.\n");
86   else
87     FPRINTF (stdout, "%s",  "Timer precision is acceptable.\n");
88   return 0;
89 }
90
91 /* end of test_scheduler_delay.c */