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