use size_t
[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      struct GNUNET_SCHEDULER_Handle *sched,
63      char *const *args,
64      const char *cfgfile,
65      const struct GNUNET_CONFIGURATION_Handle *cfg)
66 {
67   struct GNUNET_DATACACHE_Handle *h;
68   GNUNET_HashCode k;
69   GNUNET_HashCode n;
70   struct GNUNET_TIME_Absolute exp;
71   struct GNUNET_TIME_Absolute start;
72   unsigned int i;
73   
74   ok = 0;
75   h = GNUNET_DATACACHE_create (sched,
76                                cfg,
77                                "perfcache");
78
79   if (h == NULL)
80     {
81       fprintf (stderr,
82                "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
83       return;
84     }
85   exp = GNUNET_TIME_absolute_get ();
86   start = exp;
87   exp.value += 5 * 60 * 1000;
88   memset (&k, 0, sizeof (GNUNET_HashCode));
89   for (i = 0; i < ITERATIONS; i++)
90     {
91       if (0 == i % (ITERATIONS / 80))
92         fprintf (stderr, ".");
93       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
94       ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
95                                                  &k,
96                                                  sizeof (GNUNET_HashCode),
97                                                  (const char *) &n,
98                                                  1+i%16,
99                                                  exp));
100       k = n;
101     }
102   fprintf (stderr, "\n");
103   fprintf (stdout, "Stored %u items in %llums\n",
104            ITERATIONS,
105            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).value);
106   start = GNUNET_TIME_absolute_get ();
107   memset (&k, 0, sizeof (GNUNET_HashCode));
108   for (i = 0; i < ITERATIONS; i++)
109     {
110       if (0 == i % (ITERATIONS / 80))
111         fprintf (stderr, ".");
112       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
113       GNUNET_DATACACHE_get (h, &k, 1+i%16,
114                             &checkIt, &n);
115       k = n;
116     }
117   fprintf (stderr, "\n");
118   fprintf (stdout, "Found %u/%u items in %llums (%u were deleted during storage processing)\n",
119            found, ITERATIONS,
120            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).value,
121            ITERATIONS - found);
122            
123   GNUNET_DATACACHE_destroy (h);
124   ASSERT (ok == 0);
125   return;
126 FAILURE:
127   if (h != NULL)
128     GNUNET_DATACACHE_destroy (h);
129   ok = GNUNET_SYSERR;
130 }
131
132
133 int
134 main (int argc, char *argv[])
135 {
136   const char *pos;
137   char cfg_name[128];
138   char *const xargv[] = { 
139     "perf-datacache",
140     "-c",
141     cfg_name,
142 #if VERBOSE
143     "-L", "DEBUG",
144 #endif
145     NULL
146   };
147   struct GNUNET_GETOPT_CommandLineOption options[] = {
148     GNUNET_GETOPT_OPTION_END
149   };
150   
151   GNUNET_log_setup ("perf-datacache",
152 #if VERBOSE
153                     "DEBUG",
154 #else
155                     "WARNING",
156 #endif
157                     NULL);
158   /* determine name of plugin to use */
159   plugin_name = argv[0];
160   while (NULL != (pos = strstr(plugin_name, "_")))
161     plugin_name = pos+1;
162   GNUNET_snprintf (cfg_name,
163                    sizeof (cfg_name),
164                    "perf_datacache_data_%s.conf",
165                    plugin_name);
166   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
167                       xargv, "perf-datacache", "nohelp",
168                       options, &run, NULL);
169   if (ok != 0)
170     fprintf (stderr, "Missed some perfcases: %d\n", ok);
171   return ok;
172 }
173
174 /* end of perf_datacache.c */