2344dafe89e23d28438c6aef3fd5f0e38ebfce66
[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                                      GNUNET_NO,
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                                      GNUNET_NO,
171                                      &run_continuation,
172                                      crc,
173                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
174 }
175
176
177
178 static void
179 do_delete (void *cls,
180            const struct GNUNET_SCHEDULER_TaskContext *tc)
181 {
182   struct CpsRunContext *crc = cls;
183
184   stored_bytes -= crc->esize;
185   stored_entries--;
186   stored_ops++;
187   GNUNET_DATASTORE_remove (datastore,
188                            &crc->key,
189                            crc->esize,
190                            crc->data,
191                            &remove_next,
192                            crc,
193                            TIMEOUT);
194 }
195
196
197
198 static void 
199 delete_value (void *cls,
200               const GNUNET_HashCode * key,
201               uint32_t size,
202               const void *data,
203               uint32_t type,
204               uint32_t priority,
205               uint32_t anonymity,
206               struct GNUNET_TIME_Absolute
207               expiration, uint64_t uid)
208 {
209   struct CpsRunContext *crc = cls;
210
211   if (key == NULL)
212     {
213       if (stored_bytes < MAX_SIZE)
214         {
215           crc->phase = RP_REPORT;
216           GNUNET_SCHEDULER_add_continuation (crc->sched,
217                                              GNUNET_NO,
218                                              &run_continuation,
219                                              crc,
220                                              GNUNET_SCHEDULER_REASON_PREREQ_DONE);
221           return;     
222         }
223       GNUNET_SCHEDULER_add_after (crc->sched,
224                                   GNUNET_NO,
225                                   GNUNET_SCHEDULER_PRIORITY_HIGH,
226                                   GNUNET_SCHEDULER_NO_TASK,
227                                   &do_delete,
228                                   crc);
229       return;
230     }
231   stored_ops++;
232   if (stored_bytes < MAX_SIZE)
233     {
234       GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
235       return;     
236     }
237   crc->key = *key;
238   crc->esize = size;
239   memcpy (crc->data, data, size);
240   GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
241 }
242
243
244 static void
245 run_continuation (void *cls,
246                   const struct GNUNET_SCHEDULER_TaskContext *tc)
247 {
248   struct CpsRunContext *crc = cls;
249   size_t size;
250   static GNUNET_HashCode key;
251   static char data[65536];
252   int i;
253   int k;
254
255   ok = (int) crc->phase;
256   switch (crc->phase)
257     {
258     case RP_PUT:
259       memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
260       i = crc->j;
261       k = crc->i;
262       /* most content is 32k */
263       size = 32 * 1024;
264       if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
265         size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
266       crc->size = size = size - (size & 7);     /* always multiple of 8 */
267       GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
268       memset (data, i, size);
269       if (i > 255)
270         memset (data, i - 255, size / 2);
271       data[0] = k;
272       GNUNET_DATASTORE_put (datastore,
273                             0,
274                             &key,
275                             size,
276                             data,
277                             i+1,
278                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
279                             i,
280                             GNUNET_TIME_relative_to_absolute 
281                             (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
282                                                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
283                             TIMEOUT,
284                             &check_success, 
285                             crc);
286       break;
287     case RP_CUT:
288       /* trim down below MAX_SIZE again */
289       GNUNET_DATASTORE_get_random (datastore, 
290                                    &delete_value,
291                                    crc,
292                                    TIMEOUT);
293       break;
294     case RP_REPORT:
295       printf (
296 #if REPORT_ID
297                "\n"
298 #endif
299                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
300                stored_bytes / 1024,     /* used size in k */
301                stored_ops / 1024,        /* total operations (in k) */
302                1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).value));
303       crc->phase = RP_PUT;
304       crc->j = 0;
305       GNUNET_SCHEDULER_add_continuation (crc->sched,
306                                          GNUNET_NO,
307                                          &run_continuation,
308                                          crc,
309                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
310       break;
311     case RP_DONE:
312       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
313       GNUNET_free (crc);
314       ok = 0;
315       break;
316     default:
317       GNUNET_assert (0);      
318     }
319 }
320
321
322 static void
323 run (void *cls,
324      struct GNUNET_SCHEDULER_Handle *sched,
325      char *const *args,
326      const char *cfgfile,
327      const struct GNUNET_CONFIGURATION_Handle *cfg)
328 {
329   struct CpsRunContext *crc;
330
331   datastore = GNUNET_DATASTORE_connect (cfg, sched);
332   start_time = GNUNET_TIME_absolute_get ();
333   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
334   crc->sched = sched;
335   crc->cfg = cfg;
336   crc->phase = RP_PUT;
337   GNUNET_SCHEDULER_add_continuation (crc->sched,
338                                      GNUNET_NO,
339                                      &run_continuation,
340                                      crc,
341                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
342 }
343
344
345 static int
346 check ()
347 {
348   pid_t pid;
349   char *const argv[] = { 
350     "perf-datastore-api",
351     "-c",
352     "test_datastore_api_data.conf",
353 #if VERBOSE
354     "-L", "DEBUG",
355 #endif
356     NULL
357   };
358   struct GNUNET_GETOPT_CommandLineOption options[] = {
359     GNUNET_GETOPT_OPTION_END
360   };
361   pid = GNUNET_OS_start_process ("gnunet-service-arm",
362                                  "gnunet-service-arm",
363 #if VERBOSE
364                                  "-L", "DEBUG",
365 #endif
366                                  "-c", "test_datastore_api_data.conf", NULL);
367   sleep (1);
368   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
369                       argv, "perf-datastore-api", "nohelp",
370                       options, &run, NULL);
371   if (0 != PLIBC_KILL (pid, SIGTERM))
372     {
373       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
374       ok = 1;
375     }
376   GNUNET_OS_process_wait(pid);
377   return ok;
378 }
379
380
381 int
382 main (int argc, char *argv[])
383 {
384   int ret;
385
386   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
387   GNUNET_log_setup ("perf-datastore-api",
388 #if VERBOSE
389                     "DEBUG",
390 #else
391                     "WARNING",
392 #endif
393                     NULL);
394   ret = check ();
395 #if REPORT_ID
396   fprintf (stderr, "\n");
397 #endif
398   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
399   return ret;
400 }
401
402
403 /* end of perf_datastore_api.c */