Consistent GAUGER naming.
[oweals/gnunet.git] / src / datacache / perf_datacache.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009, 2010 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 datacache/perf_datacache.c
22  * @brief Performance evaluation for the datacache implementations.
23  * @author Nils Durner
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_datacache_lib.h"
28 #include <gauger.h>
29
30 #define VERBOSE GNUNET_NO
31
32 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
33
34 #define ITERATIONS 10000
35
36 static int ok;
37
38 static unsigned int found;
39
40 /**
41  * Name of plugin under test.
42  */
43 static const char *plugin_name;
44
45
46 static int
47 checkIt (void *cls,
48          struct GNUNET_TIME_Absolute exp,
49          const GNUNET_HashCode * key,
50          size_t size, 
51          const char *data, 
52          enum GNUNET_BLOCK_Type type)
53 {
54   if ( (size == sizeof (GNUNET_HashCode)) &&
55        (0 == memcmp (data, cls, size)) )
56     found++;
57   return GNUNET_OK;
58 }
59
60
61 static void
62 run (void *cls,
63      char *const *args,
64      const char *cfgfile,
65      const struct GNUNET_CONFIGURATION_Handle *cfg)
66 {
67   struct GNUNET_DATACACHE_Handle *h;
68   GNUNET_HashCode k;
69   GNUNET_HashCode n;
70   struct GNUNET_TIME_Absolute exp;
71   struct GNUNET_TIME_Absolute start;
72   unsigned int i;
73   char gstr[128];
74   
75   ok = 0;
76   h = GNUNET_DATACACHE_create (cfg,
77                                "perfcache");
78
79   if (h == NULL)
80     {
81       fprintf (stderr,
82                "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
83       return;
84     }
85   exp = GNUNET_TIME_absolute_get ();
86   start = exp;
87   exp.abs_value += 5 * 60 * 1000;
88   memset (&k, 0, sizeof (GNUNET_HashCode));
89   for (i = 0; i < ITERATIONS; i++)
90     {
91       if (0 == i % (ITERATIONS / 80))
92         fprintf (stderr, ".");
93       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
94       ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
95                                                  &k,
96                                                  sizeof (GNUNET_HashCode),
97                                                  (const char *) &n,
98                                                  1+i%16,
99                                                  exp));
100       k = n;
101     }
102   fprintf (stderr, "\n");
103   fprintf (stdout, "Stored %u items in %llums\n",
104            ITERATIONS,
105            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).rel_value);
106   GNUNET_snprintf (gstr, sizeof (gstr),
107                    "DATACACHE:Time to PUT %u items in %s-datacache",
108                    ITERATIONS,
109                    plugin_name);
110   GAUGER (gstr, GNUNET_TIME_absolute_get_duration(start).rel_value, "ms");
111   start = GNUNET_TIME_absolute_get ();
112   memset (&k, 0, sizeof (GNUNET_HashCode));
113   for (i = 0; i < ITERATIONS; i++)
114     {
115       if (0 == i % (ITERATIONS / 80))
116         fprintf (stderr, ".");
117       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
118       GNUNET_DATACACHE_get (h, &k, 1+i%16,
119                             &checkIt, &n);
120       k = n;
121     }
122   fprintf (stderr, "\n");
123   fprintf (stdout, "Found %u/%u items in %llums (%u were deleted during storage processing)\n",
124            found, ITERATIONS,
125            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).rel_value,
126            ITERATIONS - found);
127   GNUNET_snprintf (gstr, sizeof (gstr),
128                    "DATACACHE:Time to try to GET %u items from %s-datacache",
129                    ITERATIONS,
130                    plugin_name);
131   GAUGER (gstr, GNUNET_TIME_absolute_get_duration(start).rel_value, "ms");
132            
133   GNUNET_DATACACHE_destroy (h);
134   ASSERT (ok == 0);
135   return;
136 FAILURE:
137   if (h != NULL)
138     GNUNET_DATACACHE_destroy (h);
139   ok = GNUNET_SYSERR;
140 }
141
142
143 int
144 main (int argc, char *argv[])
145 {
146   char *pos;
147   char cfg_name[128];
148   char *const xargv[] = { 
149     "perf-datacache",
150     "-c",
151     cfg_name,
152 #if VERBOSE
153     "-L", "DEBUG",
154 #endif
155     NULL
156   };
157   struct GNUNET_GETOPT_CommandLineOption options[] = {
158     GNUNET_GETOPT_OPTION_END
159   };
160   
161   GNUNET_log_setup ("perf-datacache",
162 #if VERBOSE
163                     "DEBUG",
164 #else
165                     "WARNING",
166 #endif
167                     NULL);
168   /* determine name of plugin to use */
169   plugin_name = argv[0];
170   while (NULL != (pos = strstr(plugin_name, "_")))
171     plugin_name = pos+1;
172   if (NULL != (pos = strstr(plugin_name, ".")))
173     pos[0] = 0;
174   else
175     pos = (char *) plugin_name;
176   
177   GNUNET_snprintf (cfg_name,
178                    sizeof (cfg_name),
179                    "perf_datacache_data_%s.conf",
180                    plugin_name);
181   if (pos != plugin_name)
182     pos[0] = '.';
183   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
184                       xargv, "perf-datacache", "nohelp",
185                       options, &run, NULL);
186   if (ok != 0)
187     fprintf (stderr, "Missed some perfcases: %d\n", ok);
188   return ok;
189 }
190
191 /* end of perf_datacache.c */