rewriting perf test to match new API --- now it fails...
[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 #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_DATASTORE_put (datastore,
230                             0,
231                             &key,
232                             size,
233                             data,
234                             i+1,
235                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
236                             i, 0,
237                             GNUNET_TIME_relative_to_absolute 
238                             (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
239                                                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
240                             1, 1, TIMEOUT,
241                             &check_success, 
242                             crc);
243       break;
244     case RP_CUT:
245       /* trim down below MAX_SIZE again */
246       GNUNET_DATASTORE_get_for_replication (datastore, 
247                                             1, 1, TIMEOUT,
248                                             &delete_value,
249                                             crc);
250       break;
251     case RP_REPORT:
252       printf (
253 #if REPORT_ID
254                "\n"
255 #endif
256                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
257                stored_bytes / 1024,     /* used size in k */
258                stored_ops / 1024,        /* total operations (in k) */
259                1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value));
260       crc->phase = RP_PUT;
261       crc->j = 0;
262       GNUNET_SCHEDULER_add_continuation (&run_continuation,
263                                          crc,
264                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
265       break;
266     case RP_DONE:
267       GNUNET_snprintf (gstr, sizeof (gstr),
268                        "PUT operations in %s-datastore",
269                        plugin_name);
270       if (crc->i == ITERATIONS)
271         GAUGER ("DATASTORE", gstr, 1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).rel_value), "op/s");
272       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
273       GNUNET_free (crc);
274       ok = 0;
275       break;
276     default:
277       GNUNET_assert (0);      
278     }
279 }
280
281
282 static void
283 run_tests (void *cls,
284            int success,
285            const char *msg)
286 {
287   struct CpsRunContext *crc = cls;
288
289   if (success != GNUNET_YES)
290     {
291       fprintf (stderr,
292                "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
293                msg);
294       GNUNET_free (crc);
295       return;
296     }
297   GNUNET_SCHEDULER_add_continuation (&run_continuation,
298                                      crc,
299                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
300 }
301
302
303 static void
304 run (void *cls,
305      char *const *args,
306      const char *cfgfile,
307      const struct GNUNET_CONFIGURATION_Handle *cfg)
308 {
309   struct CpsRunContext *crc;
310   static GNUNET_HashCode zkey;
311
312   datastore = GNUNET_DATASTORE_connect (cfg);
313   start_time = GNUNET_TIME_absolute_get ();
314   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
315   crc->cfg = cfg;
316   crc->phase = RP_PUT;
317   if (NULL ==
318       GNUNET_DATASTORE_put (datastore, 0,
319                             &zkey, 4, "TEST",
320                             GNUNET_BLOCK_TYPE_TEST,
321                             0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
322                             0, 1, GNUNET_TIME_UNIT_MINUTES,
323                             &run_tests, crc))
324     {
325       fprintf (stderr,
326                "Test 'put' operation failed.\n");
327       ok = 1;
328       GNUNET_free (crc);
329     }
330 }
331
332
333 static int
334 check ()
335 {
336   struct GNUNET_OS_Process *proc;
337   char cfg_name[128];
338   char *const argv[] = { 
339     "perf-datastore-api",
340     "-c",
341     cfg_name,
342 #if VERBOSE
343     "-L", "DEBUG",
344 #endif
345     NULL
346   };
347   struct GNUNET_GETOPT_CommandLineOption options[] = {
348     GNUNET_GETOPT_OPTION_END
349   };
350
351   GNUNET_snprintf (cfg_name,
352                    sizeof (cfg_name),
353                    "test_datastore_api_data_%s.conf",
354                    plugin_name);
355   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
356                                  "gnunet-service-arm",
357 #if VERBOSE
358                                  "-L", "DEBUG",
359 #endif
360                                  "-c", cfg_name, NULL);
361   GNUNET_assert (NULL != proc);
362   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
363                       argv, "perf-datastore-api", "nohelp",
364                       options, &run, NULL);
365   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
366     {
367       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
368       ok = 1;
369     }
370   GNUNET_OS_process_wait (proc);
371   GNUNET_OS_process_close (proc);
372   proc = NULL;
373   return ok;
374 }
375
376
377 int
378 main (int argc, char *argv[])
379 {
380   int ret;
381   char *pos;
382   char dir_name[128];
383
384   /* determine name of plugin to use */
385   plugin_name = argv[0];
386   while (NULL != (pos = strstr(plugin_name, "_")))
387     plugin_name = pos+1;
388   if (NULL != (pos = strstr(plugin_name, ".")))
389     pos[0] = 0;
390   else
391     pos = (char *) plugin_name;
392
393   GNUNET_snprintf (dir_name,
394                    sizeof (dir_name),
395                    "/tmp/test-gnunet-datastore-%s",
396                    plugin_name);
397   GNUNET_DISK_directory_remove (dir_name);
398   GNUNET_log_setup ("perf-datastore-api",
399 #if VERBOSE
400                     "DEBUG",
401 #else
402                     "WARNING",
403 #endif
404                     NULL);
405   ret = check ();
406   if (pos != plugin_name)
407     pos[0] = '.';
408 #if REPORT_ID
409   fprintf (stderr, "\n");
410 #endif
411   GNUNET_DISK_directory_remove (dir_name);
412   return ret;
413 }
414
415 /* end of perf_datastore_api.c */