better
[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 static struct GNUNET_DATASTORE_Handle *datastore;
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  * Target datastore size (in bytes).
49  * <p>
50  * Example impact of total size on the reported number
51  * of operations (insert and delete) per second (once
52  * roughly stabilized -- this is not "sound" experimental
53  * data but just a rough idea) for a particular machine:
54  * <pre>
55  *    4: 60   at   7k ops total
56  *    8: 50   at   3k ops total
57  *   16: 48   at   8k ops total
58  *   32: 46   at   8k ops total
59  *   64: 61   at   9k ops total
60  *  128: 89   at   9k ops total
61  * 4092: 11   at 383k ops total (12 GB stored, 14.8 GB DB size on disk, 2.5 GB reported)
62  * </pre>
63  * Pure insertion performance into an empty DB initially peaks
64  * at about 400 ops.  The performance seems to drop especially
65  * once the existing (fragmented) ISAM space is filled up and
66  * the DB needs to grow on disk.  This could be explained with
67  * ISAM looking more carefully for defragmentation opportunities.
68  * <p>
69  * MySQL disk space overheads (for otherwise unused database when
70  * run with 128 MB target data size; actual size 651 MB, useful
71  * data stored 520 MB) are quite large in the range of 25-30%.
72  * <p>
73  * This kind of processing seems to be IO bound (system is roughly
74  * at 90% wait, 10% CPU).  This is with MySQL 5.0.
75  *
76  */
77 #define MAX_SIZE 1024LL * 1024 * 16
78
79 /**
80  * Report progress outside of major reports? Should probably be GNUNET_YES if
81  * size is > 16 MB.
82  */
83 #define REPORT_ID GNUNET_NO
84
85 /**
86  * Number of put operations equivalent to 1/10th of MAX_SIZE
87  */
88 #define PUT_10 MAX_SIZE / 32 / 1024 / 10
89
90 /**
91  * Progress report frequency.  1/10th of a put operation block.
92  */
93 #define REP_FREQ PUT_10 / 10
94
95 /**
96  * Total number of iterations (each iteration doing
97  * PUT_10 put operations); we report full status every
98  * 10 iterations.  Abort with CTRL-C.
99  */
100 #define ITERATIONS 100
101
102
103 static unsigned long long stored_bytes;
104
105 static unsigned long long stored_entries;
106
107 static unsigned long long stored_ops;
108
109 static struct GNUNET_TIME_Absolute start_time;
110
111 static int ok;
112
113 enum RunPhase
114   {
115     RP_DONE = 0,
116     RP_PUT,
117     RP_CUT,
118     RP_REPORT
119   };
120
121
122 struct CpsRunContext
123 {
124   struct GNUNET_SCHEDULER_Handle *sched;
125   struct GNUNET_CONFIGURATION_Handle *cfg;
126   enum RunPhase phase;
127   int j;
128   unsigned long long size;
129   int i;
130
131   GNUNET_HashCode key;
132   uint32_t esize;
133   char data[65536];
134 };
135
136
137
138 static void
139 run_continuation (void *cls,
140                   const struct GNUNET_SCHEDULER_TaskContext *tc);
141
142
143
144
145 static void
146 check_success (void *cls,
147                int success,
148                const char *msg)
149 {
150   static int ic;
151
152   struct CpsRunContext *crc = cls;
153   if (GNUNET_OK != success)
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155                 "%s\n", msg);
156   GNUNET_assert (GNUNET_OK == success);
157   ic++;
158 #if REPORT_ID
159   if (ic % REP_FREQ == 0)
160     fprintf (stderr, "I");
161 #endif
162   stored_bytes += crc->size;
163   stored_ops++;
164   stored_entries++;
165   crc->j++;
166   if (crc->j == PUT_10)
167     {
168       crc->j = 0;
169       crc->i++;
170       if (crc->i == ITERATIONS)
171         crc->phase = RP_DONE;
172       else
173         crc->phase = RP_CUT;
174     }
175   GNUNET_SCHEDULER_add_continuation (crc->sched,
176                                      GNUNET_NO,
177                                      &run_continuation,
178                                      crc,
179                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
180 }
181
182
183 /**
184  * Continuation called to notify client about result of the
185  * operation.
186  *
187  * @param cls closure
188  * @param success GNUNET_SYSERR on failure
189  * @param msg NULL on success, otherwise an error message
190  */
191 static void 
192 remove_next(void *cls,
193             int success,
194             const char *msg)
195 {
196   static int dc;
197   dc++;
198 #if REPORT_ID
199   if (dc % REP_FREQ == 0)
200     fprintf (stderr, "D");
201 #endif
202   GNUNET_assert (GNUNET_OK == success);
203 }
204
205
206
207 static void
208 do_delete (void *cls,
209            const struct GNUNET_SCHEDULER_TaskContext *tc)
210 {
211   struct CpsRunContext *crc = cls;
212
213   GNUNET_DATASTORE_remove (datastore,
214                            &crc->key,
215                            crc->esize,
216                            crc->data,
217                            &remove_next,
218                            crc);
219 }
220
221
222
223 static void 
224 delete_value (void *cls,
225               const GNUNET_HashCode * key,
226               uint32_t size,
227               const void *data,
228               uint32_t type,
229               uint32_t priority,
230               uint32_t anonymity,
231               struct GNUNET_TIME_Absolute
232               expiration, uint64_t uid)
233 {
234   struct CpsRunContext *crc = cls;
235
236   if (stored_bytes < MAX_SIZE)
237     return;     
238   if (key == NULL)
239     {
240       crc->phase = RP_REPORT;
241       GNUNET_SCHEDULER_add_continuation (crc->sched,
242                                          GNUNET_NO,
243                                          &run_continuation,
244                                          crc,
245                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
246       return;
247     }
248   stored_bytes -= size;
249   stored_entries--;
250   crc->key = *key;
251   crc->esize = size;
252   memcpy (crc->data, data, size);
253   GNUNET_SCHEDULER_add_after (crc->sched,
254                               GNUNET_NO,
255                               GNUNET_SCHEDULER_PRIORITY_HIGH,
256                               GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
257                               &do_delete,
258                               crc);
259 }
260
261
262 static void
263 run_continuation (void *cls,
264                   const struct GNUNET_SCHEDULER_TaskContext *tc)
265 {
266   struct CpsRunContext *crc = cls;
267   size_t size;
268   static GNUNET_HashCode key;
269   static char data[65536];
270   int i;
271   int k;
272
273   ok = (int) crc->phase;
274   switch (crc->phase)
275     {
276     case RP_PUT:
277       memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
278       i = crc->j;
279       k = crc->i;
280       /* most content is 32k */
281       size = 32 * 1024;
282       if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
283         size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
284       crc->size = size = size - (size & 7);     /* always multiple of 8 */
285       GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
286       memset (data, i, size);
287       if (i > 255)
288         memset (data, i - 255, size / 2);
289       data[0] = k;
290       GNUNET_DATASTORE_put (datastore,
291                             0,
292                             &key,
293                             size,
294                             data,
295                             i,
296                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
297                             i,
298                             GNUNET_TIME_relative_to_absolute 
299                             (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
300                                                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
301                             TIMEOUT,
302                             &check_success, 
303                             crc);
304       break;
305     case RP_CUT:
306       /* trim down below MAX_SIZE again */
307       GNUNET_DATASTORE_get_random (datastore, 
308                                    &delete_value,
309                                    crc);
310       break;
311     case RP_REPORT:
312       size = 0;
313       printf (
314 #if REPORT_ID
315                "\n"
316 #endif
317                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
318                stored_bytes / 1024,     /* used size in k */
319                (stored_ops * 2 - stored_entries) / 1024,        /* total operations (in k) */
320                1000 * (stored_ops * 2 - stored_entries) / (1 + GNUNET_TIME_absolute_get_duration(start_time).value));       /* operations per second */
321       crc->phase = RP_PUT;
322       GNUNET_SCHEDULER_add_continuation (crc->sched,
323                                          GNUNET_NO,
324                                          &run_continuation,
325                                          crc,
326                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
327       break;
328     case RP_DONE:
329       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
330       ok = 0;
331       break;
332     }
333 }
334
335
336 static void
337 run (void *cls,
338      struct GNUNET_SCHEDULER_Handle *sched,
339      char *const *args,
340      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
341 {
342   struct CpsRunContext *crc;
343
344   datastore = GNUNET_DATASTORE_connect (cfg, sched);
345
346   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
347   crc->sched = sched;
348   crc->cfg = cfg;
349   crc->phase = RP_PUT;
350   GNUNET_SCHEDULER_add_continuation (crc->sched,
351                                      GNUNET_NO,
352                                      &run_continuation,
353                                      crc,
354                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
355 }
356
357
358 static int
359 check ()
360 {
361   pid_t pid;
362   char *const argv[] = { 
363     "perf-datastore-api",
364     "-c",
365     "test_datastore_api_data.conf",
366 #if VERBOSE
367     "-L", "DEBUG",
368 #endif
369     NULL
370   };
371   struct GNUNET_GETOPT_CommandLineOption options[] = {
372     GNUNET_GETOPT_OPTION_END
373   };
374   pid = GNUNET_OS_start_process ("gnunet-service-datastore",
375                                  "gnunet-service-datastore",
376 #if VERBOSE
377                                  "-L", "DEBUG",
378 #endif
379                                  "-c", "test_datastore_api_data.conf", NULL);
380   sleep (1);
381   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
382                       argv, "perf-datastore-api", "nohelp",
383                       options, &run, NULL);
384   if (0 != PLIBC_KILL (pid, SIGTERM))
385     {
386       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
387       ok = 1;
388     }
389   GNUNET_OS_process_wait(pid);
390   return ok;
391 }
392
393
394 int
395 main (int argc, char *argv[])
396 {
397   int ret;
398
399   GNUNET_log_setup ("perf-datastore-api",
400 #if VERBOSE
401                     "DEBUG",
402 #else
403                     "WARNING",
404 #endif
405                     NULL);
406   ret = check ();
407
408   return ret;
409 }
410
411
412 /* end of perf_datastore_api.c */