sthuff
[oweals/gnunet.git] / src / datastore / perf_datastore_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007 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 deletes
26  * data until the (reported) database size drops below a given threshold.
27  * This is iterated 10 times, with the actual size of the content stored,
28  * the database size reported and the file size on disk being printed for
29  * 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 alternates between "lowest priority" and "earliest expiration".
32  * Priorities and expiration dates are set using a pseudo-random value
33  * within a realistic range.
34  */
35
36 #include "platform.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_datastore_service.h"
40
41 static struct GNUNET_DATASTORE_Handle *datastore;
42
43 /**
44  * How long until we give up on transmitting the message?
45  */
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
47
48 /**
49  * Target datastore size (in bytes).
50  * <p>
51  * Example impact of total size on the reported number
52  * of operations (insert and delete) per second (once
53  * roughly stabilized -- this is not "sound" experimental
54  * data but just a rough idea) for a particular machine:
55  * <pre>
56  *    4: 60   at   7k ops total
57  *    8: 50   at   3k ops total
58  *   16: 48   at   8k ops total
59  *   32: 46   at   8k ops total
60  *   64: 61   at   9k ops total
61  *  128: 89   at   9k ops total
62  * 4092: 11   at 383k ops total (12 GB stored, 14.8 GB DB size on disk, 2.5 GB reported)
63  * </pre>
64  * Pure insertion performance into an empty DB initially peaks
65  * at about 400 ops.  The performance seems to drop especially
66  * once the existing (fragmented) ISAM space is filled up and
67  * the DB needs to grow on disk.  This could be explained with
68  * ISAM looking more carefully for defragmentation opportunities.
69  * <p>
70  * MySQL disk space overheads (for otherwise unused database when
71  * run with 128 MB target data size; actual size 651 MB, useful
72  * data stored 520 MB) are quite large in the range of 25-30%.
73  * <p>
74  * This kind of processing seems to be IO bound (system is roughly
75  * at 90% wait, 10% CPU).  This is with MySQL 5.0.
76  *
77  */
78 #define MAX_SIZE 1024LL * 1024 * 16
79
80 /**
81  * Report progress outside of major reports? Should probably be GNUNET_YES if
82  * size is > 16 MB.
83  */
84 #define REPORT_ID GNUNET_NO
85
86 /**
87  * Number of put operations equivalent to 1/10th of MAX_SIZE
88  */
89 #define PUT_10 MAX_SIZE / 32 / 1024 / 10
90
91 /**
92  * Progress report frequency.  1/10th of a put operation block.
93  */
94 #define REP_FREQ PUT_10 / 10
95
96 /**
97  * Total number of iterations (each iteration doing
98  * PUT_10 put operations); we report full status every
99  * 10 iterations.  Abort with CTRL-C.
100  */
101 #define ITERATIONS 100
102
103
104 static unsigned long long stored_bytes;
105
106 static unsigned long long stored_entries;
107
108 static unsigned long long stored_ops;
109
110 static struct GNUNET_TIME_Absolute start_time;
111
112 static int ok;
113
114 enum RunPhase
115   {
116     RP_DONE = 0,
117     RP_PUT,
118     RP_CUT,
119     RP_REPORT
120   };
121
122
123 struct CpsRunContext
124 {
125   struct GNUNET_SCHEDULER_Handle *sched;
126   struct GNUNET_CONFIGURATION_Handle *cfg;
127   enum RunPhase phase;
128   int j;
129   unsigned long long size;
130   int i;
131 };
132
133
134
135 static void
136 run_continuation (void *cls,
137                   const struct GNUNET_SCHEDULER_TaskContext *tc);
138
139
140
141
142 static void
143 check_success (void *cls,
144                int success,
145                const char *msg)
146 {
147   static int ic;
148
149   struct CpsRunContext *crc = cls;
150   if (GNUNET_OK != success)
151     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152                 "%s\n", msg);
153   GNUNET_assert (GNUNET_OK == success);
154   ic++;
155 #if REPORT_ID
156   if (ic % REP_FREQ == 0)
157     fprintf (stderr, "I");
158 #endif
159   stored_bytes += crc->size;
160   stored_ops++;
161   stored_entries++;
162   crc->j++;
163   if (crc->j == PUT_10)
164     {
165       crc->j = 0;
166       crc->i++;
167       if (crc->i == ITERATIONS)
168         crc->phase = RP_DONE;
169       else
170         crc->phase = RP_CUT;
171     }
172   GNUNET_SCHEDULER_add_continuation (crc->sched,
173                                      GNUNET_NO,
174                                      &run_continuation,
175                                      crc,
176                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
177 }
178
179
180 /**
181  * Continuation called to notify client about result of the
182  * operation.
183  *
184  * @param cls closure
185  * @param success GNUNET_SYSERR on failure
186  * @param msg NULL on success, otherwise an error message
187  */
188 static void 
189 remove_next(void *cls,
190             int success,
191             const char *msg)
192 {
193   GNUNET_assert (GNUNET_OK == success);
194 }
195
196
197 static void 
198 delete_value (void *cls,
199              const GNUNET_HashCode * key,
200              uint32_t size,
201              const void *data,
202              uint32_t 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   /* FIXME: should probably abort deletion iteration
211      earlier (to not delete everything...) */
212   if (key == NULL)
213     {
214       crc->phase = RP_REPORT;
215       /* FIXME: should wait for all "remove_next" calls
216          to complete before going back to the run_continuation */
217       GNUNET_SCHEDULER_add_continuation (crc->sched,
218                                          GNUNET_NO,
219                                          &run_continuation,
220                                          crc,
221                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
222       return;
223     }
224   GNUNET_DATASTORE_remove (datastore,
225                            key,
226                            size,
227                            data,
228                            &remove_next,
229                            crc);
230 }
231
232
233
234 static void
235 run_continuation (void *cls,
236                   const struct GNUNET_SCHEDULER_TaskContext *tc)
237 {
238   struct CpsRunContext *crc = cls;
239   size_t size;
240   static GNUNET_HashCode key;
241   static char data[65536];
242   int i;
243   int k;
244
245   ok = (int) crc->phase;
246   switch (crc->phase)
247     {
248     case RP_PUT:
249       memset (&key, 256 - crc->i, sizeof (GNUNET_HashCode));
250       i = crc->j;
251       k = crc->i;
252       /* most content is 32k */
253       size = 32 * 1024;
254       if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
255         size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
256       crc->size = size = size - (size & 7);     /* always multiple of 8 */
257       GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
258       memset (data, i, size);
259       if (i > 255)
260         memset (data, i - 255, size / 2);
261       data[0] = k;
262       GNUNET_DATASTORE_put (datastore,
263                             0,
264                             &key,
265                             size,
266                             data,
267                             i,
268                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
269                             i,
270                             GNUNET_TIME_relative_to_absolute 
271                             (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
272                                                             GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
273                             TIMEOUT,
274                             &check_success, 
275                             crc);
276       break;
277     case RP_CUT:
278       /* trim down below MAX_SIZE again */
279       if ((i % 2) == 0)
280         {
281           GNUNET_DATASTORE_get_random (datastore, 
282                                        &delete_value,
283                                        crc);
284           break;
285         }
286       crc->phase = RP_REPORT;
287       GNUNET_SCHEDULER_add_continuation (crc->sched,
288                                          GNUNET_NO,
289                                          &run_continuation,
290                                          crc,
291                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
292       break;
293     case RP_REPORT:
294       size = 0;
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 * 2 - stored_entries) / 1024,        /* total operations (in k) */
302                1000 * (stored_ops * 2 - stored_entries) / (1 + GNUNET_TIME_absolute_get_duration(start_time).value));       /* operations per second */
303       crc->phase = RP_PUT;
304       GNUNET_SCHEDULER_add_continuation (crc->sched,
305                                          GNUNET_NO,
306                                          &run_continuation,
307                                          crc,
308                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
309       break;
310     case RP_DONE:
311       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
312       ok = 0;
313       break;
314     }
315 }
316
317
318 static void
319 run (void *cls,
320      struct GNUNET_SCHEDULER_Handle *sched,
321      char *const *args,
322      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
323 {
324   struct CpsRunContext *crc;
325
326   datastore = GNUNET_DATASTORE_connect (cfg, sched);
327
328   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
329   crc->sched = sched;
330   crc->cfg = cfg;
331   crc->phase = RP_PUT;
332   GNUNET_SCHEDULER_add_continuation (crc->sched,
333                                      GNUNET_NO,
334                                      &run_continuation,
335                                      crc,
336                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
337 }
338
339
340 static int
341 check ()
342 {
343   pid_t pid;
344   char *const argv[] = { "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   if (ok != 0)
372     fprintf (stderr, "Missed some testcases: %u\n", ok);
373   return ok;
374 }
375
376
377 int
378 main (int argc, char *argv[])
379 {
380   int ret;
381
382   GNUNET_log_setup ("perf-datastore-api",
383 #if VERBOSE
384                     "DEBUG",
385 #else
386                     "WARNING",
387 #endif
388                     NULL);
389   ret = check ();
390
391   return ret;
392 }
393
394
395 /* end of perf_datastore_api.c */