improving datastore API --- not working yet
[oweals/gnunet.git] / src / datastore / perf_datastore_api.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 datastore/perf_datastore_api.c
22  * @brief performance measurement for the datastore implementation
23  * @author Christian Grothoff
24  *
25  * This testcase inserts a bunch of (variable size) data and then
26  * deletes data until the (reported) database size drops below a given
27  * threshold.  This is iterated 10 times, with the actual size of the
28  * content stored and the number of operations performed being printed
29  * for each iteration.  The code also prints a "I" for every 40 blocks
30  * inserted and a "D" for every 40 blocks deleted.  The deletion
31  * strategy uses the "random" iterator.  Priorities and expiration
32  * dates are set using a pseudo-random value within a realistic range.
33  */
34
35 #include "platform.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_datastore_service.h"
39 #include <gauger.h>
40
41 #define VERBOSE GNUNET_NO
42
43 /**
44  * How long until we give up on transmitting the message?
45  */
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
47
48 static const char *plugin_name;
49
50 static struct GNUNET_DATASTORE_Handle *datastore;
51
52 /**
53  * Target datastore size (in bytes).
54  */
55 #define MAX_SIZE 1024LL * 1024 * 4
56
57 /**
58  * Report progress outside of major reports? Should probably be GNUNET_YES if
59  * size is > 16 MB.
60  */
61 #define REPORT_ID GNUNET_YES
62
63 /**
64  * Number of put operations equivalent to 1/3rd of MAX_SIZE
65  */
66 #define PUT_10 MAX_SIZE / 32 / 1024 / 3
67
68 /**
69  * Total number of iterations (each iteration doing
70  * PUT_10 put operations); we report full status every
71  * 10 iterations.  Abort with CTRL-C.
72  */
73 #define ITERATIONS 8
74
75
76 static unsigned long long stored_bytes;
77
78 static unsigned long long stored_entries;
79
80 static unsigned long long stored_ops;
81
82 static struct GNUNET_TIME_Absolute start_time;
83
84 static int ok;
85
86 enum RunPhase
87   {
88     RP_DONE = 0,
89     RP_PUT,
90     RP_CUT,
91     RP_REPORT
92   };
93
94
95 struct CpsRunContext
96 {
97   const struct GNUNET_CONFIGURATION_Handle *cfg;
98   enum RunPhase phase;
99   int j;
100   unsigned long long size;
101   int i;
102
103   GNUNET_HashCode key;
104   size_t esize;
105   char data[65536];
106 };
107
108
109
110 static void
111 run_continuation (void *cls,
112                   const struct GNUNET_SCHEDULER_TaskContext *tc);
113
114
115
116
117 static void
118 check_success (void *cls,
119                int success,
120                const char *msg)
121 {
122   struct CpsRunContext *crc = cls;
123   if (GNUNET_OK != success)
124     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
125                 "%s\n", msg);
126   GNUNET_assert (GNUNET_OK == success);
127 #if REPORT_ID
128   fprintf (stderr, "I");
129 #endif
130   stored_bytes += crc->size;
131   stored_ops++;
132   stored_entries++;
133   crc->j++;
134   if (crc->j == PUT_10)
135     {
136       crc->j = 0;
137       crc->i++;
138       if (crc->i == ITERATIONS)
139         crc->phase = RP_DONE;
140       else
141         crc->phase = RP_CUT;
142     }
143   GNUNET_SCHEDULER_add_continuation (&run_continuation,
144                                      crc,
145                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
146 }
147
148
149 /**
150  * Continuation called to notify client about result of the
151  * operation.
152  *
153  * @param cls closure
154  * @param success GNUNET_SYSERR on failure
155  * @param msg NULL on success, otherwise an error message
156  */
157 static void 
158 remove_next(void *cls,
159             int success,
160             const char *msg)
161 {
162   struct CpsRunContext *crc = cls;
163
164 #if REPORT_ID
165   fprintf (stderr, "D");
166 #endif
167   GNUNET_assert (GNUNET_OK == success);
168   GNUNET_SCHEDULER_add_continuation (&run_continuation,
169                                      crc,
170                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
171 }
172
173
174
175 static void
176 do_delete (void *cls,
177            const struct GNUNET_SCHEDULER_TaskContext *tc)
178 {
179   struct CpsRunContext *crc = cls;
180
181   stored_bytes -= crc->esize;
182   stored_entries--;
183   stored_ops++;
184   GNUNET_DATASTORE_remove (datastore,
185                            &crc->key,
186                            crc->esize,
187                            crc->data,
188                            1, 1, TIMEOUT,
189                            &remove_next,
190                            crc);
191 }
192
193
194
195 static void 
196 delete_value (void *cls,
197               const GNUNET_HashCode * key,
198               size_t size,
199               const void *data,
200               enum GNUNET_BLOCK_Type type,
201               uint32_t priority,
202               uint32_t anonymity,
203               struct GNUNET_TIME_Absolute
204               expiration, uint64_t uid)
205 {
206   struct CpsRunContext *crc = cls;
207
208   if (key == NULL)
209     {
210       if (stored_bytes < MAX_SIZE)
211         {
212           crc->phase = RP_REPORT;
213           GNUNET_SCHEDULER_add_continuation (&run_continuation,
214                                              crc,
215                                              GNUNET_SCHEDULER_REASON_PREREQ_DONE);
216           return;     
217         }
218       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
219                                           &do_delete,
220                                           crc);
221       return;
222     }
223   stored_ops++;
224   if (stored_bytes < MAX_SIZE)
225     {
226       GNUNET_DATASTORE_iterate_get_next (datastore);
227       return;     
228     }
229   crc->key = *key;
230   crc->esize = size;
231   memcpy (crc->data, data, size);
232   GNUNET_DATASTORE_iterate_get_next (datastore);
233 }
234
235
236 static void
237 run_continuation (void *cls,
238                   const struct GNUNET_SCHEDULER_TaskContext *tc)
239 {
240   struct CpsRunContext *crc = cls;
241   size_t size;
242   static GNUNET_HashCode key;
243   static char data[65536];
244   int i;
245   int k;
246   char gstr[128];
247
248   ok = (int) crc->phase;
249   switch (crc->phase)
250     {
251     case RP_PUT:
252       memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
253       i = crc->j;
254       k = crc->i;
255       /* most content is 32k */
256       size = 32 * 1024;
257       if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
258         size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
259       crc->size = size = size - (size & 7);     /* always multiple of 8 */
260       GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
261       memset (data, i, size);
262       if (i > 255)
263         memset (data, i - 255, size / 2);
264       data[0] = k;
265       GNUNET_DATASTORE_put (datastore,
266                             0,
267                             &key,
268                             size,
269                             data,
270                             i+1,
271                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
272                             i, 0,
273                             GNUNET_TIME_relative_to_absolute 
274                             (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
275                                                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
276                             1, 1, TIMEOUT,
277                             &check_success, 
278                             crc);
279       break;
280     case RP_CUT:
281       /* trim down below MAX_SIZE again */
282       GNUNET_DATASTORE_get_for_replication (datastore, 
283                                             1, 1, TIMEOUT,
284                                             &delete_value,
285                                             crc);
286       break;
287     case RP_REPORT:
288       printf (
289 #if REPORT_ID
290                "\n"
291 #endif
292                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
293                stored_bytes / 1024,     /* used size in k */
294                stored_ops / 1024,        /* total operations (in k) */
295                1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value));
296       crc->phase = RP_PUT;
297       crc->j = 0;
298       GNUNET_SCHEDULER_add_continuation (&run_continuation,
299                                          crc,
300                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
301       break;
302     case RP_DONE:
303       GNUNET_snprintf (gstr, sizeof (gstr),
304                        "PUT operations in %s-datastore",
305                        plugin_name);
306       if (crc->i == ITERATIONS)
307         GAUGER ("DATASTORE", gstr, 1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value), "op/s");
308       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
309       GNUNET_free (crc);
310       ok = 0;
311       break;
312     default:
313       GNUNET_assert (0);      
314     }
315 }
316
317
318
319 static void
320 run_tests (void *cls,
321            int success,
322            const char *msg)
323 {
324   struct CpsRunContext *crc = cls;
325
326   if (success != GNUNET_YES)
327     {
328       fprintf (stderr,
329                "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
330                msg);
331       GNUNET_free (crc);
332       return;
333     }
334   GNUNET_SCHEDULER_add_continuation (&run_continuation,
335                                      crc,
336                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
337 }
338
339
340 static void
341 run (void *cls,
342      char *const *args,
343      const char *cfgfile,
344      const struct GNUNET_CONFIGURATION_Handle *cfg)
345 {
346   struct CpsRunContext *crc;
347   static GNUNET_HashCode zkey;
348
349   datastore = GNUNET_DATASTORE_connect (cfg);
350   start_time = GNUNET_TIME_absolute_get ();
351   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
352   crc->cfg = cfg;
353   crc->phase = RP_PUT;
354   if (NULL ==
355       GNUNET_DATASTORE_put (datastore, 0,
356                             &zkey, 4, "TEST",
357                             GNUNET_BLOCK_TYPE_TEST,
358                             0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
359                             0, 1, GNUNET_TIME_UNIT_MINUTES,
360                             &run_tests, crc))
361     {
362       fprintf (stderr,
363                "Test 'put' operation failed.\n");
364       ok = 1;
365       GNUNET_free (crc);
366     }
367 }
368
369
370 static int
371 check ()
372 {
373   struct GNUNET_OS_Process *proc;
374   char cfg_name[128];
375   char *const argv[] = { 
376     "perf-datastore-api",
377     "-c",
378     cfg_name,
379 #if VERBOSE
380     "-L", "DEBUG",
381 #endif
382     NULL
383   };
384   struct GNUNET_GETOPT_CommandLineOption options[] = {
385     GNUNET_GETOPT_OPTION_END
386   };
387
388   GNUNET_snprintf (cfg_name,
389                    sizeof (cfg_name),
390                    "test_datastore_api_data_%s.conf",
391                    plugin_name);
392   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
393                                  "gnunet-service-arm",
394 #if VERBOSE
395                                  "-L", "DEBUG",
396 #endif
397                                  "-c", cfg_name, NULL);
398   GNUNET_assert (NULL != proc);
399   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
400                       argv, "perf-datastore-api", "nohelp",
401                       options, &run, NULL);
402   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
403     {
404       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
405       ok = 1;
406     }
407   GNUNET_OS_process_wait (proc);
408   GNUNET_OS_process_close (proc);
409   proc = NULL;
410   return ok;
411 }
412
413
414 int
415 main (int argc, char *argv[])
416 {
417   int ret;
418   char *pos;
419   char dir_name[128];
420
421   /* determine name of plugin to use */
422   plugin_name = argv[0];
423   while (NULL != (pos = strstr(plugin_name, "_")))
424     plugin_name = pos+1;
425   if (NULL != (pos = strstr(plugin_name, ".")))
426     pos[0] = 0;
427   else
428     pos = (char *) plugin_name;
429
430   GNUNET_snprintf (dir_name,
431                    sizeof (dir_name),
432                    "/tmp/test-gnunet-datastore-%s",
433                    plugin_name);
434   GNUNET_DISK_directory_remove (dir_name);
435   GNUNET_log_setup ("perf-datastore-api",
436 #if VERBOSE
437                     "DEBUG",
438 #else
439                     "WARNING",
440 #endif
441                     NULL);
442   ret = check ();
443   if (pos != plugin_name)
444     pos[0] = '.';
445 #if REPORT_ID
446   fprintf (stderr, "\n");
447 #endif
448   GNUNET_DISK_directory_remove (dir_name);
449   return ret;
450 }
451
452 /* end of perf_datastore_api.c */