gaugering
[oweals/gnunet.git] / src / datastore / perf_datastore_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007, 2009 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
40 #define VERBOSE GNUNET_NO
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
46
47 static const char *plugin_name;
48
49 static struct GNUNET_DATASTORE_Handle *datastore;
50
51 /**
52  * Target datastore size (in bytes).
53  */
54 #define MAX_SIZE 1024LL * 1024 * 4
55
56 /**
57  * Report progress outside of major reports? Should probably be GNUNET_YES if
58  * size is > 16 MB.
59  */
60 #define REPORT_ID GNUNET_YES
61
62 /**
63  * Number of put operations equivalent to 1/3rd of MAX_SIZE
64  */
65 #define PUT_10 MAX_SIZE / 32 / 1024 / 3
66
67 /**
68  * Total number of iterations (each iteration doing
69  * PUT_10 put operations); we report full status every
70  * 10 iterations.  Abort with CTRL-C.
71  */
72 #define ITERATIONS 8
73
74
75 static unsigned long long stored_bytes;
76
77 static unsigned long long stored_entries;
78
79 static unsigned long long stored_ops;
80
81 static struct GNUNET_TIME_Absolute start_time;
82
83 static int ok;
84
85 enum RunPhase
86   {
87     RP_DONE = 0,
88     RP_PUT,
89     RP_CUT,
90     RP_REPORT
91   };
92
93
94 struct CpsRunContext
95 {
96   const struct GNUNET_CONFIGURATION_Handle *cfg;
97   enum RunPhase phase;
98   int j;
99   unsigned long long size;
100   int i;
101
102   GNUNET_HashCode key;
103   size_t esize;
104   char data[65536];
105 };
106
107
108
109 static void
110 run_continuation (void *cls,
111                   const struct GNUNET_SCHEDULER_TaskContext *tc);
112
113
114
115
116 static void
117 check_success (void *cls,
118                int success,
119                const char *msg)
120 {
121   struct CpsRunContext *crc = cls;
122   if (GNUNET_OK != success)
123     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124                 "%s\n", msg);
125   GNUNET_assert (GNUNET_OK == success);
126 #if REPORT_ID
127   fprintf (stderr, "I");
128 #endif
129   stored_bytes += crc->size;
130   stored_ops++;
131   stored_entries++;
132   crc->j++;
133   if (crc->j == PUT_10)
134     {
135       crc->j = 0;
136       crc->i++;
137       if (crc->i == ITERATIONS)
138         crc->phase = RP_DONE;
139       else
140         crc->phase = RP_CUT;
141     }
142   GNUNET_SCHEDULER_add_continuation (&run_continuation,
143                                      crc,
144                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
145 }
146
147
148 /**
149  * Continuation called to notify client about result of the
150  * operation.
151  *
152  * @param cls closure
153  * @param success GNUNET_SYSERR on failure
154  * @param msg NULL on success, otherwise an error message
155  */
156 static void 
157 remove_next(void *cls,
158             int success,
159             const char *msg)
160 {
161   struct CpsRunContext *crc = cls;
162
163 #if REPORT_ID
164   fprintf (stderr, "D");
165 #endif
166   GNUNET_assert (GNUNET_OK == success);
167   GNUNET_SCHEDULER_add_continuation (&run_continuation,
168                                      crc,
169                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
170 }
171
172
173
174 static void
175 do_delete (void *cls,
176            const struct GNUNET_SCHEDULER_TaskContext *tc)
177 {
178   struct CpsRunContext *crc = cls;
179
180   stored_bytes -= crc->esize;
181   stored_entries--;
182   stored_ops++;
183   GNUNET_DATASTORE_remove (datastore,
184                            &crc->key,
185                            crc->esize,
186                            crc->data,
187                            1, 1, TIMEOUT,
188                            &remove_next,
189                            crc);
190 }
191
192
193
194 static void 
195 delete_value (void *cls,
196               const GNUNET_HashCode * key,
197               size_t size,
198               const void *data,
199               enum GNUNET_BLOCK_Type type,
200               uint32_t priority,
201               uint32_t anonymity,
202               struct GNUNET_TIME_Absolute
203               expiration, uint64_t uid)
204 {
205   struct CpsRunContext *crc = cls;
206
207   if (key == NULL)
208     {
209       if (stored_bytes < MAX_SIZE)
210         {
211           crc->phase = RP_REPORT;
212           GNUNET_SCHEDULER_add_continuation (&run_continuation,
213                                              crc,
214                                              GNUNET_SCHEDULER_REASON_PREREQ_DONE);
215           return;     
216         }
217       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
218                                           &do_delete,
219                                           crc);
220       return;
221     }
222   stored_ops++;
223   if (stored_bytes < MAX_SIZE)
224     {
225       GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
226       return;     
227     }
228   crc->key = *key;
229   crc->esize = size;
230   memcpy (crc->data, data, size);
231   GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
232 }
233
234
235 static void
236 run_continuation (void *cls,
237                   const struct GNUNET_SCHEDULER_TaskContext *tc)
238 {
239   struct CpsRunContext *crc = cls;
240   size_t size;
241   static GNUNET_HashCode key;
242   static char data[65536];
243   int i;
244   int k;
245   char gstr[128];
246
247   ok = (int) crc->phase;
248   switch (crc->phase)
249     {
250     case RP_PUT:
251       memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
252       i = crc->j;
253       k = crc->i;
254       /* most content is 32k */
255       size = 32 * 1024;
256       if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
257         size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
258       crc->size = size = size - (size & 7);     /* always multiple of 8 */
259       GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
260       memset (data, i, size);
261       if (i > 255)
262         memset (data, i - 255, size / 2);
263       data[0] = k;
264       GNUNET_DATASTORE_put (datastore,
265                             0,
266                             &key,
267                             size,
268                             data,
269                             i+1,
270                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
271                             i,
272                             GNUNET_TIME_relative_to_absolute 
273                             (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
274                                                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
275                             1, 1, TIMEOUT,
276                             &check_success, 
277                             crc);
278       break;
279     case RP_CUT:
280       /* trim down below MAX_SIZE again */
281       GNUNET_DATASTORE_get_random (datastore, 
282                                    1, 1, TIMEOUT,
283                                    &delete_value,
284                                    crc);
285       break;
286     case RP_REPORT:
287       printf (
288 #if REPORT_ID
289                "\n"
290 #endif
291                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
292                stored_bytes / 1024,     /* used size in k */
293                stored_ops / 1024,        /* total operations (in k) */
294                1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value));
295       crc->phase = RP_PUT;
296       crc->j = 0;
297       start = GNUNET_TIME_absolute_get ();
298       GNUNET_SCHEDULER_add_continuation (&run_continuation,
299                                          crc,
300                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
301       break;
302     case RP_DONE:
303       GNUNET_snprintf (gstr, sizeof (gstr),
304                        "PUT operations in %s-datastore_op/s",
305                        plugin_name);
306       if (crc->i == ITERATIONS)
307         GAUGER (1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value),
308                 gstr);  
309       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
310       GNUNET_free (crc);
311       ok = 0;
312       break;
313     default:
314       GNUNET_assert (0);      
315     }
316 }
317
318
319
320 static void
321 run_tests (void *cls,
322            int success,
323            const char *msg)
324 {
325   struct CpsRunContext *crc = cls;
326
327   if (success != GNUNET_YES)
328     {
329       fprintf (stderr,
330                "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
331                msg);
332       GNUNET_free (crc);
333       return;
334     }
335   GNUNET_SCHEDULER_add_continuation (&run_continuation,
336                                      crc,
337                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
338 }
339
340
341 static void
342 run (void *cls,
343      char *const *args,
344      const char *cfgfile,
345      const struct GNUNET_CONFIGURATION_Handle *cfg)
346 {
347   struct CpsRunContext *crc;
348   static GNUNET_HashCode zkey;
349
350   datastore = GNUNET_DATASTORE_connect (cfg);
351   start_time = GNUNET_TIME_absolute_get ();
352   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
353   crc->cfg = cfg;
354   crc->phase = RP_PUT;
355   if (NULL ==
356       GNUNET_DATASTORE_put (datastore, 0,
357                             &zkey, 4, "TEST",
358                             GNUNET_BLOCK_TYPE_TEST,
359                             0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
360                             0, 1, GNUNET_TIME_UNIT_MINUTES,
361                             &run_tests, crc))
362     {
363       fprintf (stderr,
364                "Test 'put' operation failed.\n");
365       ok = 1;
366       GNUNET_free (crc);
367     }
368 }
369
370
371 static int
372 check ()
373 {
374   struct GNUNET_OS_Process *proc;
375   char cfg_name[128];
376   char *const argv[] = { 
377     "perf-datastore-api",
378     "-c",
379     cfg_name,
380 #if VERBOSE
381     "-L", "DEBUG",
382 #endif
383     NULL
384   };
385   struct GNUNET_GETOPT_CommandLineOption options[] = {
386     GNUNET_GETOPT_OPTION_END
387   };
388
389   GNUNET_snprintf (cfg_name,
390                    sizeof (cfg_name),
391                    "test_datastore_api_data_%s.conf",
392                    plugin_name);
393   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
394                                  "gnunet-service-arm",
395 #if VERBOSE
396                                  "-L", "DEBUG",
397 #endif
398                                  "-c", cfg_name, NULL);
399   GNUNET_assert (NULL != proc);
400   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
401                       argv, "perf-datastore-api", "nohelp",
402                       options, &run, NULL);
403   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
404     {
405       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
406       ok = 1;
407     }
408   GNUNET_OS_process_wait (proc);
409   GNUNET_OS_process_close (proc);
410   proc = NULL;
411   return ok;
412 }
413
414
415 int
416 main (int argc, char *argv[])
417 {
418   int ret;
419   char *pos;
420   char dir_name[128];
421
422   /* determine name of plugin to use */
423   plugin_name = argv[0];
424   while (NULL != (pos = strstr(plugin_name, "_")))
425     plugin_name = pos+1;
426   if (NULL != (pos = strstr(plugin_name, ".")))
427     pos[0] = 0;
428   else
429     pos = (char *) plugin_name;
430
431   GNUNET_snprintf (dir_name,
432                    sizeof (dir_name),
433                    "/tmp/test-gnunet-datastore-%s",
434                    plugin_name);
435   GNUNET_DISK_directory_remove (dir_name);
436   GNUNET_log_setup ("perf-datastore-api",
437 #if VERBOSE
438                     "DEBUG",
439 #else
440                     "WARNING",
441 #endif
442                     NULL);
443   ret = check ();
444   if (pos != plugin_name)
445     pos[0] = '.';
446 #if REPORT_ID
447   fprintf (stderr, "\n");
448 #endif
449   GNUNET_DISK_directory_remove (dir_name);
450   return ret;
451 }
452
453 /* end of perf_datastore_api.c */