fix
[oweals/gnunet.git] / src / datastore / perf_datastore_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007, 2009, 2011 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     RP_ERROR
93   };
94
95
96 struct CpsRunContext
97 {
98   const struct GNUNET_CONFIGURATION_Handle *cfg;
99   enum RunPhase phase;
100   int j;
101   unsigned long long size;
102   int i;
103 };
104
105
106
107 static void
108 run_continuation (void *cls,
109                   const struct GNUNET_SCHEDULER_TaskContext *tc);
110
111
112
113
114 static void
115 check_success (void *cls,
116                int success,
117                const char *msg)
118 {
119   struct CpsRunContext *crc = cls;
120
121   if (GNUNET_OK != success)
122     {
123       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124                   "Check success failed: `%s'\n", msg);
125       crc->phase = RP_ERROR;
126       GNUNET_SCHEDULER_add_now (&run_continuation,
127                                 crc);
128       return;
129     }
130 #if REPORT_ID
131   fprintf (stderr, "I");
132 #endif
133   stored_bytes += crc->size;
134   stored_ops++;
135   stored_entries++;
136   crc->j++;
137   if (crc->j >= PUT_10)
138     {
139       crc->j = 0;
140       crc->i++;
141       if (crc->i == ITERATIONS)
142         crc->phase = RP_DONE;
143       else
144         crc->phase = RP_CUT;
145     }
146   GNUNET_SCHEDULER_add_continuation (&run_continuation,
147                                      crc,
148                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
149 }
150
151
152 /**
153  * Continuation called to notify client about result of the
154  * operation.
155  *
156  * @param cls closure
157  * @param success GNUNET_SYSERR on failure
158  * @param msg NULL on success, otherwise an error message
159  */
160 static void 
161 remove_next(void *cls,
162             int success,
163             const char *msg)
164 {
165   struct CpsRunContext *crc = cls;
166
167   if (GNUNET_OK != success)
168     {
169       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170                   "remove_next failed: `%s'\n", msg);
171       crc->phase = RP_ERROR;
172       GNUNET_SCHEDULER_add_now (&run_continuation,
173                                 crc);
174       return;
175     }
176 #if REPORT_ID
177   fprintf (stderr, "D");
178 #endif
179   GNUNET_assert (GNUNET_OK == success);
180   GNUNET_SCHEDULER_add_now (&run_continuation,
181                             crc);
182 }
183
184
185 static void 
186 delete_value (void *cls,
187               const GNUNET_HashCode *key,
188               size_t size,
189               const void *data,
190               enum GNUNET_BLOCK_Type type,
191               uint32_t priority,
192               uint32_t anonymity,
193               struct GNUNET_TIME_Absolute
194               expiration, uint64_t uid)
195 {
196   struct CpsRunContext *crc = cls;
197
198   GNUNET_assert (NULL != key);
199   stored_ops++;
200   stored_bytes -= size;
201   stored_entries--;
202   stored_ops++;
203   if (stored_bytes < MAX_SIZE)
204     crc->phase = RP_PUT;
205   GNUNET_assert (NULL !=
206                  GNUNET_DATASTORE_remove (datastore,
207                                           key,
208                                           size,
209                                           data,
210                                           1, 1, TIMEOUT,
211                                           &remove_next,
212                                           crc));
213 }
214
215
216 static void
217 run_continuation (void *cls,
218                   const struct GNUNET_SCHEDULER_TaskContext *tc)
219 {
220   struct CpsRunContext *crc = cls;
221   size_t size;
222   static GNUNET_HashCode key;
223   static char data[65536];
224   int i;
225   int k;
226   char gstr[128];
227
228   ok = (int) crc->phase;
229   switch (crc->phase)
230     {
231     case RP_PUT:
232       memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
233       i = crc->j;
234       k = crc->i;
235       /* most content is 32k */
236       size = 32 * 1024;
237       if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
238         size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
239       crc->size = size = size - (size & 7);     /* always multiple of 8 */
240       GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
241       memset (data, i, size);
242       if (i > 255)
243         memset (data, i - 255, size / 2);
244       data[0] = k;
245       GNUNET_assert (NULL !=
246                      GNUNET_DATASTORE_put (datastore,
247                                            0,
248                                            &key,
249                                            size,
250                                            data,
251                                            i+1,
252                                            GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
253                                            i, 0,
254                                            GNUNET_TIME_relative_to_absolute 
255                                            (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
256                                                                            GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
257                                            1, 1, TIMEOUT,
258                                            &check_success, 
259                                            crc));
260       break;
261     case RP_CUT:
262       /* trim down below MAX_SIZE again */
263       GNUNET_assert (NULL !=
264                      GNUNET_DATASTORE_get_for_replication (datastore, 
265                                                            1, 1, TIMEOUT,
266                                                            &delete_value,
267                                                            crc));
268       break;
269     case RP_REPORT:
270       printf (
271 #if REPORT_ID
272                "\n"
273 #endif
274                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
275                stored_bytes / 1024,     /* used size in k */
276                stored_ops / 1024,        /* total operations (in k) */
277                1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value));
278       crc->phase = RP_PUT;
279       crc->j = 0;
280       GNUNET_SCHEDULER_add_continuation (&run_continuation,
281                                          crc,
282                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
283       break;
284     case RP_DONE:
285       GNUNET_snprintf (gstr, sizeof (gstr),
286                        "PUT operations in %s-datastore",
287                        plugin_name);
288       if (crc->i == ITERATIONS)
289         GAUGER ("DATASTORE", gstr, 1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value), "op/s");
290       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
291       GNUNET_free (crc);
292       ok = 0;
293       break;
294     case RP_ERROR:
295       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
296       GNUNET_free (crc);
297       ok = 1;
298       break;
299     default:
300       GNUNET_assert (0);      
301     }
302 }
303
304
305 static void
306 run_tests (void *cls,
307            int success,
308            const char *msg)
309 {
310   struct CpsRunContext *crc = cls;
311
312   if (success != GNUNET_YES)
313     {
314       fprintf (stderr,
315                "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
316                msg);
317       GNUNET_free (crc);
318       return;
319     }
320   GNUNET_SCHEDULER_add_continuation (&run_continuation,
321                                      crc,
322                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
323 }
324
325
326 static void
327 run (void *cls,
328      char *const *args,
329      const char *cfgfile,
330      const struct GNUNET_CONFIGURATION_Handle *cfg)
331 {
332   struct CpsRunContext *crc;
333   static GNUNET_HashCode zkey;
334
335   datastore = GNUNET_DATASTORE_connect (cfg);
336   start_time = GNUNET_TIME_absolute_get ();
337   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
338   crc->cfg = cfg;
339   crc->phase = RP_PUT;
340   if (NULL ==
341       GNUNET_DATASTORE_put (datastore, 0,
342                             &zkey, 4, "TEST",
343                             GNUNET_BLOCK_TYPE_TEST,
344                             0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
345                             0, 1, GNUNET_TIME_UNIT_MINUTES,
346                             &run_tests, crc))
347     {
348       fprintf (stderr,
349                "Test 'put' operation failed.\n");
350       ok = 1;
351       GNUNET_free (crc);
352     }
353 }
354
355
356 static int
357 check ()
358 {
359   struct GNUNET_OS_Process *proc;
360   char cfg_name[128];
361   char *const argv[] = { 
362     "perf-datastore-api",
363     "-c",
364     cfg_name,
365 #if VERBOSE
366     "-L", "DEBUG",
367 #endif
368     NULL
369   };
370   struct GNUNET_GETOPT_CommandLineOption options[] = {
371     GNUNET_GETOPT_OPTION_END
372   };
373
374   GNUNET_snprintf (cfg_name,
375                    sizeof (cfg_name),
376                    "test_datastore_api_data_%s.conf",
377                    plugin_name);
378   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
379                                  "gnunet-service-arm",
380 #if VERBOSE
381                                  "-L", "DEBUG",
382 #endif
383                                  "-c", cfg_name, NULL);
384   GNUNET_assert (NULL != proc);
385   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
386                       argv, "perf-datastore-api", "nohelp",
387                       options, &run, NULL);
388   sleep (1); /* give datastore chance to process 'DROP' */
389   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
390     {
391       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
392       ok = 1;
393     }
394   GNUNET_OS_process_wait (proc);
395   GNUNET_OS_process_close (proc);
396   proc = NULL;
397   return ok;
398 }
399
400
401 int
402 main (int argc, char *argv[])
403 {
404   int ret;
405   char *pos;
406   char dir_name[128];
407
408   /* determine name of plugin to use */
409   plugin_name = argv[0];
410   while (NULL != (pos = strstr(plugin_name, "_")))
411     plugin_name = pos+1;
412   if (NULL != (pos = strstr(plugin_name, ".")))
413     pos[0] = 0;
414   else
415     pos = (char *) plugin_name;
416
417   GNUNET_snprintf (dir_name,
418                    sizeof (dir_name),
419                    "/tmp/test-gnunet-datastore-%s",
420                    plugin_name);
421   GNUNET_DISK_directory_remove (dir_name);
422   GNUNET_log_setup ("perf-datastore-api",
423 #if VERBOSE
424                     "DEBUG",
425 #else
426                     "WARNING",
427 #endif
428                     NULL);
429   ret = check ();
430   if (pos != plugin_name)
431     pos[0] = '.';
432 #if REPORT_ID
433   fprintf (stderr, "\n");
434 #endif
435   GNUNET_DISK_directory_remove (dir_name);
436   return ret;
437 }
438
439 /* end of perf_datastore_api.c */