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