use NULL value in load_path_suffix to NOT load any files
[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      SPDX-License-Identifier: AGPL3.0-or-later
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__, \
33                                             __LINE__); goto FAILURE; \
34                        } } while (0)
35
36 #define ITERATIONS 10000
37
38 static int ok;
39
40 static unsigned int found;
41
42 /**
43  * Name of plugin under test.
44  */
45 static const char *plugin_name;
46
47
48 static int
49 checkIt (void *cls,
50          const struct GNUNET_HashCode *key, size_t size, const char *data,
51          enum GNUNET_BLOCK_Type type,
52          struct GNUNET_TIME_Absolute exp,
53          unsigned int path_len,
54          const struct GNUNET_PeerIdentity *path)
55 {
56   if ((size == sizeof(struct GNUNET_HashCode)) && (0 == memcmp (data, cls,
57                                                                 size)))
58     found++;
59   return GNUNET_OK;
60 }
61
62
63 static void
64 run (void *cls, char *const *args, const char *cfgfile,
65      const struct GNUNET_CONFIGURATION_Handle *cfg)
66 {
67   struct GNUNET_DATACACHE_Handle *h;
68   struct GNUNET_HashCode k;
69   struct 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, "perfcache");
77
78   if (h == NULL)
79   {
80     fprintf (stderr, "%s",
81              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
82     ok = 77;   /* mark test as skipped */
83     return;
84   }
85   exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
86   start = GNUNET_TIME_absolute_get ();
87   memset (&k, 0, sizeof(struct GNUNET_HashCode));
88   for (i = 0; i < ITERATIONS; i++)
89   {
90     if (0 == i % (ITERATIONS / 80))
91       fprintf (stderr, "%s", ".");
92     GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
93     ASSERT (GNUNET_OK ==
94             GNUNET_DATACACHE_put (h, &k, sizeof(struct GNUNET_HashCode),
95                                   (const char *) &n, 1 + i % 16, exp,
96                                   0, NULL));
97     k = n;
98   }
99   fprintf (stderr, "%s", "\n");
100   fprintf (stdout, "Stored %u items in %s\n", ITERATIONS,
101            GNUNET_STRINGS_relative_time_to_string (
102              GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
103   GNUNET_snprintf (gstr, sizeof(gstr), "DATACACHE-%s", plugin_name);
104   GAUGER (gstr, "Time to PUT item in datacache",
105           GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL
106           / ITERATIONS,
107           "ms/item");
108   start = GNUNET_TIME_absolute_get ();
109   memset (&k, 0, sizeof(struct GNUNET_HashCode));
110   for (i = 0; i < ITERATIONS; i++)
111   {
112     if (0 == i % (ITERATIONS / 80))
113       fprintf (stderr, "%s", ".");
114     GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
115     GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n);
116     k = n;
117   }
118   fprintf (stderr, "%s", "\n");
119   fprintf (stdout,
120            "Found %u/%u items in %s (%u were deleted during storage processing)\n",
121            found, ITERATIONS,
122            GNUNET_STRINGS_relative_time_to_string (
123              GNUNET_TIME_absolute_get_duration (start), GNUNET_YES),
124            ITERATIONS - found);
125   if (found > 0)
126     GAUGER (gstr, "Time to GET item from datacache",
127             GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL
128             / found,
129             "ms/item");
130   GNUNET_DATACACHE_destroy (h);
131   ASSERT (ok == 0);
132   return;
133 FAILURE:
134   if (h != NULL)
135     GNUNET_DATACACHE_destroy (h);
136   ok = GNUNET_SYSERR;
137 }
138
139
140 int
141 main (int argc, char *argv[])
142 {
143   char cfg_name[PATH_MAX];
144   char *const xargv[] = {
145     "perf-datacache",
146     "-c",
147     cfg_name,
148     NULL
149   };
150   struct GNUNET_GETOPT_CommandLineOption options[] = {
151     GNUNET_GETOPT_OPTION_END
152   };
153
154   GNUNET_log_setup ("perf-datacache",
155                     "WARNING",
156                     NULL);
157   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
158   GNUNET_snprintf (cfg_name, sizeof(cfg_name), "perf_datacache_data_%s.conf",
159                    plugin_name);
160   GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1, xargv,
161                       "perf-datacache", "nohelp", options, &run, NULL);
162   if ((0 != ok) && (77 != ok))
163     fprintf (stderr, "Missed some perfcases: %d\n", ok);
164   return ok;
165 }
166
167
168 /* end of perf_datacache.c */