typo
[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 static int
115 putValue (int i, int k)
116 {
117   size_t size;
118   static GNUNET_HashCode key;
119   static int ic;
120   static char data[65536];
121
122   /* most content is 32k */
123   size = 32 * 1024;
124   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
125     size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
126   size = size - (size & 7);     /* always multiple of 8 */
127
128   GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
129   memset (data, i, size);
130   if (i > 255)
131     memset (data, i - 255, size / 2);
132   data[0] = k;
133   GNUNET_DATASTORE_put (datastore,
134                         0,
135                         &key,
136                         size,
137                         data,
138                         i,
139                         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
140                         i,
141                         GNUNET_TIME_relative_to_absolute 
142                         (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
143                                                         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
144                         TIMEOUT,
145                         NULL, NULL);
146   ic++;
147 #if REPORT_ID
148   if (ic % REP_FREQ == 0)
149     fprintf (stderr, "I");
150 #endif
151   stored_bytes += size;
152   stored_ops++;
153   stored_entries++;
154   return GNUNET_OK;
155 }
156
157
158 static void
159 iterate_delete (void *cls,
160                 const GNUNET_HashCode * key,
161                 uint32_t size,
162                 const void *data,
163                 uint32_t type,
164                 uint32_t priority,
165                 uint32_t anonymity,
166                 struct GNUNET_TIME_Absolute
167                 expiration, uint64_t uid)
168 {
169   GNUNET_DATASTORE_remove (datastore, key, size, data, NULL, NULL);
170 }
171
172
173 enum RunPhase
174   {
175     RP_DONE = 0,
176     RP_PUT,
177     RP_CUT,
178     RP_REPORT,
179     RP_END
180   };
181
182
183 struct CpsRunContext
184 {
185   struct GNUNET_SCHEDULER_Handle *sched;
186   struct GNUNET_CONFIGURATION_Handle *cfg;
187   enum RunPhase phase;
188   int j;
189   unsigned long long size;
190   int i;
191
192
193   GNUNET_HashCode key;
194   int *iptr;
195 };
196
197
198
199 static void
200 run_continuation (void *cls,
201                   const struct GNUNET_SCHEDULER_TaskContext *tc);
202
203
204
205 static void
206 run_continuation (void *cls,
207                   const struct GNUNET_SCHEDULER_TaskContext *tc)
208 {
209   struct CpsRunContext *crc = cls;
210   ok = (int) crc->phase;
211   switch (crc->phase)
212     {
213     case RP_PUT:
214       memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
215
216       GNUNET_assert (GNUNET_OK == putValue (j, i));
217       GNUNET_DATASTORE_put (datastore,
218                             0,
219                             &crc->key,
220                             get_size (crc->i),
221                             get_data (crc->i),
222                             get_type (crc->i),
223                             get_priority (crc->i),
224                             get_anonymity (crc->i),
225                             get_expiration (crc->i),
226                             TIMEOUT,
227                             &check_success,
228                             crc);
229       crc->j++;
230       if (crc->j < PUT_10)
231         break;
232       crc->j = 0;
233       crc->i++;
234       if (crc->i == ITERATIONS)
235         crc->phase = RP_DONE;
236       else
237         crc->phase = RP_CUT;
238       break;
239     case RP_CUT:
240       /* trim down below MAX_SIZE again */
241       if ((i % 2) == 0)
242         GNUNET_DATASTORE_get_random (datastore, 
243                                      &iterate_delete,
244                                      NULL);
245       crc->phase = RP_REPORT;
246       break;
247     case RP_REPORT:
248       size = 0;
249       printf (
250 #if REPORT_ID
251                "\n"
252 #endif
253                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
254                stored_bytes / 1024,     /* used size in k */
255                (stored_ops * 2 - stored_entries) / 1024,        /* total operations (in k) */
256                1000 * (stored_ops * 2 - stored_entries) / (1 + GNUNET_TIME_absolute_get_duration(start_time).value));       /* operations per second */
257       crc->phase = RP_PUT;
258       // fixme: trigger next round...
259       GNUNET_SCHEDULER_add_continuation (crc->sched,
260                                          GNUNET_NO,
261                                          &run_continuation,
262                                          crc,
263                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
264       break;
265     case RP_DONE:
266       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
267       ok = 0;
268       break;
269     }
270 }
271
272
273 static void
274 run (void *cls,
275      struct GNUNET_SCHEDULER_Handle *sched,
276      char *const *args,
277      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
278 {
279   struct CpsRunContext *crc;
280
281   datastore = GNUNET_DATASTORE_connect (cfg, sched);
282
283   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
284   crc->sched = sched;
285   crc->cfg = cfg;
286   crc->phase = RP_PUT;
287   GNUNET_SCHEDULER_add_continuation (crc->sched,
288                                      GNUNET_NO,
289                                      &run_continuation,
290                                      crc,
291                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
292 }
293
294
295 static int
296 check ()
297 {
298   pid_t pid;
299   char *const argv[] = { "perf-datastore-api",
300     "-c",
301     "test_datastore_api_data.conf",
302 #if VERBOSE
303     "-L", "DEBUG",
304 #endif
305     NULL
306   };
307   struct GNUNET_GETOPT_CommandLineOption options[] = {
308     GNUNET_GETOPT_OPTION_END
309   };
310   pid = GNUNET_OS_start_process ("gnunet-service-datastore",
311                                  "gnunet-service-datastore",
312 #if VERBOSE
313                                  "-L", "DEBUG",
314 #endif
315                                  "-c", "test_datastore_api_data.conf", NULL);
316   sleep (1);
317   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
318                       argv, "perf-datastore-api", "nohelp",
319                       options, &run, NULL);
320   if (0 != PLIBC_KILL (pid, SIGTERM))
321     {
322       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
323       ok = 1;
324     }
325   GNUNET_OS_process_wait(pid);
326   if (ok != 0)
327     fprintf (stderr, "Missed some testcases: %u\n", ok);
328   return ok;
329 }
330
331
332 int
333 main (int argc, char *argv[])
334 {
335   int ret;
336
337   GNUNET_log_setup ("perf-datastore-api",
338 #if VERBOSE
339                     "DEBUG",
340 #else
341                     "WARNING",
342 #endif
343                     NULL);
344   ret = check ();
345
346   return ret;
347 }
348
349
350 /* end of perf_datastore_api.c */