quota management and better name for NO_TASK'
[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   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     return;     
234   crc->key = *key;
235   crc->esize = size;
236   memcpy (crc->data, data, size);
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                             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                                    &delete_value,
287                                    crc,
288                                    TIMEOUT);
289       break;
290     case RP_REPORT:
291       size = 0;
292       printf (
293 #if REPORT_ID
294                "\n"
295 #endif
296                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
297                stored_bytes / 1024,     /* used size in k */
298                stored_ops / 1024,        /* total operations (in k) */
299                1000 * stored_ops / (1 + GNUNET_TIME_absolute_get_duration(start_time).value));
300       crc->phase = RP_PUT;
301       crc->j = 0;
302       GNUNET_SCHEDULER_add_continuation (crc->sched,
303                                          GNUNET_NO,
304                                          &run_continuation,
305                                          crc,
306                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
307       break;
308     case RP_DONE:
309       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
310       GNUNET_free (crc);
311       ok = 0;
312       break;
313     }
314 }
315
316
317 static void
318 run (void *cls,
319      struct GNUNET_SCHEDULER_Handle *sched,
320      char *const *args,
321      const char *cfgfile, 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                                      GNUNET_NO,
333                                      &run_continuation,
334                                      crc,
335                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
336 }
337
338
339 static int
340 check ()
341 {
342   pid_t pid;
343   char *const argv[] = { 
344     "perf-datastore-api",
345     "-c",
346     "test_datastore_api_data.conf",
347 #if VERBOSE
348     "-L", "DEBUG",
349 #endif
350     NULL
351   };
352   struct GNUNET_GETOPT_CommandLineOption options[] = {
353     GNUNET_GETOPT_OPTION_END
354   };
355   pid = GNUNET_OS_start_process ("gnunet-service-datastore",
356                                  "gnunet-service-datastore",
357 #if VERBOSE
358                                  "-L", "DEBUG",
359 #endif
360                                  "-c", "test_datastore_api_data.conf", NULL);
361   sleep (1);
362   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
363                       argv, "perf-datastore-api", "nohelp",
364                       options, &run, NULL);
365   if (0 != PLIBC_KILL (pid, SIGTERM))
366     {
367       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
368       ok = 1;
369     }
370   GNUNET_OS_process_wait(pid);
371   return ok;
372 }
373
374
375 int
376 main (int argc, char *argv[])
377 {
378   int ret;
379
380   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
381   GNUNET_log_setup ("perf-datastore-api",
382 #if VERBOSE
383                     "DEBUG",
384 #else
385                     "WARNING",
386 #endif
387                     NULL);
388   ret = check ();
389 #if REPORT_ID
390   fprintf (stderr, "\n");
391 #endif
392   return ret;
393 }
394
395
396 /* end of perf_datastore_api.c */