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