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