first batch of license fixes (boring)
[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 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 /*
16  * @file datacache/perf_datacache.c
17  * @brief Performance evaluation for the datacache implementations.
18  * @author Nils Durner
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_datacache_lib.h"
23 #include "gnunet_testing_lib.h"
24 #include <gauger.h>
25
26
27 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
28
29 #define ITERATIONS 10000
30
31 static int ok;
32
33 static unsigned int found;
34
35 /**
36  * Name of plugin under test.
37  */
38 static const char *plugin_name;
39
40
41 static int
42 checkIt (void *cls,
43          const struct GNUNET_HashCode * key, size_t size, const char *data,
44          enum GNUNET_BLOCK_Type type,
45          struct GNUNET_TIME_Absolute exp,
46          unsigned int path_len,
47          const struct GNUNET_PeerIdentity *path)
48 {
49   if ((size == sizeof (struct GNUNET_HashCode)) && (0 == memcmp (data, cls, size)))
50     found++;
51   return GNUNET_OK;
52 }
53
54
55 static void
56 run (void *cls, char *const *args, const char *cfgfile,
57      const struct GNUNET_CONFIGURATION_Handle *cfg)
58 {
59   struct GNUNET_DATACACHE_Handle *h;
60   struct GNUNET_HashCode k;
61   struct GNUNET_HashCode n;
62   struct GNUNET_TIME_Absolute exp;
63   struct GNUNET_TIME_Absolute start;
64   unsigned int i;
65   char gstr[128];
66
67   ok = 0;
68   h = GNUNET_DATACACHE_create (cfg, "perfcache");
69
70   if (h == NULL)
71   {
72     FPRINTF (stderr, "%s", "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
73     ok = 77; /* mark test as skipped */
74     return;
75   }
76   exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
77   start = GNUNET_TIME_absolute_get ();
78   memset (&k, 0, sizeof (struct GNUNET_HashCode));
79   for (i = 0; i < ITERATIONS; i++)
80   {
81     if (0 == i % (ITERATIONS / 80))
82       FPRINTF (stderr, "%s",  ".");
83     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
84     ASSERT (GNUNET_OK ==
85             GNUNET_DATACACHE_put (h, &k, sizeof (struct GNUNET_HashCode),
86                                   (const char *) &n, 1 + i % 16, exp,
87                                   0, NULL));
88     k = n;
89   }
90   FPRINTF (stderr, "%s",  "\n");
91   FPRINTF (stdout, "Stored %u items in %s\n", ITERATIONS,
92            GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
93   GNUNET_snprintf (gstr, sizeof (gstr), "DATACACHE-%s", plugin_name);
94   GAUGER (gstr, "Time to PUT item in datacache",
95           GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL / ITERATIONS,
96           "ms/item");
97   start = GNUNET_TIME_absolute_get ();
98   memset (&k, 0, sizeof (struct GNUNET_HashCode));
99   for (i = 0; i < ITERATIONS; i++)
100   {
101     if (0 == i % (ITERATIONS / 80))
102       FPRINTF (stderr, "%s",  ".");
103     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
104     GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n);
105     k = n;
106   }
107   FPRINTF (stderr, "%s",  "\n");
108   FPRINTF (stdout,
109            "Found %u/%u items in %s (%u were deleted during storage processing)\n",
110            found, ITERATIONS,
111            GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES),
112            ITERATIONS - found);
113   if (found > 0)
114     GAUGER (gstr, "Time to GET item from datacache",
115             GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL / found,
116             "ms/item");
117   GNUNET_DATACACHE_destroy (h);
118   ASSERT (ok == 0);
119   return;
120 FAILURE:
121   if (h != NULL)
122     GNUNET_DATACACHE_destroy (h);
123   ok = GNUNET_SYSERR;
124 }
125
126
127 int
128 main (int argc, char *argv[])
129 {
130   char cfg_name[128];
131   char *const xargv[] = {
132     "perf-datacache",
133     "-c",
134     cfg_name,
135     NULL
136   };
137   struct GNUNET_GETOPT_CommandLineOption options[] = {
138     GNUNET_GETOPT_OPTION_END
139   };
140
141   GNUNET_log_setup ("perf-datacache",
142                     "WARNING",
143                     NULL);
144   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
145   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "perf_datacache_data_%s.conf",
146                    plugin_name);
147   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
148                       "perf-datacache", "nohelp", options, &run, NULL);
149   if ( (0 != ok) && (77 != ok) )
150     FPRINTF (stderr, "Missed some perfcases: %d\n", ok);
151   return ok;
152 }
153
154 /* end of perf_datacache.c */