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