Merge branch 'master' of gnunet.org:gnunet
[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 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 /**
19  * @file util/test_scheduler_delay.c
20  * @brief testcase for delay of scheduler, measures how
21  *  precise the timers are.  Expect values between 0.2 and 2 ms on
22  *  modern machines.
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26
27 static struct GNUNET_TIME_Absolute target;
28
29 static int i;
30
31 static unsigned long long cumDelta;
32
33 #define INCR 47
34
35 #define MAXV 5000
36
37
38 /**
39  * Signature of the main function of a task.
40  *
41  * @param cls closure
42  */
43 static void
44 test_task (void *cls)
45 {
46   struct GNUNET_TIME_Absolute now;
47
48   now = GNUNET_TIME_absolute_get ();
49   if (now.abs_value_us > target.abs_value_us)
50     cumDelta += (now.abs_value_us - target.abs_value_us);
51   else
52     cumDelta += (target.abs_value_us - now.abs_value_us);
53   target =
54       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
55                                         (GNUNET_TIME_UNIT_MICROSECONDS, i));
56   FPRINTF (stderr, "%s",  ".");
57   if (i > MAXV)
58   {
59     FPRINTF (stderr, "%s",  "\n");
60     return;
61   }
62   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
63                                 (GNUNET_TIME_UNIT_MICROSECONDS, i),
64                                 &test_task,
65                                 NULL);
66   i += INCR;
67 }
68
69
70 int
71 main (int argc, char *argv[])
72 {
73   GNUNET_log_setup ("test-scheduler-delay",
74                     "WARNING",
75                     NULL);
76   target = GNUNET_TIME_absolute_get ();
77   GNUNET_SCHEDULER_run (&test_task, NULL);
78   FPRINTF (stdout,
79            "Sleep precision: %llu microseconds (average delta). ",
80            cumDelta / (MAXV / INCR));
81   if (cumDelta <= 500 * MAXV / INCR)
82     FPRINTF (stdout, "%s",  "Timer precision is excellent.\n");
83   else if (cumDelta <= 5000 * MAXV / INCR)        /* 5 ms average deviation */
84     FPRINTF (stdout, "%s",  "Timer precision is good.\n");
85   else if (cumDelta > 25000 * MAXV / INCR)
86     FPRINTF (stdout, "%s",  "Timer precision is awful.\n");
87   else
88     FPRINTF (stdout, "%s",  "Timer precision is acceptable.\n");
89   return 0;
90 }
91
92 /* end of test_scheduler_delay.c */