-remove trailing whitespace
[oweals/gnunet.git] / src / util / test_scheduler_delay.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2013 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 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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  * @param tc context
45  */
46 static void
47 test_task (void *cls,
48            const struct GNUNET_SCHEDULER_TaskContext *tc)
49 {
50   struct GNUNET_TIME_Absolute now;
51
52   now = GNUNET_TIME_absolute_get ();
53   if (now.abs_value_us > target.abs_value_us)
54     cumDelta += (now.abs_value_us - target.abs_value_us);
55   else
56     cumDelta += (target.abs_value_us - now.abs_value_us);
57   target =
58       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
59                                         (GNUNET_TIME_UNIT_MICROSECONDS, i));
60   FPRINTF (stderr, "%s",  ".");
61   if (i > MAXV)
62   {
63     FPRINTF (stderr, "%s",  "\n");
64     return;
65   }
66   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
67                                 (GNUNET_TIME_UNIT_MICROSECONDS, i), &test_task,
68                                 NULL);
69   i += INCR;
70 }
71
72
73 int
74 main (int argc, char *argv[])
75 {
76   GNUNET_log_setup ("test-scheduler-delay", "WARNING", NULL);
77   target = GNUNET_TIME_absolute_get ();
78   GNUNET_SCHEDULER_run (&test_task, NULL);
79   FPRINTF (stdout,
80            "Sleep precision: %llu microseconds (average delta). ",
81            cumDelta / (MAXV / INCR));
82   if (cumDelta <= 500 * MAXV / INCR)
83     FPRINTF (stdout, "%s",  "Timer precision is excellent.\n");
84   else if (cumDelta <= 5000 * MAXV / INCR)        /* 5 ms average deviation */
85     FPRINTF (stdout, "%s",  "Timer precision is good.\n");
86   else if (cumDelta > 25000 * MAXV / INCR)
87     FPRINTF (stdout, "%s",  "Timer precision is awful.\n");
88   else
89     FPRINTF (stdout, "%s",  "Timer precision is acceptable.\n");
90   return 0;
91 }
92
93 /* end of test_scheduler_delay.c */