fixes
[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 2, 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
48 static struct GNUNET_DATASTORE_Handle *datastore;
49
50 /**
51  * Target datastore size (in bytes).
52  */
53 #define MAX_SIZE 1024LL * 1024 * 4
54
55 /**
56  * Report progress outside of major reports? Should probably be GNUNET_YES if
57  * size is > 16 MB.
58  */
59 #define REPORT_ID GNUNET_YES
60
61 /**
62  * Number of put operations equivalent to 1/3rd of MAX_SIZE
63  */
64 #define PUT_10 MAX_SIZE / 32 / 1024 / 3
65
66 /**
67  * Total number of iterations (each iteration doing
68  * PUT_10 put operations); we report full status every
69  * 10 iterations.  Abort with CTRL-C.
70  */
71 #define ITERATIONS 8
72
73
74 static unsigned long long stored_bytes;
75
76 static unsigned long long stored_entries;
77
78 static unsigned long long stored_ops;
79
80 static struct GNUNET_TIME_Absolute start_time;
81
82 static int ok;
83
84 enum RunPhase
85   {
86     RP_DONE = 0,
87     RP_PUT,
88     RP_CUT,
89     RP_REPORT
90   };
91
92
93 struct CpsRunContext
94 {
95   struct GNUNET_SCHEDULER_Handle *sched;
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   uint32_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 (crc->sched,
143                                      &run_continuation,
144                                      crc,
145                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
146 }
147
148
149 /**
150  * Continuation called to notify client about result of the
151  * operation.
152  *
153  * @param cls closure
154  * @param success GNUNET_SYSERR on failure
155  * @param msg NULL on success, otherwise an error message
156  */
157 static void 
158 remove_next(void *cls,
159             int success,
160             const char *msg)
161 {
162   struct CpsRunContext *crc = cls;
163
164 #if REPORT_ID
165   fprintf (stderr, "D");
166 #endif
167   GNUNET_assert (GNUNET_OK == success);
168   GNUNET_SCHEDULER_add_continuation (crc->sched,
169                                      &run_continuation,
170                                      crc,
171                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
172 }
173
174
175
176 static void
177 do_delete (void *cls,
178            const struct GNUNET_SCHEDULER_TaskContext *tc)
179 {
180   struct CpsRunContext *crc = cls;
181
182   stored_bytes -= crc->esize;
183   stored_entries--;
184   stored_ops++;
185   GNUNET_DATASTORE_remove (datastore,
186                            &crc->key,
187                            crc->esize,
188                            crc->data,
189                            1, 1, TIMEOUT,
190                            &remove_next,
191                            crc);
192 }
193
194
195
196 static void 
197 delete_value (void *cls,
198               const GNUNET_HashCode * key,
199               uint32_t size,
200               const void *data,
201               enum GNUNET_BLOCK_Type type,
202               uint32_t priority,
203               uint32_t anonymity,
204               struct GNUNET_TIME_Absolute
205               expiration, uint64_t uid)
206 {
207   struct CpsRunContext *crc = cls;
208
209   if (key == NULL)
210     {
211       if (stored_bytes < MAX_SIZE)
212         {
213           crc->phase = RP_REPORT;
214           GNUNET_SCHEDULER_add_continuation (crc->sched,
215                                              &run_continuation,
216                                              crc,
217                                              GNUNET_SCHEDULER_REASON_PREREQ_DONE);
218           return;     
219         }
220       GNUNET_SCHEDULER_add_with_priority (crc->sched,
221                                           GNUNET_SCHEDULER_PRIORITY_HIGH,
222                                           &do_delete,
223                                           crc);
224       return;
225     }
226   stored_ops++;
227   if (stored_bytes < MAX_SIZE)
228     {
229       GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
230       return;     
231     }
232   crc->key = *key;
233   crc->esize = size;
234   memcpy (crc->data, data, size);
235   GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
236 }
237
238
239 static void
240 run_continuation (void *cls,
241                   const struct GNUNET_SCHEDULER_TaskContext *tc)
242 {
243   struct CpsRunContext *crc = cls;
244   size_t size;
245   static GNUNET_HashCode key;
246   static char data[65536];
247   int i;
248   int k;
249
250   ok = (int) crc->phase;
251   switch (crc->phase)
252     {
253     case RP_PUT:
254       memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
255       i = crc->j;
256       k = crc->i;
257       /* most content is 32k */
258       size = 32 * 1024;
259       if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
260         size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
261       crc->size = size = size - (size & 7);     /* always multiple of 8 */
262       GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
263       memset (data, i, size);
264       if (i > 255)
265         memset (data, i - 255, size / 2);
266       data[0] = k;
267       GNUNET_DATASTORE_put (datastore,
268                             0,
269                             &key,
270                             size,
271                             data,
272                             i+1,
273                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
274                             i,
275                             GNUNET_TIME_relative_to_absolute 
276                             (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
277                                                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
278                             1, 1, TIMEOUT,
279                             &check_success, 
280                             crc);
281       break;
282     case RP_CUT:
283       /* trim down below MAX_SIZE again */
284       GNUNET_DATASTORE_get_random (datastore, 
285                                    1, 1, TIMEOUT,
286                                    &delete_value,
287                                    crc);
288       break;
289     case RP_REPORT:
290       printf (
291 #if REPORT_ID
292                "\n"
293 #endif
294                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
295                stored_bytes / 1024,     /* used size in k */
296                stored_ops / 1024,        /* total operations (in k) */
297                1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).value));
298       crc->phase = RP_PUT;
299       crc->j = 0;
300       GNUNET_SCHEDULER_add_continuation (crc->sched,
301                                          &run_continuation,
302                                          crc,
303                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
304       break;
305     case RP_DONE:
306       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
307       GNUNET_free (crc);
308       ok = 0;
309       break;
310     default:
311       GNUNET_assert (0);      
312     }
313 }
314
315
316 static void
317 run (void *cls,
318      struct GNUNET_SCHEDULER_Handle *sched,
319      char *const *args,
320      const char *cfgfile,
321      const struct GNUNET_CONFIGURATION_Handle *cfg)
322 {
323   struct CpsRunContext *crc;
324
325   datastore = GNUNET_DATASTORE_connect (cfg, sched);
326   start_time = GNUNET_TIME_absolute_get ();
327   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
328   crc->sched = sched;
329   crc->cfg = cfg;
330   crc->phase = RP_PUT;
331   GNUNET_SCHEDULER_add_continuation (crc->sched,
332                                      &run_continuation,
333                                      crc,
334                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
335 }
336
337
338 static int
339 check ()
340 {
341   pid_t pid;
342   char *const argv[] = { 
343     "perf-datastore-api",
344     "-c",
345     "test_datastore_api_data.conf",
346 #if VERBOSE
347     "-L", "DEBUG",
348 #endif
349     NULL
350   };
351   struct GNUNET_GETOPT_CommandLineOption options[] = {
352     GNUNET_GETOPT_OPTION_END
353   };
354   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
355                                  "gnunet-service-arm",
356 #if VERBOSE
357                                  "-L", "DEBUG",
358 #endif
359                                  "-c", "test_datastore_api_data.conf", NULL);
360   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
361                       argv, "perf-datastore-api", "nohelp",
362                       options, &run, NULL);
363   if (0 != PLIBC_KILL (pid, SIGTERM))
364     {
365       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
366       ok = 1;
367     }
368   GNUNET_OS_process_wait(pid);
369   return ok;
370 }
371
372
373 int
374 main (int argc, char *argv[])
375 {
376   int ret;
377
378   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
379   GNUNET_log_setup ("perf-datastore-api",
380 #if VERBOSE
381                     "DEBUG",
382 #else
383                     "WARNING",
384 #endif
385                     NULL);
386   ret = check ();
387 #if REPORT_ID
388   fprintf (stderr, "\n");
389 #endif
390   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
391   return ret;
392 }
393
394
395 /* end of perf_datastore_api.c */