airplane hackery
[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  * Target datastore size (in bytes).
45  * <p>
46  * Example impact of total size on the reported number
47  * of operations (insert and delete) per second (once
48  * roughly stabilized -- this is not "sound" experimental
49  * data but just a rough idea) for a particular machine:
50  * <pre>
51  *    4: 60   at   7k ops total
52  *    8: 50   at   3k ops total
53  *   16: 48   at   8k ops total
54  *   32: 46   at   8k ops total
55  *   64: 61   at   9k ops total
56  *  128: 89   at   9k ops total
57  * 4092: 11   at 383k ops total (12 GB stored, 14.8 GB DB size on disk, 2.5 GB reported)
58  * </pre>
59  * Pure insertion performance into an empty DB initially peaks
60  * at about 400 ops.  The performance seems to drop especially
61  * once the existing (fragmented) ISAM space is filled up and
62  * the DB needs to grow on disk.  This could be explained with
63  * ISAM looking more carefully for defragmentation opportunities.
64  * <p>
65  * MySQL disk space overheads (for otherwise unused database when
66  * run with 128 MB target data size; actual size 651 MB, useful
67  * data stored 520 MB) are quite large in the range of 25-30%.
68  * <p>
69  * This kind of processing seems to be IO bound (system is roughly
70  * at 90% wait, 10% CPU).  This is with MySQL 5.0.
71  *
72  */
73 #define MAX_SIZE 1024LL * 1024 * 16
74
75 /**
76  * Report progress outside of major reports? Should probably be GNUNET_YES if
77  * size is > 16 MB.
78  */
79 #define REPORT_ID GNUNET_NO
80
81 /**
82  * Number of put operations equivalent to 1/10th of MAX_SIZE
83  */
84 #define PUT_10 MAX_SIZE / 32 / 1024 / 10
85
86 /**
87  * Progress report frequency.  1/10th of a put operation block.
88  */
89 #define REP_FREQ PUT_10 / 10
90
91 /**
92  * Total number of iterations (each iteration doing
93  * PUT_10 put operations); we report full status every
94  * 10 iterations.  Abort with CTRL-C.
95  */
96 #define ITERATIONS 100
97
98
99 static unsigned long long stored_bytes;
100
101 static unsigned long long stored_entries;
102
103 static unsigned long long stored_ops;
104
105 static struct GNUNET_TIME_Absolute start_time;
106
107 static int ok;
108
109
110 static int
111 putValue (int i, int k)
112 {
113   size_t size;
114   static GNUNET_HashCode key;
115   static int ic;
116   static char data[65536];
117
118   /* most content is 32k */
119   size = 32 * 1024;
120   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
121     size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
122   size = size - (size & 7);     /* always multiple of 8 */
123
124   GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
125   memset (data, i, size);
126   if (i > 255)
127     memset (data, i - 255, size / 2);
128   data[0] = k;
129   GNUNET_DATASTORE_put (datastore,
130                         0,
131                         &key,
132                         size,
133                         data,
134                         i,
135                         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100),
136                         i,
137                         GNUNET_TIME_relative_to_absolute 
138                         (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
139                                                         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
140                         NULL, NULL);
141   ic++;
142 #if REPORT_ID
143   if (ic % REP_FREQ == 0)
144     fprintf (stderr, "I");
145 #endif
146   stored_bytes += size;
147   stored_ops++;
148   stored_entries++;
149   return GNUNET_OK;
150 }
151
152
153 static void
154 iterate_delete (void *cls,
155                 const GNUNET_HashCode * key,
156                 uint32_t size,
157                 const void *data,
158                 uint32_t type,
159                 uint32_t priority,
160                 uint32_t anonymity,
161                 struct GNUNET_TIME_Absolute
162                 expiration, uint64_t uid)
163 {
164   GNUNET_DATASTORE_remove (datastore, key, size, data, NULL, NULL);
165 }
166
167
168
169 static void
170 run (void *cls,
171      struct GNUNET_SCHEDULER_Handle *sched,
172      char *const *args,
173      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
174 {
175   int j;
176   unsigned long long size;
177   int i;
178
179   datastore = GNUNET_DATASTORE_connect (cfg, sched);
180   /* FIXME: change loop to use CPS; current
181      datastore API will likely react negative to
182      us ignoring the callbacks... */
183   for (i = 0; i < ITERATIONS; i++)
184     {
185 #if REPORT_ID
186       fprintf (stderr, ".");
187 #endif
188       /* insert data equivalent to 1/10th of MAX_SIZE */
189       for (j = 0; j < PUT_10; j++)
190         GNUNET_assert (GNUNET_OK == putValue (j, i));
191
192       /* trim down below MAX_SIZE again */
193       if ((i % 2) == 0)
194         GNUNET_DATASTORE_get_random (datastore, &iterate_delete, NULL);
195       size = 0;
196       printf (
197 #if REPORT_ID
198                "\n"
199 #endif
200                "Stored %llu kB / %lluk ops / %llu ops/s\n", 
201                stored_bytes / 1024,     /* used size in k */
202                (stored_ops * 2 - stored_entries) / 1024,        /* total operations (in k) */
203                1000 * (stored_ops * 2 - stored_entries) / (1 + GNUNET_TIME_absolute_get_duration(start_time).value));       /* operations per second */
204     }
205   GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
206 }
207
208
209 static int
210 check ()
211 {
212   pid_t pid;
213   char *const argv[] = { "perf-datastore-api",
214     "-c",
215     "test_datastore_api_data.conf",
216 #if VERBOSE
217     "-L", "DEBUG",
218 #endif
219     NULL
220   };
221   struct GNUNET_GETOPT_CommandLineOption options[] = {
222     GNUNET_GETOPT_OPTION_END
223   };
224   pid = GNUNET_OS_start_process ("gnunet-service-datastore",
225                                  "gnunet-service-datastore",
226 #if VERBOSE
227                                  "-L", "DEBUG",
228 #endif
229                                  "-c", "test_datastore_api_data.conf", NULL);
230   sleep (1);
231   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
232                       argv, "perf-datastore-api", "nohelp",
233                       options, &run, NULL);
234   if (0 != PLIBC_KILL (pid, SIGTERM))
235     {
236       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
237       ok = 1;
238     }
239   GNUNET_OS_process_wait(pid);
240   if (ok != 0)
241     fprintf (stderr, "Missed some testcases: %u\n", ok);
242   return ok;
243 }
244
245
246 int
247 main (int argc, char *argv[])
248 {
249   int ret;
250
251   GNUNET_log_setup ("perf-datastore-api",
252 #if VERBOSE
253                     "DEBUG",
254 #else
255                     "WARNING",
256 #endif
257                     NULL);
258   ret = check ();
259
260   return ret;
261 }
262
263
264 /* end of perf_datastore_api.c */