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