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