-add newline
[oweals/gnunet.git] / src / datastore / perf_datastore_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007, 2009, 2011 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 3, 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 #include "gnunet_testing_lib-new.h"
40 #include <gauger.h>
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  */
50 #define MAX_SIZE 1024LL * 1024 * 4
51
52 /**
53  * Report progress outside of major reports? Should probably be GNUNET_YES if
54  * size is > 16 MB.
55  */
56 #define REPORT_ID GNUNET_YES
57
58 /**
59  * Number of put operations equivalent to 1/3rd of MAX_SIZE
60  */
61 #define PUT_10 MAX_SIZE / 32 / 1024 / 3
62
63 /**
64  * Total number of iterations (each iteration doing
65  * PUT_10 put operations); we report full status every
66  * 10 iterations.  Abort with CTRL-C.
67  */
68 #define ITERATIONS 8
69
70
71 static unsigned long long stored_bytes;
72
73 static unsigned long long stored_entries;
74
75 static unsigned long long stored_ops;
76
77 static struct GNUNET_TIME_Absolute start_time;
78
79 static const char *plugin_name;
80
81 static struct GNUNET_DATASTORE_Handle *datastore;
82
83 static int ok;
84
85
86 enum RunPhase
87 {
88   RP_DONE = 0,
89   RP_PUT,
90   RP_CUT,
91   RP_REPORT,
92   RP_ERROR
93 };
94
95
96 struct CpsRunContext
97 {
98   const struct GNUNET_CONFIGURATION_Handle *cfg;
99   enum RunPhase phase;
100   int j;
101   unsigned long long size;
102   int i;
103 };
104
105
106 static void
107 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
108
109
110 static void
111 check_success (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration,  const char *msg)
112 {
113   struct CpsRunContext *crc = cls;
114
115   if (GNUNET_OK != success)
116   {
117     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Check success failed: `%s'\n", msg);
118     crc->phase = RP_ERROR;
119     GNUNET_SCHEDULER_add_now (&run_continuation, crc);
120     return;
121   }
122 #if REPORT_ID
123   FPRINTF (stderr, "%s",  "I");
124 #endif
125   stored_bytes += crc->size;
126   stored_ops++;
127   stored_entries++;
128   crc->j++;
129   if (crc->j >= PUT_10)
130   {
131     crc->j = 0;
132     crc->i++;
133     if (crc->i == ITERATIONS)
134       crc->phase = RP_DONE;
135     else
136       crc->phase = RP_CUT;
137   }
138   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
139                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
140 }
141
142
143 /**
144  * Continuation called to notify client about result of the
145  * operation.
146  *
147  * @param cls closure
148  * @param success GNUNET_SYSERR on failure
149  * @param min_expiration minimum expiration time required for content to be stored
150  *                by the datacache at this time, zero for unknown
151  * @param msg NULL on success, otherwise an error message
152  */
153 static void
154 remove_next (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
155 {
156   struct CpsRunContext *crc = cls;
157
158   if (GNUNET_OK != success)
159   {
160     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "remove_next failed: `%s'\n", msg);
161     crc->phase = RP_ERROR;
162     GNUNET_SCHEDULER_add_now (&run_continuation, crc);
163     return;
164   }
165 #if REPORT_ID
166   FPRINTF (stderr, "%s",  "D");
167 #endif
168   GNUNET_assert (GNUNET_OK == success);
169   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
170 }
171
172
173 static void
174 delete_value (void *cls, const struct GNUNET_HashCode * key, size_t size,
175               const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
176               uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
177               uint64_t uid)
178 {
179   struct CpsRunContext *crc = cls;
180
181   GNUNET_assert (NULL != key);
182   stored_ops++;
183   stored_bytes -= size;
184   stored_entries--;
185   stored_ops++;
186   if (stored_bytes < MAX_SIZE)
187     crc->phase = RP_PUT;
188   GNUNET_assert (NULL !=
189                  GNUNET_DATASTORE_remove (datastore, key, size, data, 1, 1,
190                                           TIMEOUT, &remove_next, crc));
191 }
192
193
194 static void
195 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 {
197   struct CpsRunContext *crc = cls;
198   size_t size;
199   static struct GNUNET_HashCode key;
200   static char data[65536];
201   int i;
202   int k;
203   char gstr[128];
204
205   ok = (int) crc->phase;
206   switch (crc->phase)
207   {
208   case RP_PUT:
209     memset (&key, 256 - crc->i, sizeof (struct GNUNET_HashCode));
210     i = crc->j;
211     k = crc->i;
212     /* most content is 32k */
213     size = 32 * 1024;
214     if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
215       size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
216     crc->size = size = size - (size & 7);       /* always multiple of 8 */
217     GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &key);
218     memset (data, i, size);
219     if (i > 255)
220       memset (data, i - 255, size / 2);
221     data[0] = k;
222     GNUNET_assert (NULL !=
223                    GNUNET_DATASTORE_put (datastore, 0, &key, size, data, i + 1,
224                                          GNUNET_CRYPTO_random_u32
225                                          (GNUNET_CRYPTO_QUALITY_WEAK, 100), i,
226                                          0,
227                                          GNUNET_TIME_relative_to_absolute
228                                          (GNUNET_TIME_relative_multiply
229                                           (GNUNET_TIME_UNIT_SECONDS,
230                                            GNUNET_CRYPTO_random_u32
231                                            (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
232                                          1, 1, TIMEOUT, &check_success, crc));
233     break;
234   case RP_CUT:
235     /* trim down below MAX_SIZE again */
236     GNUNET_assert (NULL !=
237                    GNUNET_DATASTORE_get_for_replication (datastore, 1, 1,
238                                                          TIMEOUT, &delete_value,
239                                                          crc));
240     break;
241   case RP_REPORT:
242     printf (
243 #if REPORT_ID
244              "\n"
245 #endif
246              "Stored %llu kB / %lluk ops / %llu ops/s\n", stored_bytes / 1024,  /* used size in k */
247              stored_ops / 1024, /* total operations (in k) */
248              1000 * stored_ops / (1 +
249                                   GNUNET_TIME_absolute_get_duration
250                                   (start_time).rel_value));
251     crc->phase = RP_PUT;
252     crc->j = 0;
253     GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
254                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
255     break;
256   case RP_DONE:
257     GNUNET_snprintf (gstr, sizeof (gstr), "DATASTORE-%s", plugin_name);
258     if ((crc->i == ITERATIONS) && (stored_ops > 0))
259       GAUGER (gstr, "PUT operation duration",
260               GNUNET_TIME_absolute_get_duration (start_time).rel_value /
261               stored_ops, "ms/operation");
262     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
263     GNUNET_free (crc);
264     ok = 0;
265     break;
266   case RP_ERROR:
267     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
268     GNUNET_free (crc);
269     ok = 1;
270     break;
271   default:
272     GNUNET_assert (0);
273   }
274 }
275
276
277 static void
278 run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
279 {
280   struct CpsRunContext *crc = cls;
281
282   if (success != GNUNET_YES)
283   {
284     FPRINTF (stderr,
285              "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
286              msg);
287     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
288     GNUNET_free (crc);
289     return;
290   }
291   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
292                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
293 }
294
295
296 static void
297 run (void *cls, 
298      const struct GNUNET_CONFIGURATION_Handle *cfg,
299      struct GNUNET_TESTING_Peer *peer)
300 {
301   struct CpsRunContext *crc;
302   static struct GNUNET_HashCode zkey;
303
304   datastore = GNUNET_DATASTORE_connect (cfg);
305   start_time = GNUNET_TIME_absolute_get ();
306   crc = GNUNET_malloc (sizeof (struct CpsRunContext));
307   crc->cfg = cfg;
308   crc->phase = RP_PUT;
309   if (NULL ==
310       GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST",
311                             GNUNET_BLOCK_TYPE_TEST, 0, 0, 0,
312                             GNUNET_TIME_relative_to_absolute
313                             (GNUNET_TIME_UNIT_SECONDS), 0, 1,
314                             GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
315   {
316     FPRINTF (stderr, "%s",  "Test 'put' operation failed.\n");
317     ok = 1;
318     GNUNET_free (crc);
319   }
320 }
321
322
323 int
324 main (int argc, char *argv[])
325 {
326   char cfg_name[128];
327
328   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
329   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
330                    "test_datastore_api_data_%s.conf", plugin_name);
331   if (0 !=
332       GNUNET_TESTING_peer_run ("perf-gnunet-datastore",
333                                cfg_name,
334                                &run,
335                                NULL))
336     return 1;
337   FPRINTF (stderr, "%s", "\n");
338   return ok;
339 }
340
341 /* end of perf_datastore_api.c */