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