Merge branch 'master' of gnunet.org:gnunet
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file peerstore/perf_peerstore_store.c
20  * @brief performance test for peerstore store operation
21  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testing_lib.h"
25 #include "gnunet_peerstore_service.h"
26
27 #define STORES 10000
28
29 static int ok = 1;
30
31 static struct GNUNET_PEERSTORE_Handle *h;
32
33 static char *ss = "test_peerstore_stress";
34 static struct GNUNET_PeerIdentity p;
35 static char *k = "test_peerstore_stress_key";
36 static char *v = "test_peerstore_stress_val";
37
38 static int count = 0;
39
40 static void
41 disconnect ()
42 {
43   if (NULL != h)
44     GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
45   GNUNET_SCHEDULER_shutdown ();
46 }
47
48
49 static void
50 store ()
51 {
52   GNUNET_PEERSTORE_store (h, ss, &p, k, v, strlen (v) + 1,
53                           GNUNET_TIME_UNIT_FOREVER_ABS,
54                           (count ==
55                            0) ? GNUNET_PEERSTORE_STOREOPTION_REPLACE :
56                           GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
57   count++;
58 }
59
60
61 static void
62 watch_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
63           const char *emsg)
64 {
65   GNUNET_assert (NULL == emsg);
66   if (STORES == count)
67   {
68     ok = 0;
69     disconnect ();
70   }
71   else
72     store ();
73 }
74
75
76 static void
77 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
78      struct GNUNET_TESTING_Peer *peer)
79 {
80   memset (&p, 5, sizeof (p));
81   h = GNUNET_PEERSTORE_connect (cfg);
82   GNUNET_assert (NULL != h);
83   GNUNET_PEERSTORE_watch (h, ss, &p, k, &watch_cb, NULL);
84   store ();
85 }
86
87
88 int
89 main (int argc, char *argv[])
90 {
91   struct GNUNET_TIME_Absolute start;
92   struct GNUNET_TIME_Relative diff;
93
94   start = GNUNET_TIME_absolute_get ();
95   if (0 !=
96       GNUNET_TESTING_service_run ("perf-peerstore-store", "peerstore",
97                                   "test_peerstore_api_data.conf", &run, NULL))
98     return 1;
99   diff = GNUNET_TIME_absolute_get_duration (start);
100   fprintf (stderr, "Stored and retrieved %d records in %s (%s).\n", STORES,
101            GNUNET_STRINGS_relative_time_to_string (diff, GNUNET_YES),
102            GNUNET_STRINGS_relative_time_to_string (diff, GNUNET_NO));
103   return ok;
104 }
105
106 /* end of perf_peerstore_store.c */