7f699f2af448ba0c058635823a3bdbdbbd0d972a
[oweals/gnunet.git] / src / datacache / perf_datacache.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2009, 2010 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /*
19  * @file datacache/perf_datacache.c
20  * @brief Performance evaluation for the datacache implementations.
21  * @author Nils Durner
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_datacache_lib.h"
26 #include "gnunet_testing_lib.h"
27 #include <gauger.h>
28
29
30 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
31
32 #define ITERATIONS 10000
33
34 static int ok;
35
36 static unsigned int found;
37
38 /**
39  * Name of plugin under test.
40  */
41 static const char *plugin_name;
42
43
44 static int
45 checkIt (void *cls,
46          const struct GNUNET_HashCode * key, size_t size, const char *data,
47          enum GNUNET_BLOCK_Type type,
48          struct GNUNET_TIME_Absolute exp,
49          unsigned int path_len,
50          const struct GNUNET_PeerIdentity *path)
51 {
52   if ((size == sizeof (struct GNUNET_HashCode)) && (0 == memcmp (data, cls, size)))
53     found++;
54   return GNUNET_OK;
55 }
56
57
58 static void
59 run (void *cls, char *const *args, const char *cfgfile,
60      const struct GNUNET_CONFIGURATION_Handle *cfg)
61 {
62   struct GNUNET_DATACACHE_Handle *h;
63   struct GNUNET_HashCode k;
64   struct GNUNET_HashCode n;
65   struct GNUNET_TIME_Absolute exp;
66   struct GNUNET_TIME_Absolute start;
67   unsigned int i;
68   char gstr[128];
69
70   ok = 0;
71   h = GNUNET_DATACACHE_create (cfg, "perfcache");
72
73   if (h == NULL)
74   {
75     FPRINTF (stderr, "%s", "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
76     ok = 77; /* mark test as skipped */
77     return;
78   }
79   exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
80   start = GNUNET_TIME_absolute_get ();
81   memset (&k, 0, sizeof (struct GNUNET_HashCode));
82   for (i = 0; i < ITERATIONS; i++)
83   {
84     if (0 == i % (ITERATIONS / 80))
85       FPRINTF (stderr, "%s",  ".");
86     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
87     ASSERT (GNUNET_OK ==
88             GNUNET_DATACACHE_put (h, &k, sizeof (struct GNUNET_HashCode),
89                                   (const char *) &n, 1 + i % 16, exp,
90                                   0, NULL));
91     k = n;
92   }
93   FPRINTF (stderr, "%s",  "\n");
94   FPRINTF (stdout, "Stored %u items in %s\n", ITERATIONS,
95            GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
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_us / 1000LL / ITERATIONS,
99           "ms/item");
100   start = GNUNET_TIME_absolute_get ();
101   memset (&k, 0, sizeof (struct GNUNET_HashCode));
102   for (i = 0; i < ITERATIONS; i++)
103   {
104     if (0 == i % (ITERATIONS / 80))
105       FPRINTF (stderr, "%s",  ".");
106     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
107     GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n);
108     k = n;
109   }
110   FPRINTF (stderr, "%s",  "\n");
111   FPRINTF (stdout,
112            "Found %u/%u items in %s (%u were deleted during storage processing)\n",
113            found, ITERATIONS,
114            GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES),
115            ITERATIONS - found);
116   if (found > 0)
117     GAUGER (gstr, "Time to GET item from datacache",
118             GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL / 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 cfg_name[128];
134   char *const xargv[] = {
135     "perf-datacache",
136     "-c",
137     cfg_name,
138     NULL
139   };
140   struct GNUNET_GETOPT_CommandLineOption options[] = {
141     GNUNET_GETOPT_OPTION_END
142   };
143
144   GNUNET_log_setup ("perf-datacache",
145                     "WARNING",
146                     NULL);
147   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
148   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "perf_datacache_data_%s.conf",
149                    plugin_name);
150   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
151                       "perf-datacache", "nohelp", options, &run, NULL);
152   if ( (0 != ok) && (77 != ok) )
153     FPRINTF (stderr, "Missed some perfcases: %d\n", ok);
154   return ok;
155 }
156
157 /* end of perf_datacache.c */