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