indentation
[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, const char *data, enum GNUNET_BLOCK_Type type)
51 {
52   if ((size == sizeof (GNUNET_HashCode)) && (0 == memcmp (data, cls, size)))
53     found++;
54   return GNUNET_OK;
55 }
56
57
58 static void
59 run (void *cls,
60      char *const *args,
61      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
62 {
63   struct GNUNET_DATACACHE_Handle *h;
64   GNUNET_HashCode k;
65   GNUNET_HashCode n;
66   struct GNUNET_TIME_Absolute exp;
67   struct GNUNET_TIME_Absolute start;
68   unsigned int i;
69   char gstr[128];
70
71   ok = 0;
72   h = GNUNET_DATACACHE_create (cfg, "perfcache");
73
74   if (h == NULL)
75   {
76     fprintf (stderr,
77              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
78     return;
79   }
80   exp = GNUNET_TIME_absolute_get ();
81   start = exp;
82   exp.abs_value += 5 * 60 * 1000;
83   memset (&k, 0, sizeof (GNUNET_HashCode));
84   for (i = 0; i < ITERATIONS; i++)
85   {
86     if (0 == i % (ITERATIONS / 80))
87       fprintf (stderr, ".");
88     GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
89     ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
90                                                &k,
91                                                sizeof (GNUNET_HashCode),
92                                                (const char *) &n,
93                                                1 + i % 16, exp));
94     k = n;
95   }
96   fprintf (stderr, "\n");
97   fprintf (stdout, "Stored %u items in %llums\n",
98            ITERATIONS,
99            (unsigned long long)
100            GNUNET_TIME_absolute_get_duration (start).rel_value);
101   GNUNET_snprintf (gstr, sizeof (gstr), "DATACACHE-%s", plugin_name);
102   GAUGER (gstr,
103           "Time to PUT item in datacache",
104           GNUNET_TIME_absolute_get_duration (start).rel_value / ITERATIONS,
105           "ms/item");
106   start = GNUNET_TIME_absolute_get ();
107   memset (&k, 0, sizeof (GNUNET_HashCode));
108   for (i = 0; i < ITERATIONS; i++)
109   {
110     if (0 == i % (ITERATIONS / 80))
111       fprintf (stderr, ".");
112     GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
113     GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n);
114     k = n;
115   }
116   fprintf (stderr, "\n");
117   fprintf (stdout,
118            "Found %u/%u items in %llums (%u were deleted during storage processing)\n",
119            found, ITERATIONS,
120            (unsigned long long)
121            GNUNET_TIME_absolute_get_duration (start).rel_value,
122            ITERATIONS - found);
123   if (found > 0)
124     GAUGER (gstr,
125             "Time to GET item from datacache",
126             GNUNET_TIME_absolute_get_duration (start).rel_value / found,
127             "ms/item");
128   GNUNET_DATACACHE_destroy (h);
129   ASSERT (ok == 0);
130   return;
131 FAILURE:
132   if (h != NULL)
133     GNUNET_DATACACHE_destroy (h);
134   ok = GNUNET_SYSERR;
135 }
136
137
138 int
139 main (int argc, char *argv[])
140 {
141   char *pos;
142   char cfg_name[128];
143
144   char *const xargv[] = {
145     "perf-datacache",
146     "-c",
147     cfg_name,
148 #if VERBOSE
149     "-L", "DEBUG",
150 #endif
151     NULL
152   };
153   struct GNUNET_GETOPT_CommandLineOption options[] = {
154     GNUNET_GETOPT_OPTION_END
155   };
156
157   GNUNET_log_setup ("perf-datacache",
158 #if VERBOSE
159                     "DEBUG",
160 #else
161                     "WARNING",
162 #endif
163                     NULL);
164   /* determine name of plugin to use */
165   plugin_name = argv[0];
166   while (NULL != (pos = strstr (plugin_name, "_")))
167     plugin_name = pos + 1;
168   if (NULL != (pos = strstr (plugin_name, ".")))
169     pos[0] = 0;
170   else
171     pos = (char *) plugin_name;
172
173   GNUNET_snprintf (cfg_name,
174                    sizeof (cfg_name),
175                    "perf_datacache_data_%s.conf", plugin_name);
176   if (pos != plugin_name)
177     pos[0] = '.';
178   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
179                       xargv, "perf-datacache", "nohelp", options, &run, NULL);
180   if (ok != 0)
181     fprintf (stderr, "Missed some perfcases: %d\n", ok);
182   return ok;
183 }
184
185 /* end of perf_datacache.c */