session time out for http client/server
[oweals/gnunet.git] / src / set / gnunet-set-profiler.c
1 /*
2       This file is part of GNUnet
3       (C) 2013 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 /**
22  * @file set/gnunet-set-profiler.c
23  * @brief profiling tool for set
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_set_service.h"
29 #include "gnunet_testbed_service.h"
30
31
32 static int ret;
33
34 static unsigned int num_a = 5;
35 static unsigned int num_b = 5;
36 static unsigned int num_c = 20;
37
38 static unsigned int salt = 42;
39
40 static char *op_str = "union";
41
42 const static struct GNUNET_CONFIGURATION_Handle *config;
43
44 struct SetInfo
45 {
46   char *id;
47   struct GNUNET_SET_Handle *set;
48   struct GNUNET_SET_OperationHandle *oh;
49   struct GNUNET_CONTAINER_MultiHashMap *sent;
50   struct GNUNET_CONTAINER_MultiHashMap *received;
51   int done;
52 } info1, info2;
53
54 struct GNUNET_CONTAINER_MultiHashMap *common_sent;
55
56 struct GNUNET_HashCode app_id;
57
58 struct GNUNET_PeerIdentity local_peer;
59
60 struct GNUNET_SET_ListenHandle *set_listener;
61
62
63 static int
64 map_remove_iterator (void *cls,
65                      const struct GNUNET_HashCode *key,
66                      void *value)
67 {
68   struct GNUNET_CONTAINER_MultiHashMap *m = cls;
69   int ret;
70
71   GNUNET_assert (NULL != key);
72
73   ret = GNUNET_CONTAINER_multihashmap_remove (m, key, NULL);
74   if (GNUNET_OK != ret)
75     printf ("spurious element\n");
76   return GNUNET_YES;
77
78 }
79
80 static void
81 check_all_done (void)
82 {
83   if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
84     return;
85
86   GNUNET_CONTAINER_multihashmap_iterate (info1.received, map_remove_iterator, info2.sent);
87   GNUNET_CONTAINER_multihashmap_iterate (info2.received, map_remove_iterator, info1.sent);
88
89   printf ("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info1.sent));
90   printf ("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size (info2.sent));
91
92   GNUNET_SCHEDULER_shutdown ();
93 }
94
95
96 static void
97 set_result_cb (void *cls,
98                  const struct GNUNET_SET_Element *element,
99                  enum GNUNET_SET_Status status)
100 {
101   struct SetInfo *info = cls;
102
103   GNUNET_assert (GNUNET_NO == info->done);
104   switch (status)
105   {
106     case GNUNET_SET_STATUS_DONE:
107     case GNUNET_SET_STATUS_HALF_DONE:
108       info->done = GNUNET_YES;
109       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
110       check_all_done ();
111       info->oh = NULL;
112       return;
113     case GNUNET_SET_STATUS_FAILURE:
114       info->oh = NULL;
115       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failure\n");
116       GNUNET_SCHEDULER_shutdown ();
117       return;
118     case GNUNET_SET_STATUS_OK:
119       break;
120     default:
121       GNUNET_assert (0);
122   }
123
124   if (element->size != sizeof (struct GNUNET_HashCode))
125   {
126     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "wrong element size: %u\n", element->size);
127     GNUNET_assert (0);
128   }
129
130   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
131               info->id, GNUNET_h2s (element->data));
132   GNUNET_assert (NULL != element->data);
133   GNUNET_CONTAINER_multihashmap_put (info->received,
134                                      element->data, NULL,
135                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
136 }
137
138
139 static void
140 set_listen_cb (void *cls,
141                const struct GNUNET_PeerIdentity *other_peer,
142                const struct GNUNET_MessageHeader *context_msg,
143                struct GNUNET_SET_Request *request)
144 {
145   if (NULL == request)
146   {
147     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "listener failed\n");
148     return;
149   }
150   GNUNET_assert (NULL == info2.oh);
151   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set listen cb called\n");
152   info2.oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED,
153                                set_result_cb, &info2);
154   GNUNET_SET_commit (info2.oh, info2.set);
155 }
156
157
158
159 static int
160 set_insert_iterator (void *cls,
161                      const struct GNUNET_HashCode *key,
162                      void *value)
163 {
164   struct GNUNET_SET_Handle *set = cls;
165   struct GNUNET_SET_Element *el;
166
167   el = GNUNET_malloc (sizeof *el + sizeof *key);
168   el->type = 0;
169   memcpy (&el[1], key, sizeof *key);
170   el->data = &el[1];
171   el->size = sizeof *key;
172   GNUNET_SET_add_element (set, el, NULL, NULL);
173   GNUNET_free (el);
174   return GNUNET_YES;
175 }
176
177
178 static void
179 handle_shutdown (void *cls,
180                  const struct GNUNET_SCHEDULER_TaskContext *tc)
181 {
182   if (NULL != set_listener)
183   {
184     GNUNET_SET_listen_cancel (set_listener);
185     set_listener = NULL;
186   }
187   if (NULL != info1.oh)
188   {
189     GNUNET_SET_operation_cancel (info1.oh);
190     info1.oh = NULL;
191   }
192   if (NULL != info2.oh)
193   {
194     GNUNET_SET_operation_cancel (info2.oh);
195     info2.oh = NULL;
196   }
197   if (NULL != info1.set)
198   {
199     GNUNET_SET_destroy (info1.set);
200     info1.set = NULL;
201   }
202   if (NULL != info2.set)
203   {
204     GNUNET_SET_destroy (info2.set);
205     info2.set = NULL;
206   }
207 }
208
209 static void
210 run (void *cls, char *const *args, const char *cfgfile,
211      const struct GNUNET_CONFIGURATION_Handle *cfg)
212 {
213   unsigned int i;
214   struct GNUNET_HashCode hash;
215
216   config = cfg;
217
218   if (GNUNET_OK != GNUNET_CRYPTO_get_peer_identity (cfg, &local_peer))
219   {
220     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
221     ret = 0;
222     return;
223   }
224
225   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, handle_shutdown, NULL);
226
227   info1.id = "a";
228   info2.id = "b";
229
230   info1.sent = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
231   info2.sent = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
232   common_sent = GNUNET_CONTAINER_multihashmap_create (num_c+1, GNUNET_NO);
233
234   info1.received = GNUNET_CONTAINER_multihashmap_create (num_a+1, GNUNET_NO);
235   info2.received = GNUNET_CONTAINER_multihashmap_create (num_b+1, GNUNET_NO);
236
237   for (i = 0; i < num_a; i++)
238   {
239     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
240     GNUNET_CONTAINER_multihashmap_put (info1.sent, &hash, NULL,
241                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
242   }
243
244   for (i = 0; i < num_b; i++)
245   {
246     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
247     GNUNET_CONTAINER_multihashmap_put (info2.sent, &hash, NULL,
248                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
249   }
250
251   for (i = 0; i < num_c; i++)
252   {
253     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
254     GNUNET_CONTAINER_multihashmap_put (common_sent, &hash, NULL,
255                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
256   }
257
258   /* use last hash for app id */
259   app_id = hash;
260
261   /* FIXME: also implement intersection etc. */
262   info1.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
263   info2.set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
264
265   GNUNET_CONTAINER_multihashmap_iterate (info1.sent, set_insert_iterator, info1.set);
266   GNUNET_CONTAINER_multihashmap_iterate (info2.sent, set_insert_iterator, info2.set);
267   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info1.set);
268   GNUNET_CONTAINER_multihashmap_iterate (common_sent, set_insert_iterator, info2.set);
269
270   set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
271                                     &app_id, set_listen_cb, NULL);
272
273   info1.oh = GNUNET_SET_prepare (&local_peer, &app_id, NULL, salt, GNUNET_SET_RESULT_ADDED,
274                        set_result_cb, &info1);
275   GNUNET_SET_commit (info1.oh, info1.set);
276   GNUNET_SET_destroy (info1.set);
277   info1.set = NULL;
278 }
279
280
281 int
282 main (int argc, char **argv)
283 {
284    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
285       { 'A', "num-first", NULL,
286         gettext_noop ("number of values"),
287         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
288       { 'B', "num-second", NULL,
289         gettext_noop ("number of values"),
290         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
291       { 'C', "num-common", NULL,
292         gettext_noop ("number of values"),
293         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
294       { 'x', "operation", NULL,
295         gettext_noop ("oeration to execute"),
296         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
297       GNUNET_GETOPT_OPTION_END
298   };
299   GNUNET_PROGRAM_run (argc, argv, "gnunet-set-profiler",
300                       "help",
301                       options, &run, NULL);
302   return ret;
303 }
304