-fix (C) notices
[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  * @param tc scheduler context (unused)
190  */
191 static void
192 run_continuation (void *cls,
193                   const struct GNUNET_SCHEDULER_TaskContext *tc);
194
195
196 /**
197  * Continuation called to notify client about result of the insertion
198  * operation.  Checks for errors, updates our iteration counters and
199  * continues execution with #run_continuation().
200  *
201  * @param cls the `struct CpsRunContext`
202  * @param success #GNUNET_SYSERR on failure
203  * @param min_expiration minimum expiration time required for content to be stored
204  *                by the datacache at this time, zero for unknown
205  * @param msg NULL on success, otherwise an error message
206  */
207 static void
208 check_success (void *cls,
209                int success,
210                struct GNUNET_TIME_Absolute min_expiration,
211                const char *msg)
212 {
213   struct CpsRunContext *crc = cls;
214
215 #if REPORT_ID
216   FPRINTF (stderr, "%s",  (GNUNET_OK == success) ? "I" : "i");
217 #endif
218   if (GNUNET_OK != success)
219   {
220     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
221                 "Check success failed: `%s'\n",
222                 msg);
223     crc->phase = RP_ERROR;
224     GNUNET_SCHEDULER_add_now (&run_continuation,
225                               crc);
226     return;
227   }
228   stored_bytes += crc->size;
229   stored_ops++;
230   stored_entries++;
231   crc->j++;
232   switch (crc->phase)
233   {
234   case RP_PUT:
235     if (crc->j >= PUT_10)
236     {
237       crc->j = 0;
238       crc->i++;
239       if (crc->i == ITERATIONS)
240         crc->phase = RP_PUT_QUOTA;
241       else
242         crc->phase = RP_CUT;
243     }
244     break;
245   case RP_PUT_QUOTA:
246     if (crc->j >= QUOTA_PUTS)
247     {
248       crc->j = 0;
249       crc->phase = RP_DONE;
250     }
251     break;
252   default:
253     GNUNET_assert (0);
254   }
255   GNUNET_SCHEDULER_add_now (&run_continuation,
256                             crc);
257 }
258
259
260 /**
261  * Continuation called to notify client about result of the
262  * deletion operation.  Checks for errors and continues
263  * execution with #run_continuation().
264  *
265  * @param cls the `struct CpsRunContext`
266  * @param success #GNUNET_SYSERR on failure
267  * @param min_expiration minimum expiration time required for content to be stored
268  *                by the datacache at this time, zero for unknown
269  * @param msg NULL on success, otherwise an error message
270  */
271 static void
272 remove_next (void *cls,
273              int success,
274              struct GNUNET_TIME_Absolute min_expiration,
275              const char *msg)
276 {
277   struct CpsRunContext *crc = cls;
278
279   if (GNUNET_OK != success)
280   {
281     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282                 "remove_next failed: `%s'\n",
283                 msg);
284     crc->phase = RP_ERROR;
285     GNUNET_SCHEDULER_add_now (&run_continuation,
286                               crc);
287     return;
288   }
289 #if REPORT_ID
290   FPRINTF (stderr, "%s",  "D");
291 #endif
292   GNUNET_assert (GNUNET_OK == success);
293   GNUNET_SCHEDULER_add_now (&run_continuation,
294                             crc);
295 }
296
297
298 /**
299  * We have selected a value for deletion, trigger removal.
300  *
301  * @param cls the `struct CpsRunContext`
302  * @param key key for the content
303  * @param size number of bytes in data
304  * @param data content stored
305  * @param type type of the content
306  * @param priority priority of the content
307  * @param anonymity anonymity-level for the content
308  * @param expiration expiration time for the content
309  * @param uid unique identifier for the datum;
310  *        maybe 0 if no unique identifier is available
311  */
312 static void
313 delete_value (void *cls,
314               const struct GNUNET_HashCode *key,
315               size_t size,
316               const void *data,
317               enum GNUNET_BLOCK_Type type,
318               uint32_t priority,
319               uint32_t anonymity,
320               struct GNUNET_TIME_Absolute expiration,
321               uint64_t uid)
322 {
323   struct CpsRunContext *crc = cls;
324
325   GNUNET_assert (NULL != key);
326   stored_ops++;
327   stored_bytes -= size;
328   stored_entries--;
329   stored_ops++;
330   if (stored_bytes < MAX_SIZE)
331     crc->phase = RP_PUT;
332   GNUNET_assert (NULL !=
333                  GNUNET_DATASTORE_remove (datastore,
334                                           key,
335                                           size,
336                                           data, 1, 1,
337                                           TIMEOUT,
338                                           &remove_next, crc));
339 }
340
341
342 /**
343  * Main state machine.  Executes the next step of the benchmark
344  * depending on the current state.
345  *
346  * @param cls the `struct CpsRunContext`
347  * @param tc scheduler context (unused)
348  */
349 static void
350 run_continuation (void *cls,
351                   const struct GNUNET_SCHEDULER_TaskContext *tc)
352 {
353   struct CpsRunContext *crc = cls;
354   size_t size;
355   static struct GNUNET_HashCode key;
356   static char data[65536];
357   char gstr[128];
358
359   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
360     crc->phase = RP_ERROR;
361   ok = (int) crc->phase;
362   switch (crc->phase)
363   {
364   case RP_PUT:
365     memset (&key,
366             256 - crc->i,
367             sizeof (struct GNUNET_HashCode));
368     /* most content is 32k */
369     size = 32 * 1024;
370     if (0 ==
371         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
372                                   16)) /* but some of it is less! */
373       size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
374                                        32 * 1024);
375     crc->size = size = size - (size & 7);       /* always multiple of 8 */
376     GNUNET_CRYPTO_hash (&key,
377                         sizeof (struct GNUNET_HashCode),
378                         &key);
379     memset (data,
380             (int) crc->j,
381             size);
382     if (crc->j > 255)
383       memset (data,
384               (int) (crc->j - 255),
385               size / 2);
386     data[0] = crc->i;
387     GNUNET_assert (NULL !=
388                    GNUNET_DATASTORE_put (datastore,
389                                          0,
390                                          &key,
391                                          size,
392                                          data,
393                                          crc->j + 1,
394                                          GNUNET_CRYPTO_random_u32
395                                          (GNUNET_CRYPTO_QUALITY_WEAK, 100),
396                                          crc->j,
397                                          0,
398                                          GNUNET_TIME_relative_to_absolute
399                                          (GNUNET_TIME_relative_multiply
400                                           (GNUNET_TIME_UNIT_SECONDS,
401                                            GNUNET_CRYPTO_random_u32
402                                            (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
403                                          1,
404                                          1,
405                                          TIMEOUT,
406                                          &check_success, crc));
407     break;
408   case RP_CUT:
409     /* trim down below MAX_SIZE again */
410     GNUNET_assert (NULL !=
411                    GNUNET_DATASTORE_get_for_replication (datastore,
412                                                          1, 1,
413                                                          TIMEOUT,
414                                                          &delete_value,
415                                                          crc));
416     break;
417   case RP_REPORT:
418     printf (
419 #if REPORT_ID
420              "\n"
421 #endif
422              "Stored %llu kB / %lluk ops / %llu ops/s\n",
423              stored_bytes / 1024,  /* used size in k */
424              stored_ops / 1024, /* total operations (in k) */
425              1000LL * 1000LL * stored_ops / (1 +
426                                              GNUNET_TIME_absolute_get_duration
427                                              (start_time).rel_value_us));
428     crc->phase = RP_PUT;
429     crc->j = 0;
430     GNUNET_SCHEDULER_add_now (&run_continuation,
431                               crc);
432     break;
433   case RP_PUT_QUOTA:
434     memset (&key,
435             256 - crc->i,
436             sizeof (struct GNUNET_HashCode));
437     /* most content is 32k */
438     size = 32 * 1024;
439     if (0 ==
440         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
441                                   16)) /* but some of it is less! */
442       size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
443                                        32 * 1024);
444     crc->size = size = size - (size & 7);       /* always multiple of 8 */
445     GNUNET_CRYPTO_hash (&key,
446                         sizeof (struct GNUNET_HashCode),
447                         &key);
448     memset (data,
449             (int) crc->j,
450             size);
451     if (crc->j > 255)
452       memset (data,
453               (int) (crc->j - 255),
454               size / 2);
455     data[0] = crc->i;
456     GNUNET_assert (NULL !=
457                    GNUNET_DATASTORE_put (datastore,
458                                          0, /* reservation ID */
459                                          &key,
460                                          size,
461                                          data,
462                                          crc->j + 1, /* type */
463                                          GNUNET_CRYPTO_random_u32
464                                          (GNUNET_CRYPTO_QUALITY_WEAK,
465                                           100), /* priority */
466                                          crc->j, /* anonymity */
467                                          0, /* replication */
468                                          GNUNET_TIME_relative_to_absolute
469                                          (GNUNET_TIME_relative_multiply
470                                           (GNUNET_TIME_UNIT_SECONDS,
471                                            GNUNET_CRYPTO_random_u32
472                                            (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
473                                          1,
474                                          1,
475                                          TIMEOUT,
476                                          &check_success, crc));
477     break;
478
479   case RP_DONE:
480     GNUNET_snprintf (gstr,
481                      sizeof (gstr),
482                      "DATASTORE-%s",
483                      plugin_name);
484     if ((crc->i == ITERATIONS) && (stored_ops > 0))
485     {
486       GAUGER (gstr,
487               "PUT operation duration",
488               GNUNET_TIME_absolute_get_duration (start_time).rel_value_us / 1000LL /
489               stored_ops,
490               "ms/operation");
491       fprintf (stdout,
492                "\nPUT performance: %s for %llu operations\n",
493                GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start_time),
494                                                        GNUNET_YES),
495                stored_ops);
496       fprintf (stdout,
497                "PUT performance: %llu ms/operation\n",
498                GNUNET_TIME_absolute_get_duration (start_time).rel_value_us / 1000LL /
499                stored_ops);
500     }
501     GNUNET_DATASTORE_disconnect (datastore,
502                                  GNUNET_YES);
503     GNUNET_free (crc);
504     ok = 0;
505     break;
506   case RP_ERROR:
507     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
508     GNUNET_free (crc);
509     ok = 1;
510     break;
511   default:
512     GNUNET_assert (0);
513   }
514 }
515
516
517 /**
518  * Function called with the result of the initial PUT operation.  If
519  * the PUT succeeded, we start the actual benchmark loop, otherwise we
520  * bail out with an error.
521  *
522  *
523  * @param cls closure
524  * @param success #GNUNET_SYSERR on failure
525  * @param min_expiration minimum expiration time required for content to be stored
526  *                by the datacache at this time, zero for unknown
527  * @param msg NULL on success, otherwise an error message
528  */
529 static void
530 run_tests (void *cls,
531            int success,
532            struct GNUNET_TIME_Absolute min_expiration,
533            const char *msg)
534 {
535   struct CpsRunContext *crc = cls;
536
537   if (success != GNUNET_YES)
538   {
539     FPRINTF (stderr,
540              "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
541              msg);
542     GNUNET_DATASTORE_disconnect (datastore,
543                                  GNUNET_YES);
544     GNUNET_free (crc);
545     return;
546   }
547   GNUNET_SCHEDULER_add_now (&run_continuation,
548                             crc);
549 }
550
551
552 /**
553  * Beginning of the actual execution of the benchmark.
554  * Performs a first test operation (PUT) to verify that
555  * the plugin works at all.
556  *
557  * @param cls NULL
558  * @param cfg configuration to use
559  * @param peer peer handle (unused)
560  */
561 static void
562 run (void *cls,
563      const struct GNUNET_CONFIGURATION_Handle *cfg,
564      struct GNUNET_TESTING_Peer *peer)
565 {
566   struct CpsRunContext *crc;
567   static struct GNUNET_HashCode zkey;
568
569   datastore = GNUNET_DATASTORE_connect (cfg);
570   start_time = GNUNET_TIME_absolute_get ();
571   crc = GNUNET_new (struct CpsRunContext);
572   crc->phase = RP_PUT;
573   if (NULL ==
574       GNUNET_DATASTORE_put (datastore,
575                             0,
576                             &zkey,
577                             4, "TEST",
578                             GNUNET_BLOCK_TYPE_TEST,
579                             0, 0, 0,
580                             GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
581                             0, 1,
582                             TIMEOUT,
583                             &run_tests, crc))
584   {
585     FPRINTF (stderr,
586              "%s",
587              "Test 'put' operation failed.\n");
588     ok = 1;
589     GNUNET_free (crc);
590   }
591 }
592
593
594 /**
595  * Entry point into the test. Determines which configuration / plugin
596  * we are running with based on the name of the binary and starts
597  * the peer.
598  *
599  * @param argc should be 1
600  * @param argv used to determine plugin / configuration name.
601  * @return 0 on success
602  */
603 int
604 main (int argc,
605       char *argv[])
606 {
607   char cfg_name[128];
608
609   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
610   GNUNET_snprintf (cfg_name,
611                    sizeof (cfg_name),
612                    "test_datastore_api_data_%s.conf",
613                    plugin_name);
614   if (0 !=
615       GNUNET_TESTING_peer_run ("perf-gnunet-datastore",
616                                cfg_name,
617                                &run,
618                                NULL))
619     return 1;
620   FPRINTF (stderr, "%s", "\n");
621   return ok;
622 }
623
624 /* end of perf_datastore_api.c */