fix
[oweals/gnunet.git] / src / datastore / perf_plugin_datastore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007, 2009 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 perf_plugin_datastore.c
22  * @brief Profile database plugin directly, focusing on iterators.
23  * @author Christian Grothoff
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "plugin_datastore.h"
30
31 #define VERBOSE GNUNET_NO
32
33 /**
34  * Target datastore size (in bytes).  Realistic sizes are
35  * more like 16 GB (not the default of 16 MB); however,
36  * those take too long to run them in the usual "make check"
37  * sequence.  Hence the value used for shipping is tiny.
38  */
39 #define MAX_SIZE 1024LL * 1024 * 128
40
41 #define ITERATIONS 10
42
43 /**
44  * Number of put operations equivalent to 1/10th of MAX_SIZE
45  */
46 #define PUT_10 (MAX_SIZE / 32 / 1024 / ITERATIONS)
47
48 static unsigned long long stored_bytes;
49
50 static unsigned long long stored_entries;
51
52 static unsigned long long stored_ops;
53
54 static const char *plugin_name;
55
56 static int ok;
57
58 enum RunPhase
59   {
60     RP_DONE = 0,
61     RP_PUT,
62     RP_LP_GET,
63     RP_AE_GET,
64     RP_ZA_GET,
65     RP_MO_GET,
66     RP_AN_GET
67   };
68
69
70 struct CpsRunContext
71 {
72   unsigned int i;
73   struct GNUNET_TIME_Absolute start;
74   struct GNUNET_TIME_Absolute end;
75   struct GNUNET_SCHEDULER_Handle *sched;
76   const struct GNUNET_CONFIGURATION_Handle *cfg;
77   struct GNUNET_DATASTORE_PluginFunctions * api;
78   const char *msg;
79   enum RunPhase phase;
80   unsigned int cnt;
81 };
82
83
84              
85 static void
86 putValue (struct GNUNET_DATASTORE_PluginFunctions * api, int i, int k)
87 {
88   char value[65536];
89   size_t size;
90   static GNUNET_HashCode key;
91   static int ic;
92   char *msg;
93   unsigned int prio;
94
95   /* most content is 32k */
96   size = 32 * 1024;
97
98   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
99     size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
100   size = size - (size & 7);     /* always multiple of 8 */
101
102   /* generate random key */
103   key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().value;
104   GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
105   memset (value, i, size);
106   if (i > 255)
107     memset (value, i - 255, size / 2);
108   value[0] = k;
109   msg = NULL;
110   prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
111   if (GNUNET_OK != api->put (api->cls,
112                              &key, 
113                              size,
114                              value,
115                              i,
116                              prio,
117                              i,
118                              GNUNET_TIME_relative_to_absolute 
119                              (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
120                                                              60 * 60 * 60 * 1000 +
121                                                              GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
122                              &msg))
123     {
124       fprintf (stderr, "ERROR: `%s'\n", msg);
125       GNUNET_free_non_null (msg);
126       return;
127     }
128   ic++;
129   stored_bytes += size;
130   stored_ops++;
131   stored_entries++;
132 }
133
134 static void
135 test (void *cls,
136       const struct GNUNET_SCHEDULER_TaskContext *tc);
137
138
139 static int
140 iterateDummy (void *cls,
141               void *next_cls,
142               const GNUNET_HashCode * key,
143               uint32_t size,
144               const void *data,
145               enum GNUNET_BLOCK_Type type,
146               uint32_t priority,
147               uint32_t anonymity,
148               struct GNUNET_TIME_Absolute
149               expiration, 
150               uint64_t uid)
151 {
152   struct CpsRunContext *crc = cls;
153   
154   if (key == NULL)
155     {
156       crc->end = GNUNET_TIME_absolute_get();
157       printf (crc->msg,
158               crc->i,
159               (unsigned long long) (crc->end.value - crc->start.value),
160               crc->cnt);
161       if (crc->phase != RP_AN_GET)
162         {
163           crc->phase++;
164         }
165       else
166         {
167           if (crc->i == ITERATIONS)
168             crc->phase = RP_DONE;
169           else
170             crc->phase = RP_PUT;
171         }
172       GNUNET_SCHEDULER_add_after (crc->sched,
173                                   GNUNET_SCHEDULER_NO_TASK,
174                                   &test, crc);
175       return GNUNET_OK;
176     }
177 #if VERBOSE
178   fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
179            type, priority, size,
180            (unsigned long long) expiration.value);
181 #endif
182   crc->cnt++;
183   crc->api->next_request (next_cls,
184                           GNUNET_NO);
185   return GNUNET_OK;
186 }
187
188
189
190 /**
191  * Function called when the service shuts
192  * down.  Unloads our datastore plugin.
193  *
194  * @param api api to unload
195  * @param cfg configuration to use
196  */
197 static void
198 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
199                const struct GNUNET_CONFIGURATION_Handle *cfg)
200 {
201   char *name;
202   char *libname;
203
204   if (GNUNET_OK !=
205       GNUNET_CONFIGURATION_get_value_string (cfg,
206                                              "DATASTORE", "DATABASE", &name))
207     {
208       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209                   _("No `%s' specified for `%s' in configuration!\n"),
210                   "DATABASE",
211                   "DATASTORE");
212       return;
213     }
214   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
215   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
216   GNUNET_free (libname);
217   GNUNET_free (name);
218 }
219
220
221
222 /**
223  * Last task run during shutdown.  Disconnects us from
224  * the transport and core.
225  */
226 static void
227 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
228 {
229   struct CpsRunContext *crc = cls;
230
231   unload_plugin (crc->api, crc->cfg);
232   GNUNET_free (crc);
233 }
234
235
236 static void
237 test (void *cls,
238       const struct GNUNET_SCHEDULER_TaskContext *tc)
239 {  
240   struct CpsRunContext *crc = cls;
241   int j;
242
243   switch (crc->phase)
244     {
245     case RP_PUT:      
246       crc->start = GNUNET_TIME_absolute_get ();
247       for (j=0;j<PUT_10;j++)
248         putValue (crc->api, j, crc->i);
249       crc->end = GNUNET_TIME_absolute_get ();
250       printf ("%3u insertion took                      %20llums for %u\n",
251               crc->i,
252               (unsigned long long) (crc->end.value - crc->start.value),
253               (unsigned int) PUT_10);
254       crc->i++;
255       crc->phase = RP_LP_GET;
256       GNUNET_SCHEDULER_add_after (crc->sched,
257                                   GNUNET_SCHEDULER_NO_TASK,
258                                   &test, crc);
259       break;
260     case RP_LP_GET:
261       crc->cnt = 0;
262       crc->start = GNUNET_TIME_absolute_get ();      
263       crc->msg = "%3u low priority iteration took         %20llums for %u\n";
264       crc->api->iter_low_priority (crc->api->cls, 0, 
265                                    &iterateDummy,
266                                    crc);
267       break;
268     case RP_AE_GET:
269       crc->cnt = 0;
270       crc->start = GNUNET_TIME_absolute_get ();      
271       crc->msg = "%3u ascending expiration iteration took %20llums for %u\n";
272       crc->api->iter_ascending_expiration (crc->api->cls, 0, 
273                                       &iterateDummy,
274                                       crc);
275       break;
276     case RP_ZA_GET:
277       crc->cnt = 0;
278       crc->start = GNUNET_TIME_absolute_get ();      
279       crc->msg = "%3u zero anonymity iteration took       %20llums for %u\n";
280       crc->api->iter_zero_anonymity (crc->api->cls, 0, 
281                                      &iterateDummy,
282                                      crc);
283       break;
284     case RP_MO_GET:
285       crc->cnt = 0;
286       crc->start = GNUNET_TIME_absolute_get ();      
287       crc->msg = "%3u migration order iteration took      %20llums for %u\n";
288       crc->api->iter_migration_order (crc->api->cls, 0, 
289                                       &iterateDummy,
290                                       crc);
291       break;
292     case RP_AN_GET:
293       crc->cnt = 0;
294       crc->start = GNUNET_TIME_absolute_get ();      
295       crc->msg = "%3u all now iteration took              %20llums for %u\n";
296       crc->api->iter_all_now (crc->api->cls, 0,
297                               &iterateDummy,
298                               crc);
299       break;
300     case RP_DONE:
301       crc->api->drop (crc->api->cls);
302       GNUNET_SCHEDULER_add_with_priority (crc->sched,
303                                     GNUNET_SCHEDULER_PRIORITY_IDLE,
304                                     &cleaning_task, crc);
305       break;
306     }
307 }
308
309
310 /**
311  * Load the datastore plugin.
312  */
313 static struct GNUNET_DATASTORE_PluginFunctions *
314 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg,
315              struct GNUNET_SCHEDULER_Handle *sched)
316 {
317   static struct GNUNET_DATASTORE_PluginEnvironment env;
318   struct GNUNET_DATASTORE_PluginFunctions * ret; 
319   char *name;
320   char *libname;
321
322   if (GNUNET_OK !=
323       GNUNET_CONFIGURATION_get_value_string (cfg,
324                                              "DATASTORE", "DATABASE", &name))
325     {
326       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
327                   _("No `%s' specified for `%s' in configuration!\n"),
328                   "DATABASE",
329                   "DATASTORE");
330       return NULL;
331     }
332   env.cfg = cfg;
333   env.sched = sched;  
334   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
335               _("Loading `%s' datastore plugin\n"), name);
336   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
337   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
338     {
339       fprintf (stderr,
340                "Failed to load plugin `%s'!\n",
341                name);
342       return NULL;
343     }
344   GNUNET_free (libname);
345   GNUNET_free (name);
346   return ret;
347 }
348
349
350 static void
351 run (void *cls,
352      struct GNUNET_SCHEDULER_Handle *s,
353      char *const *args,
354      const char *cfgfile,
355      const struct GNUNET_CONFIGURATION_Handle *c)
356 {
357   struct GNUNET_DATASTORE_PluginFunctions *api;
358   struct CpsRunContext *crc;
359
360   api = load_plugin (c, s);
361   if (api == NULL)
362     {
363       fprintf (stderr, 
364                "Could not initialize plugin, assuming database not configured. Test not run!\n");
365       return;
366     }
367   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
368   crc->api = api;
369   crc->sched = s;
370   crc->cfg = c;
371   crc->phase = RP_PUT;
372   GNUNET_SCHEDULER_add_now (crc->sched,
373                             &test, crc);
374 }
375
376
377 static int
378 check ()
379 {
380   char cfg_name[128];
381   char *const argv[] = { 
382     "perf-plugin-datastore",
383     "-c",
384     cfg_name,
385 #if VERBOSE
386     "-L", "DEBUG",
387 #endif
388     NULL
389   };
390   struct GNUNET_GETOPT_CommandLineOption options[] = {
391     GNUNET_GETOPT_OPTION_END
392   };
393
394   GNUNET_snprintf (cfg_name,
395                    sizeof (cfg_name),
396                    "perf_plugin_datastore_data_%s.conf",
397                    plugin_name);
398   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
399                       argv, "perf-plugin-datastore", "nohelp",
400                       options, &run, NULL);
401   if (ok != 0)
402     fprintf (stderr, "Missed some testcases: %u\n", ok);
403   return ok;
404 }
405
406
407 int
408 main (int argc, char *argv[])
409 {
410   int ret;
411   const char *pos;
412   char dir_name[128];
413
414   /* determine name of plugin to use */
415   plugin_name = argv[0];
416   while (NULL != (pos = strstr(plugin_name, "_")))
417     plugin_name = pos+1;
418
419   GNUNET_snprintf (dir_name,
420                    sizeof (dir_name),
421                    "/tmp/perf-gnunet-datastore-%s",
422                    plugin_name);
423   GNUNET_DISK_directory_remove (dir_name);
424   GNUNET_log_setup ("perf-plugin-datastore",
425 #if VERBOSE
426                     "DEBUG",
427 #else
428                     "WARNING",
429 #endif
430                     NULL);
431   ret = check ();
432   GNUNET_DISK_directory_remove (dir_name);
433
434   return ret;
435 }
436
437 /* end of perf_plugin_datastore.c */
438
439