glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / test_speedup.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011-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_speedup.c
17  * @brief testcase for speedup.c
18  */
19 #include "platform.h"
20 #include "gnunet_util_lib.h"
21
22 /**
23  * Start time of the testcase
24  */
25 static struct GNUNET_TIME_Absolute start;
26
27 /**
28  * End-time of the testcase (affected by speed-up)
29  */
30 static struct GNUNET_TIME_Absolute end;
31
32 /**
33  * Number of cycles we have spent in 'run'.
34  */
35 static unsigned int cycles;
36
37
38 /**
39  * Main task that is scheduled with the speed-up.
40  *
41  * @param cls NULL
42  * @param tc scheduler context, unused
43  */
44 static void
45 run (void *cls)
46 {
47   cycles++;
48   fprintf (stderr, "..%u", cycles);
49   if (cycles <= 5)
50   {
51     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
52                                   &run,
53                                   NULL);
54     return;
55   }
56   end = GNUNET_TIME_absolute_get();
57   fprintf (stderr, "\n");
58   fflush(stdout);
59 }
60
61
62 /**
63  *
64  */
65 static void
66 check (void *cls,
67        char *const *args,
68        const char *cfgfile,
69        const struct GNUNET_CONFIGURATION_Handle *cfg)
70 {
71   fprintf (stderr, "0");
72   fflush (stdout);
73   GNUNET_SCHEDULER_add_now (&run, NULL);
74 }
75
76
77 int
78 main (int argc, char *argv[])
79 {
80   static char *const argvn[] = {
81     "test-speedup",
82     "-c",  "test_speedup_data.conf",
83     NULL
84   };
85   static struct GNUNET_GETOPT_CommandLineOption options[] = {
86     GNUNET_GETOPT_OPTION_END
87   };
88   time_t start_real;
89   time_t end_real;
90   struct GNUNET_TIME_Relative delta;
91
92   start_real = time (NULL);
93   start = GNUNET_TIME_absolute_get();
94   GNUNET_PROGRAM_run ((sizeof (argvn) / sizeof (char *)) - 1, argvn, "test-speedup",
95                       "nohelp", options, &check, NULL);
96
97   end_real = time (NULL);
98   delta = GNUNET_TIME_absolute_get_difference (start, end);
99
100   if (delta.rel_value_us >  ((end_real - start_real) * 1500LL * 1000LL))
101   {
102     GNUNET_log  (GNUNET_ERROR_TYPE_DEBUG,
103                  "Execution time in GNUnet time: %s\n",
104                  GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
105     GNUNET_log  (GNUNET_ERROR_TYPE_DEBUG,
106                  "Execution time in system time: %llu ms\n",
107                  (unsigned long long) ((end_real - start_real) * 1000LL));
108     return 0;
109   }
110   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111               "Execution time in GNUnet time: %s\n",
112               GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
113   GNUNET_log  (GNUNET_ERROR_TYPE_ERROR,
114                "Execution time in system time: %llu ms\n",
115                (unsigned long long) ((end_real - start_real) * 1000LL));
116   return 1;
117 }
118
119 /* end of test_speedup.c */