implementing all-now iterator
[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 2, 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 int ok;
55
56 enum RunPhase
57   {
58     RP_DONE = 0,
59     RP_PUT,
60     RP_LP_GET,
61     RP_AE_GET,
62     RP_ZA_GET,
63     RP_MO_GET,
64     RP_AN_GET
65   };
66
67
68 struct CpsRunContext
69 {
70   unsigned int i;
71   struct GNUNET_TIME_Absolute start;
72   struct GNUNET_TIME_Absolute end;
73   struct GNUNET_SCHEDULER_Handle *sched;
74   struct GNUNET_CONFIGURATION_Handle *cfg;
75   struct GNUNET_DATASTORE_PluginFunctions * api;
76   const char *msg;
77   enum RunPhase phase;
78   unsigned int cnt;
79 };
80
81
82              
83 static void
84 putValue (struct GNUNET_DATASTORE_PluginFunctions * api, int i, int k)
85 {
86   char value[65536];
87   size_t size;
88   static GNUNET_HashCode key;
89   static int ic;
90   char *msg;
91
92   /* most content is 32k */
93   size = 32 * 1024;
94
95   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
96     size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
97   size = size - (size & 7);     /* always multiple of 8 */
98
99   /* generate random key */
100   key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().value;
101   GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
102   memset (value, i, size);
103   if (i > 255)
104     memset (value, i - 255, size / 2);
105   value[0] = k;
106   msg = NULL;
107   if (GNUNET_OK != api->put (api->cls,
108                              &key, 
109                              size,
110                              value,
111                              i,
112                              GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
113                              i,
114                              GNUNET_TIME_relative_to_absolute 
115                              (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
116                                                              60 * 60 * 60 * 1000 +
117                                                              GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
118                              &msg))
119     {
120       fprintf (stderr, "ERROR: `%s'\n", msg);
121       GNUNET_free_non_null (msg);
122       return;
123     }
124   ic++;
125   stored_bytes += size;
126   stored_ops++;
127   stored_entries++;
128 }
129
130 static void
131 test (void *cls,
132       const struct GNUNET_SCHEDULER_TaskContext *tc);
133
134
135 static int
136 iterateDummy (void *cls,
137               void *next_cls,
138               const GNUNET_HashCode * key,
139               uint32_t size,
140               const void *data,
141               uint32_t type,
142               uint32_t priority,
143               uint32_t anonymity,
144               struct GNUNET_TIME_Absolute
145               expiration, 
146               uint64_t uid)
147 {
148   struct CpsRunContext *crc = cls;
149   
150   if (key == NULL)
151     {
152       crc->end = GNUNET_TIME_absolute_get();
153       printf (crc->msg,
154               crc->i,
155               (unsigned long long) (crc->end.value - crc->start.value),
156               crc->cnt);
157       if (crc->phase != RP_AN_GET)
158         {
159           crc->phase++;
160         }
161       else
162         {
163           if (crc->i == ITERATIONS)
164             crc->phase = RP_DONE;
165           else
166             crc->phase = RP_PUT;
167         }
168       GNUNET_SCHEDULER_add_after (crc->sched,
169                                   GNUNET_NO,
170                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
171                                   GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
172                                   &test, crc);
173       return GNUNET_OK;
174     }
175   crc->cnt++;
176   crc->api->next_request (next_cls,
177                           GNUNET_NO);
178   return GNUNET_OK;
179 }
180
181
182
183 /**
184  * Function called when the service shuts
185  * down.  Unloads our datastore plugin.
186  *
187  * @param api api to unload
188  */
189 static void
190 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
191                struct GNUNET_CONFIGURATION_Handle *cfg)
192 {
193   char *name;
194   char *libname;
195
196   if (GNUNET_OK !=
197       GNUNET_CONFIGURATION_get_value_string (cfg,
198                                              "DATASTORE", "DATABASE", &name))
199     {
200       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201                   _("No `%s' specified for `%s' in configuration!\n"),
202                   "DATABASE",
203                   "DATASTORE");
204       return;
205     }
206   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
207   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
208   GNUNET_free (libname);
209   GNUNET_free (name);
210 }
211
212
213
214 /**
215  * Last task run during shutdown.  Disconnects us from
216  * the transport and core.
217  */
218 static void
219 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
220 {
221   struct CpsRunContext *crc = cls;
222
223   unload_plugin (crc->api, crc->cfg);
224   GNUNET_free (crc);
225 }
226
227
228 static void
229 test (void *cls,
230       const struct GNUNET_SCHEDULER_TaskContext *tc)
231 {  
232   struct CpsRunContext *crc = cls;
233   int j;
234
235   switch (crc->phase)
236     {
237     case RP_PUT:      
238       crc->start = GNUNET_TIME_absolute_get ();
239       for (j=0;j<PUT_10;j++)
240         putValue (crc->api, j, crc->i);
241       crc->end = GNUNET_TIME_absolute_get ();
242       printf ("%3u insertion took                      %20llums for %u\n",
243               crc->i,
244               (unsigned long long) (crc->end.value - crc->start.value),
245               (unsigned int) PUT_10);
246       crc->i++;
247       crc->phase = RP_LP_GET;
248       GNUNET_SCHEDULER_add_after (crc->sched,
249                                   GNUNET_NO,
250                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
251                                   GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
252                                   &test, crc);
253       break;
254     case RP_LP_GET:
255       crc->cnt = 0;
256       crc->start = GNUNET_TIME_absolute_get ();      
257       crc->msg = "%3u low priority iteration took         %20llums for %u\n";
258       crc->api->iter_low_priority (crc->api->cls, 0, 
259                                    &iterateDummy,
260                                    crc);
261       break;
262     case RP_AE_GET:
263       crc->cnt = 0;
264       crc->start = GNUNET_TIME_absolute_get ();      
265       crc->msg = "%3u ascending expiration iteration took %20llums for %u\n";
266       crc->api->iter_ascending_expiration (crc->api->cls, 0, 
267                                       &iterateDummy,
268                                       crc);
269       break;
270     case RP_ZA_GET:
271       crc->cnt = 0;
272       crc->start = GNUNET_TIME_absolute_get ();      
273       crc->msg = "%3u zero anonymity iteration took       %20llums for %u\n";
274       crc->api->iter_zero_anonymity (crc->api->cls, 0, 
275                                      &iterateDummy,
276                                      crc);
277       break;
278     case RP_MO_GET:
279       crc->cnt = 0;
280       crc->start = GNUNET_TIME_absolute_get ();      
281       crc->msg = "%3u migration order iteration took      %20llums for %u\n";
282       crc->api->iter_migration_order (crc->api->cls, 0, 
283                                       &iterateDummy,
284                                       crc);
285       break;
286     case RP_AN_GET:
287       crc->cnt = 0;
288       crc->start = GNUNET_TIME_absolute_get ();      
289       crc->msg = "%3u all now iteration took              %20llums for %u\n";
290       crc->api->iter_all_now (crc->api->cls, 0,
291                               &iterateDummy,
292                               crc);
293       break;
294     case RP_DONE:
295       crc->api->drop (crc->api->cls);
296       GNUNET_SCHEDULER_add_delayed (crc->sched,
297                                     GNUNET_YES,
298                                     GNUNET_SCHEDULER_PRIORITY_IDLE,
299                                     GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
300                                     GNUNET_TIME_UNIT_ZERO,
301                                     &cleaning_task, crc);
302       break;
303     }
304 }
305
306
307 /**
308  * Load the datastore plugin.
309  */
310 static struct GNUNET_DATASTORE_PluginFunctions *
311 load_plugin (struct GNUNET_CONFIGURATION_Handle *cfg,
312              struct GNUNET_SCHEDULER_Handle *sched)
313 {
314   static struct GNUNET_DATASTORE_PluginEnvironment env;
315   struct GNUNET_DATASTORE_PluginFunctions * ret; 
316   char *name;
317   char *libname;
318
319   if (GNUNET_OK !=
320       GNUNET_CONFIGURATION_get_value_string (cfg,
321                                              "DATASTORE", "DATABASE", &name))
322     {
323       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
324                   _("No `%s' specified for `%s' in configuration!\n"),
325                   "DATABASE",
326                   "DATASTORE");
327       return NULL;
328     }
329   env.cfg = cfg;
330   env.sched = sched;  
331   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
332               _("Loading `%s' datastore plugin\n"), name);
333   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
334   GNUNET_assert (NULL != (ret = GNUNET_PLUGIN_load (libname, &env)));
335   GNUNET_free (libname);
336   GNUNET_free (name);
337   return ret;
338 }
339
340
341 static void
342 run (void *cls,
343      struct GNUNET_SCHEDULER_Handle *s,
344      char *const *args,
345      const char *cfgfile,
346      struct GNUNET_CONFIGURATION_Handle *c)
347 {
348   struct GNUNET_DATASTORE_PluginFunctions *api;
349   struct CpsRunContext *crc;
350
351   api = load_plugin (c, s);
352   GNUNET_assert (api != NULL);
353   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
354   crc->api = api;
355   crc->sched = s;
356   crc->cfg = c;
357   crc->phase = RP_PUT;
358   GNUNET_SCHEDULER_add_after (s,
359                               GNUNET_YES,
360                               GNUNET_SCHEDULER_PRIORITY_KEEP,
361                               GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
362                               &test, crc);
363 }
364
365
366 static int
367 check ()
368 {
369   char *const argv[] = { 
370     "perf-plugin-datastore",
371     "-c",
372     "perf_plugin_datastore_data.conf",
373 #if VERBOSE
374     "-L", "DEBUG",
375 #endif
376     NULL
377   };
378   struct GNUNET_GETOPT_CommandLineOption options[] = {
379     GNUNET_GETOPT_OPTION_END
380   };
381   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
382                       argv, "perf-plugin-datastore", "nohelp",
383                       options, &run, NULL);
384   if (ok != 0)
385     fprintf (stderr, "Missed some testcases: %u\n", ok);
386   return ok;
387 }
388
389
390 int
391 main (int argc, char *argv[])
392 {
393   int ret;
394
395   GNUNET_log_setup ("perf-plugin-datastore",
396 #if VERBOSE
397                     "DEBUG",
398 #else
399                     "WARNING",
400 #endif
401                     NULL);
402   ret = check ();
403
404   return ret;
405 }
406
407
408 /* end of perf_plugin_datastore.c */
409
410