mantis 1627
[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
29 #define VERBOSE GNUNET_NO
30
31 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
32
33 #define ITERATIONS 10000
34
35 static int ok;
36
37 static unsigned int found;
38
39 /**
40  * Name of plugin under test.
41  */
42 static const char *plugin_name;
43
44
45 static int
46 checkIt (void *cls,
47          struct GNUNET_TIME_Absolute exp,
48          const GNUNET_HashCode * key,
49          size_t size, 
50          const char *data, 
51          enum GNUNET_BLOCK_Type type)
52 {
53   if ( (size == sizeof (GNUNET_HashCode)) &&
54        (0 == memcmp (data, cls, size)) )
55     found++;
56   return GNUNET_OK;
57 }
58
59
60 static void
61 run (void *cls,
62      char *const *args,
63      const char *cfgfile,
64      const struct GNUNET_CONFIGURATION_Handle *cfg)
65 {
66   struct GNUNET_DATACACHE_Handle *h;
67   GNUNET_HashCode k;
68   GNUNET_HashCode n;
69   struct GNUNET_TIME_Absolute exp;
70   struct GNUNET_TIME_Absolute start;
71   unsigned int i;
72   
73   ok = 0;
74   h = GNUNET_DATACACHE_create (cfg,
75                                "perfcache");
76
77   if (h == NULL)
78     {
79       fprintf (stderr,
80                "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
81       return;
82     }
83   exp = GNUNET_TIME_absolute_get ();
84   start = exp;
85   exp.abs_value += 5 * 60 * 1000;
86   memset (&k, 0, sizeof (GNUNET_HashCode));
87   for (i = 0; i < ITERATIONS; i++)
88     {
89       if (0 == i % (ITERATIONS / 80))
90         fprintf (stderr, ".");
91       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
92       ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
93                                                  &k,
94                                                  sizeof (GNUNET_HashCode),
95                                                  (const char *) &n,
96                                                  1+i%16,
97                                                  exp));
98       k = n;
99     }
100   fprintf (stderr, "\n");
101   fprintf (stdout, "Stored %u items in %llums\n",
102            ITERATIONS,
103            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).rel_value);
104   start = GNUNET_TIME_absolute_get ();
105   memset (&k, 0, sizeof (GNUNET_HashCode));
106   for (i = 0; i < ITERATIONS; i++)
107     {
108       if (0 == i % (ITERATIONS / 80))
109         fprintf (stderr, ".");
110       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
111       GNUNET_DATACACHE_get (h, &k, 1+i%16,
112                             &checkIt, &n);
113       k = n;
114     }
115   fprintf (stderr, "\n");
116   fprintf (stdout, "Found %u/%u items in %llums (%u were deleted during storage processing)\n",
117            found, ITERATIONS,
118            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).rel_value,
119            ITERATIONS - found);
120            
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 *pos;
135   char cfg_name[128];
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,
166                    sizeof (cfg_name),
167                    "perf_datacache_data_%s.conf",
168                    plugin_name);
169   if (pos != plugin_name)
170     pos[0] = '.';
171   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
172                       xargv, "perf-datacache", "nohelp",
173                       options, &run, NULL);
174   if (ok != 0)
175     fprintf (stderr, "Missed some perfcases: %d\n", ok);
176   return ok;
177 }
178
179 /* end of perf_datacache.c */