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