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