glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / peerstore / perf_peerstore_store.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file peerstore/perf_peerstore_store.c
17  * @brief performance test for peerstore store operation
18  */
19 #include "platform.h"
20 #include "gnunet_util_lib.h"
21 #include "gnunet_testing_lib.h"
22 #include "gnunet_peerstore_service.h"
23
24 #define STORES 10000
25
26 static int ok = 1;
27
28 static struct GNUNET_PEERSTORE_Handle *h;
29
30 static char *ss = "test_peerstore_stress";
31 static struct GNUNET_PeerIdentity p;
32 static char *k = "test_peerstore_stress_key";
33 static char *v = "test_peerstore_stress_val";
34
35 static int count = 0;
36
37 static void
38 disconnect ()
39 {
40   if (NULL != h)
41     GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
42   GNUNET_SCHEDULER_shutdown ();
43 }
44
45
46 static void
47 store ()
48 {
49   GNUNET_PEERSTORE_store (h, ss, &p, k, v, strlen (v) + 1,
50                           GNUNET_TIME_UNIT_FOREVER_ABS,
51                           (count ==
52                            0) ? GNUNET_PEERSTORE_STOREOPTION_REPLACE :
53                           GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
54   count++;
55 }
56
57
58 static void
59 watch_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
60           const char *emsg)
61 {
62   GNUNET_assert (NULL == emsg);
63   if (STORES == count)
64   {
65     ok = 0;
66     disconnect ();
67   }
68   else
69     store ();
70 }
71
72
73 static void
74 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
75      struct GNUNET_TESTING_Peer *peer)
76 {
77   memset (&p, 5, sizeof (p));
78   h = GNUNET_PEERSTORE_connect (cfg);
79   GNUNET_assert (NULL != h);
80   GNUNET_PEERSTORE_watch (h, ss, &p, k, &watch_cb, NULL);
81   store ();
82 }
83
84
85 int
86 main (int argc, char *argv[])
87 {
88   struct GNUNET_TIME_Absolute start;
89   struct GNUNET_TIME_Relative diff;
90
91   start = GNUNET_TIME_absolute_get ();
92   if (0 !=
93       GNUNET_TESTING_service_run ("perf-peerstore-store", "peerstore",
94                                   "test_peerstore_api_data.conf", &run, NULL))
95     return 1;
96   diff = GNUNET_TIME_absolute_get_duration (start);
97   fprintf (stderr, "Stored and retrieved %d records in %s (%s).\n", STORES,
98            GNUNET_STRINGS_relative_time_to_string (diff, GNUNET_YES),
99            GNUNET_STRINGS_relative_time_to_string (diff, GNUNET_NO));
100   return ok;
101 }
102
103 /* end of perf_peerstore_store.c */