97774198c3b3070d168b555697979d95c0be036e
[oweals/gnunet.git] / src / datastore / perf_datastore_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2007, 2009, 2011, 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 #include "platform.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_datastore_service.h"
38 #include "gnunet_testing_lib.h"
39 #include <gauger.h>
40
41 /**
42  * How long until we give up on transmitting the message?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
45
46 /**
47  * Target datastore size (in bytes).
48  */
49 #define MAX_SIZE (1024LL * 1024 * 4)
50
51 /**
52  * Report progress outside of major reports? Should probably be #GNUNET_YES if
53  * size is > 16 MB.
54  */
55 #define REPORT_ID GNUNET_YES
56
57 /**
58  * Number of put operations equivalent to 1/3rd of #MAX_SIZE
59  */
60 #define PUT_10 MAX_SIZE / 32 / 1024 / 3
61
62 /**
63  * Total number of iterations (each iteration doing
64  * PUT_10 put operations); we report full status every
65  * 10 iterations.  Abort with CTRL-C.
66  */
67 #define ITERATIONS 8
68
69 /**
70  * Total number of iterations to do to go beyond the quota.
71  * The quota is set to 10 MB or 2.5 times #MAX_SIZE,
72  * so we got 16 times #MAX_SIZE to be sure to hit it a LOT.
73  */
74 #define QUOTA_PUTS (MAX_SIZE / 32 / 1024 * 16LL)
75
76
77 /**
78  * Number of bytes stored in the datastore in total.
79  */
80 static unsigned long long stored_bytes;
81
82 /**
83  * Number of entries stored in the datastore in total.
84  */
85 static unsigned long long stored_entries;
86
87 /**
88  * Number of database operations performed.  Inserting
89  * counts as one operation, deleting as two (as deletion
90  * requires selecting a value for deletion first).
91  */
92 static unsigned long long stored_ops;
93
94 /**
95  * Start time of the benchmark.
96  */
97 static struct GNUNET_TIME_Absolute start_time;
98
99 /**
100  * Database backend we use.
101  */
102 static const char *plugin_name;
103
104 /**
105  * Handle to the datastore.
106  */
107 static struct GNUNET_DATASTORE_Handle *datastore;
108
109 /**
110  * Value we return from #main().
111  */
112 static int ok;
113
114 /**
115  * Which phase of the process are we in?
116  */
117 enum RunPhase
118 {
119   /**
120    * We are done (shutting down normally).
121    */
122   RP_DONE = 0,
123
124   /**
125    * We are adding new entries to the datastore.
126    */
127   RP_PUT,
128
129   /**
130    * We are deleting entries from the datastore.
131    */
132   RP_CUT,
133
134   /**
135    * We are putting as much as we can to see how the database performs
136    * when it reaches the quota and has to auto-delete (see #3903).
137    */
138   RP_PUT_QUOTA,
139
140   /**
141    * We are generating a report.
142    */
143   RP_REPORT,
144
145   /**
146    * Execution failed with some kind of error.
147    */
148   RP_ERROR
149 };
150
151
152 /**
153  * Closure we give to all of the functions executing the
154  * benchmark.  Could right now be global, but this allows
155  * us to theoretically run multiple clients "in parallel".
156  */
157 struct CpsRunContext
158 {
159   /**
160    * Execution phase we are in.
161    */
162   enum RunPhase phase;
163
164   /**
165    * Size of the value we are currently storing (during #RP_PUT).
166    */
167   size_t size;
168
169   /**
170    * Current iteration counter, we are done with the benchmark
171    * once it hits #ITERATIONS.
172    */
173   unsigned int i;
174
175   /**
176    * Counts the number of items put in the current phase.
177    * Once it hits #PUT_10, we progress tot he #RP_CUT phase
178    * or are done if @e i reaches #ITERATIONS.
179    */
180   unsigned int j;
181 };
182
183
184 /**
185  * Main state machine.  Executes the next step of the benchmark
186  * depending on the current state.
187  *
188  * @param cls the `struct CpsRunContext`
189  */
190 static void
191 run_continuation (void *cls);
192
193
194 /**
195  * Continuation called to notify client about result of the insertion
196  * operation.  Checks for errors, updates our iteration counters and
197  * continues execution with #run_continuation().
198  *
199  * @param cls the `struct CpsRunContext`
200  * @param success #GNUNET_SYSERR on failure
201  * @param min_expiration minimum expiration time required for content to be stored
202  *                by the datacache at this time, zero for unknown
203  * @param msg NULL on success, otherwise an error message
204  */
205 static void
206 check_success (void *cls,
207                int success,
208                struct GNUNET_TIME_Absolute min_expiration,
209                const char *msg)
210 {
211   struct CpsRunContext *crc = cls;
212
213 #if REPORT_ID
214   FPRINTF (stderr, "%s",  (GNUNET_OK == success) ? "I" : "i");
215 #endif
216   if (GNUNET_OK != success)
217   {
218     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219                 "Check success failed: `%s'\n",
220                 msg);
221     crc->phase = RP_ERROR;
222     GNUNET_SCHEDULER_add_now (&run_continuation,
223                               crc);
224     return;
225   }
226   stored_bytes += crc->size;
227   stored_ops++;
228   stored_entries++;
229   crc->j++;
230   switch (crc->phase)
231   {
232   case RP_PUT:
233     if (crc->j >= PUT_10)
234     {
235       crc->j = 0;
236       crc->i++;
237       if (crc->i == ITERATIONS)
238         crc->phase = RP_PUT_QUOTA;
239       else
240         crc->phase = RP_CUT;
241     }
242     break;
243   case RP_PUT_QUOTA:
244     if (crc->j >= QUOTA_PUTS)
245     {
246       crc->j = 0;
247       crc->phase = RP_DONE;
248     }
249     break;
250   default:
251     GNUNET_assert (0);
252   }
253   GNUNET_SCHEDULER_add_now (&run_continuation,
254                             crc);
255 }
256
257
258 /**
259  * Continuation called to notify client about result of the
260  * deletion operation.  Checks for errors and continues
261  * execution with #run_continuation().
262  *
263  * @param cls the `struct CpsRunContext`
264  * @param success #GNUNET_SYSERR on failure
265  * @param min_expiration minimum expiration time required for content to be stored
266  *                by the datacache at this time, zero for unknown
267  * @param msg NULL on success, otherwise an error message
268  */
269 static void
270 remove_next (void *cls,
271              int success,
272              struct GNUNET_TIME_Absolute min_expiration,
273              const char *msg)
274 {
275   struct CpsRunContext *crc = cls;
276
277   if (GNUNET_OK != success)
278   {
279     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280                 "remove_next failed: `%s'\n",
281                 msg);
282     crc->phase = RP_ERROR;
283     GNUNET_SCHEDULER_add_now (&run_continuation,
284                               crc);
285     return;
286   }
287 #if REPORT_ID
288   FPRINTF (stderr, "%s",  "D");
289 #endif
290   GNUNET_assert (GNUNET_OK == success);
291   GNUNET_SCHEDULER_add_now (&run_continuation,
292                             crc);
293 }
294
295
296 /**
297  * We have selected a value for deletion, trigger removal.
298  *
299  * @param cls the `struct CpsRunContext`
300  * @param key key for the content
301  * @param size number of bytes in data
302  * @param data content stored
303  * @param type type of the content
304  * @param priority priority of the content
305  * @param anonymity anonymity-level for the content
306  * @param expiration expiration time for the content
307  * @param uid unique identifier for the datum;
308  *        maybe 0 if no unique identifier is available
309  */
310 static void
311 delete_value (void *cls,
312               const struct GNUNET_HashCode *key,
313               size_t size,
314               const void *data,
315               enum GNUNET_BLOCK_Type type,
316               uint32_t priority,
317               uint32_t anonymity,
318               struct GNUNET_TIME_Absolute expiration,
319               uint64_t uid)
320 {
321   struct CpsRunContext *crc = cls;
322
323   GNUNET_assert (NULL != key);
324   stored_ops++;
325   stored_bytes -= size;
326   stored_entries--;
327   stored_ops++;
328   if (stored_bytes < MAX_SIZE)
329     crc->phase = RP_PUT;
330   GNUNET_assert (NULL !=
331                  GNUNET_DATASTORE_remove (datastore,
332                                           key,
333                                           size,
334                                           data, 1, 1,
335                                           TIMEOUT,
336                                           &remove_next, crc));
337 }
338
339
340 /**
341  * Main state machine.  Executes the next step of the benchmark
342  * depending on the current state.
343  *
344  * @param cls the `struct CpsRunContext`
345  */
346 static void
347 run_continuation (void *cls)
348 {
349   struct CpsRunContext *crc = cls;
350   size_t size;
351   static struct GNUNET_HashCode key;
352   static char data[65536];
353   char gstr[128];
354
355   ok = (int) crc->phase;
356   switch (crc->phase)
357   {
358   case RP_PUT:
359     memset (&key,
360             256 - crc->i,
361             sizeof (struct GNUNET_HashCode));
362     /* most content is 32k */
363     size = 32 * 1024;
364     if (0 ==
365         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
366                                   16)) /* but some of it is less! */
367       size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
368                                        32 * 1024);
369     crc->size = size = size - (size & 7);       /* always multiple of 8 */
370     GNUNET_CRYPTO_hash (&key,
371                         sizeof (struct GNUNET_HashCode),
372                         &key);
373     memset (data,
374             (int) crc->j,
375             size);
376     if (crc->j > 255)
377       memset (data,
378               (int) (crc->j - 255),
379               size / 2);
380     data[0] = crc->i;
381     GNUNET_assert (NULL !=
382                    GNUNET_DATASTORE_put (datastore,
383                                          0,
384                                          &key,
385                                          size,
386                                          data,
387                                          crc->j + 1,
388                                          GNUNET_CRYPTO_random_u32
389                                          (GNUNET_CRYPTO_QUALITY_WEAK, 100),
390                                          crc->j,
391                                          0,
392                                          GNUNET_TIME_relative_to_absolute
393                                          (GNUNET_TIME_relative_multiply
394                                           (GNUNET_TIME_UNIT_SECONDS,
395                                            GNUNET_CRYPTO_random_u32
396                                            (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
397                                          1,
398                                          1,
399                                          TIMEOUT,
400                                          &check_success, crc));
401     break;
402   case RP_CUT:
403     /* trim down below MAX_SIZE again */
404     GNUNET_assert (NULL !=
405                    GNUNET_DATASTORE_get_for_replication (datastore,
406                                                          1, 1,
407                                                          TIMEOUT,
408                                                          &delete_value,
409                                                          crc));
410     break;
411   case RP_REPORT:
412     printf (
413 #if REPORT_ID
414              "\n"
415 #endif
416              "Stored %llu kB / %lluk ops / %llu ops/s\n",
417              stored_bytes / 1024,  /* used size in k */
418              stored_ops / 1024, /* total operations (in k) */
419              1000LL * 1000LL * stored_ops / (1 +
420                                              GNUNET_TIME_absolute_get_duration
421                                              (start_time).rel_value_us));
422     crc->phase = RP_PUT;
423     crc->j = 0;
424     GNUNET_SCHEDULER_add_now (&run_continuation,
425                               crc);
426     break;
427   case RP_PUT_QUOTA:
428     memset (&key,
429             256 - crc->i,
430             sizeof (struct GNUNET_HashCode));
431     /* most content is 32k */
432     size = 32 * 1024;
433     if (0 ==
434         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
435                                   16)) /* but some of it is less! */
436       size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
437                                        32 * 1024);
438     crc->size = size = size - (size & 7);       /* always multiple of 8 */
439     GNUNET_CRYPTO_hash (&key,
440                         sizeof (struct GNUNET_HashCode),
441                         &key);
442     memset (data,
443             (int) crc->j,
444             size);
445     if (crc->j > 255)
446       memset (data,
447               (int) (crc->j - 255),
448               size / 2);
449     data[0] = crc->i;
450     GNUNET_assert (NULL !=
451                    GNUNET_DATASTORE_put (datastore,
452                                          0, /* reservation ID */
453                                          &key,
454                                          size,
455                                          data,
456                                          crc->j + 1, /* type */
457                                          GNUNET_CRYPTO_random_u32
458                                          (GNUNET_CRYPTO_QUALITY_WEAK,
459                                           100), /* priority */
460                                          crc->j, /* anonymity */
461                                          0, /* replication */
462                                          GNUNET_TIME_relative_to_absolute
463                                          (GNUNET_TIME_relative_multiply
464                                           (GNUNET_TIME_UNIT_SECONDS,
465                                            GNUNET_CRYPTO_random_u32
466                                            (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
467                                          1,
468                                          1,
469                                          TIMEOUT,
470                                          &check_success, crc));
471     break;
472
473   case RP_DONE:
474     GNUNET_snprintf (gstr,
475                      sizeof (gstr),
476                      "DATASTORE-%s",
477                      plugin_name);
478     if ((crc->i == ITERATIONS) && (stored_ops > 0))
479     {
480       GAUGER (gstr,
481               "PUT operation duration",
482               GNUNET_TIME_absolute_get_duration (start_time).rel_value_us / 1000LL /
483               stored_ops,
484               "ms/operation");
485       fprintf (stdout,
486                "\nPUT performance: %s for %llu operations\n",
487                GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start_time),
488                                                        GNUNET_YES),
489                stored_ops);
490       fprintf (stdout,
491                "PUT performance: %llu ms/operation\n",
492                GNUNET_TIME_absolute_get_duration (start_time).rel_value_us / 1000LL /
493                stored_ops);
494     }
495     GNUNET_DATASTORE_disconnect (datastore,
496                                  GNUNET_YES);
497     GNUNET_free (crc);
498     ok = 0;
499     break;
500   case RP_ERROR:
501     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
502     GNUNET_free (crc);
503     ok = 1;
504     break;
505   default:
506     GNUNET_assert (0);
507   }
508 }
509
510
511 /**
512  * Function called with the result of the initial PUT operation.  If
513  * the PUT succeeded, we start the actual benchmark loop, otherwise we
514  * bail out with an error.
515  *
516  *
517  * @param cls closure
518  * @param success #GNUNET_SYSERR on failure
519  * @param min_expiration minimum expiration time required for content to be stored
520  *                by the datacache at this time, zero for unknown
521  * @param msg NULL on success, otherwise an error message
522  */
523 static void
524 run_tests (void *cls,
525            int success,
526            struct GNUNET_TIME_Absolute min_expiration,
527            const char *msg)
528 {
529   struct CpsRunContext *crc = cls;
530
531   if (success != GNUNET_YES)
532   {
533     FPRINTF (stderr,
534              "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
535              msg);
536     GNUNET_DATASTORE_disconnect (datastore,
537                                  GNUNET_YES);
538     GNUNET_free (crc);
539     return;
540   }
541   GNUNET_SCHEDULER_add_now (&run_continuation,
542                             crc);
543 }
544
545
546 /**
547  * Beginning of the actual execution of the benchmark.
548  * Performs a first test operation (PUT) to verify that
549  * the plugin works at all.
550  *
551  * @param cls NULL
552  * @param cfg configuration to use
553  * @param peer peer handle (unused)
554  */
555 static void
556 run (void *cls,
557      const struct GNUNET_CONFIGURATION_Handle *cfg,
558      struct GNUNET_TESTING_Peer *peer)
559 {
560   struct CpsRunContext *crc;
561   static struct GNUNET_HashCode zkey;
562
563   datastore = GNUNET_DATASTORE_connect (cfg);
564   start_time = GNUNET_TIME_absolute_get ();
565   crc = GNUNET_new (struct CpsRunContext);
566   crc->phase = RP_PUT;
567   if (NULL ==
568       GNUNET_DATASTORE_put (datastore,
569                             0,
570                             &zkey,
571                             4, "TEST",
572                             GNUNET_BLOCK_TYPE_TEST,
573                             0, 0, 0,
574                             GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
575                             0, 1,
576                             TIMEOUT,
577                             &run_tests, crc))
578   {
579     FPRINTF (stderr,
580              "%s",
581              "Test 'put' operation failed.\n");
582     ok = 1;
583     GNUNET_free (crc);
584   }
585 }
586
587
588 /**
589  * Entry point into the test. Determines which configuration / plugin
590  * we are running with based on the name of the binary and starts
591  * the peer.
592  *
593  * @param argc should be 1
594  * @param argv used to determine plugin / configuration name.
595  * @return 0 on success
596  */
597 int
598 main (int argc,
599       char *argv[])
600 {
601   char cfg_name[128];
602
603   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
604   GNUNET_snprintf (cfg_name,
605                    sizeof (cfg_name),
606                    "test_datastore_api_data_%s.conf",
607                    plugin_name);
608   if (0 !=
609       GNUNET_TESTING_peer_run ("perf-gnunet-datastore",
610                                cfg_name,
611                                &run,
612                                NULL))
613     return 1;
614   FPRINTF (stderr, "%s", "\n");
615   return ok;
616 }
617
618 /* end of perf_datastore_api.c */