more client code
[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 <gauger.h>
29
30 #define VERBOSE GNUNET_NO
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          struct GNUNET_TIME_Absolute exp,
49          const GNUNET_HashCode * key,
50          size_t size, 
51          const char *data, 
52          enum GNUNET_BLOCK_Type type)
53 {
54   if ( (size == sizeof (GNUNET_HashCode)) &&
55        (0 == memcmp (data, cls, size)) )
56     found++;
57   return GNUNET_OK;
58 }
59
60
61 static void
62 run (void *cls,
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   char gstr[128];
74   
75   ok = 0;
76   h = GNUNET_DATACACHE_create (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.abs_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).rel_value);
106   GNUNET_snprintf (gstr, sizeof (gstr),
107                    "DATACACHE-%s",
108                    plugin_name);
109   GAUGER (gstr,
110           "Time to PUT item in datacache", 
111           GNUNET_TIME_absolute_get_duration(start).rel_value / ITERATIONS, 
112           "ms/item");
113   start = GNUNET_TIME_absolute_get ();
114   memset (&k, 0, sizeof (GNUNET_HashCode));
115   for (i = 0; i < ITERATIONS; i++)
116     {
117       if (0 == i % (ITERATIONS / 80))
118         fprintf (stderr, ".");
119       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
120       GNUNET_DATACACHE_get (h, &k, 1+i%16,
121                             &checkIt, &n);
122       k = n;
123     }
124   fprintf (stderr, "\n");
125   fprintf (stdout, 
126            "Found %u/%u items in %llums (%u were deleted during storage processing)\n",
127            found, ITERATIONS,
128            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).rel_value,
129            ITERATIONS - found);
130   if (found > 0)
131     GAUGER (gstr,
132             "Time to GET item from datacache",
133             GNUNET_TIME_absolute_get_duration(start).rel_value / found, "ms/item");        
134   GNUNET_DATACACHE_destroy (h);
135   ASSERT (ok == 0);
136   return;
137 FAILURE:
138   if (h != NULL)
139     GNUNET_DATACACHE_destroy (h);
140   ok = GNUNET_SYSERR;
141 }
142
143
144 int
145 main (int argc, char *argv[])
146 {
147   char *pos;
148   char cfg_name[128];
149   char *const xargv[] = { 
150     "perf-datacache",
151     "-c",
152     cfg_name,
153 #if VERBOSE
154     "-L", "DEBUG",
155 #endif
156     NULL
157   };
158   struct GNUNET_GETOPT_CommandLineOption options[] = {
159     GNUNET_GETOPT_OPTION_END
160   };
161   
162   GNUNET_log_setup ("perf-datacache",
163 #if VERBOSE
164                     "DEBUG",
165 #else
166                     "WARNING",
167 #endif
168                     NULL);
169   /* determine name of plugin to use */
170   plugin_name = argv[0];
171   while (NULL != (pos = strstr(plugin_name, "_")))
172     plugin_name = pos+1;
173   if (NULL != (pos = strstr(plugin_name, ".")))
174     pos[0] = 0;
175   else
176     pos = (char *) plugin_name;
177   
178   GNUNET_snprintf (cfg_name,
179                    sizeof (cfg_name),
180                    "perf_datacache_data_%s.conf",
181                    plugin_name);
182   if (pos != plugin_name)
183     pos[0] = '.';
184   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
185                       xargv, "perf-datacache", "nohelp",
186                       options, &run, NULL);
187   if (ok != 0)
188     fprintf (stderr, "Missed some perfcases: %d\n", ok);
189   return ok;
190 }
191
192 /* end of perf_datacache.c */