gaugering
[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                    "Time to PUT %u items in %s-datacache_ms",
108                    ITERATIONS,
109                    plugins_name);
110   GAUGER (GNUNET_TIME_absolute_get_duration(start).rel_value,
111           gstr);  
112   start = GNUNET_TIME_absolute_get ();
113   memset (&k, 0, sizeof (GNUNET_HashCode));
114   for (i = 0; i < ITERATIONS; i++)
115     {
116       if (0 == i % (ITERATIONS / 80))
117         fprintf (stderr, ".");
118       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
119       GNUNET_DATACACHE_get (h, &k, 1+i%16,
120                             &checkIt, &n);
121       k = n;
122     }
123   fprintf (stderr, "\n");
124   fprintf (stdout, "Found %u/%u items in %llums (%u were deleted during storage processing)\n",
125            found, ITERATIONS,
126            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).rel_value,
127            ITERATIONS - found);
128   GNUNET_snprintf (gstr, sizeof (gstr),
129                    "Time to try to GET %u items from %s-datacache_ms",
130                    ITERATIONS,
131                    plugins_name);
132   GAUGER (GNUNET_TIME_absolute_get_duration(start).rel_value,
133           gstr);  
134            
135   GNUNET_DATACACHE_destroy (h);
136   ASSERT (ok == 0);
137   return;
138 FAILURE:
139   if (h != NULL)
140     GNUNET_DATACACHE_destroy (h);
141   ok = GNUNET_SYSERR;
142 }
143
144
145 int
146 main (int argc, char *argv[])
147 {
148   char *pos;
149   char cfg_name[128];
150   char *const xargv[] = { 
151     "perf-datacache",
152     "-c",
153     cfg_name,
154 #if VERBOSE
155     "-L", "DEBUG",
156 #endif
157     NULL
158   };
159   struct GNUNET_GETOPT_CommandLineOption options[] = {
160     GNUNET_GETOPT_OPTION_END
161   };
162   
163   GNUNET_log_setup ("perf-datacache",
164 #if VERBOSE
165                     "DEBUG",
166 #else
167                     "WARNING",
168 #endif
169                     NULL);
170   /* determine name of plugin to use */
171   plugin_name = argv[0];
172   while (NULL != (pos = strstr(plugin_name, "_")))
173     plugin_name = pos+1;
174   if (NULL != (pos = strstr(plugin_name, ".")))
175     pos[0] = 0;
176   else
177     pos = (char *) plugin_name;
178   
179   GNUNET_snprintf (cfg_name,
180                    sizeof (cfg_name),
181                    "perf_datacache_data_%s.conf",
182                    plugin_name);
183   if (pos != plugin_name)
184     pos[0] = '.';
185   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
186                       xargv, "perf-datacache", "nohelp",
187                       options, &run, NULL);
188   if (ok != 0)
189     fprintf (stderr, "Missed some perfcases: %d\n", ok);
190   return ok;
191 }
192
193 /* end of perf_datacache.c */