a few more changes to the lib and the service for intersection
[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 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 /**
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_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_set_service.h"
30 #include "gnunet_testbed_service.h"
31
32
33 static int ret;
34
35 static unsigned int num_a = 5;
36 static unsigned int num_b = 5;
37 static unsigned int num_c = 20;
38
39 static unsigned int salt = 42;
40
41 static char* op_str = "union";
42
43 const static struct GNUNET_CONFIGURATION_Handle *config;
44
45 struct GNUNET_CONTAINER_MultiHashMap *map_a;
46 struct GNUNET_CONTAINER_MultiHashMap *map_b;
47 struct GNUNET_CONTAINER_MultiHashMap *map_c;
48
49
50 /**
51  * Elements that set a received, should match map_c
52  * in the end.
53  */
54 struct GNUNET_CONTAINER_MultiHashMap *map_a_received;
55
56 /**
57  * Elements that set b received, should match map_c
58  * in the end.
59  */
60 struct GNUNET_CONTAINER_MultiHashMap *map_b_received;
61
62 struct GNUNET_SET_Handle *set_a;
63 struct GNUNET_SET_Handle *set_b;
64
65 struct GNUNET_HashCode app_id;
66
67 struct GNUNET_PeerIdentity local_peer;
68
69 struct GNUNET_SET_ListenHandle *set_listener;
70
71 struct GNUNET_SET_OperationHandle *set_oh1;
72 struct GNUNET_SET_OperationHandle *set_oh2;
73
74
75 int a_done;
76 int b_done;
77
78
79
80 static int
81 map_remove_iterator (void *cls,
82                      const struct GNUNET_HashCode *key,
83                      void *value)
84 {
85   struct GNUNET_CONTAINER_MultiHashMap *m = cls;
86   int ret;
87
88   ret = GNUNET_CONTAINER_multihashmap_remove (m, key, NULL);
89   GNUNET_assert (GNUNET_OK == ret);
90   return GNUNET_YES;
91
92 }
93
94
95 static void
96 set_result_cb_1 (void *cls,
97                  const struct GNUNET_SET_Element *element,
98                  enum GNUNET_SET_Status status)
99 {
100   GNUNET_assert (GNUNET_NO == a_done);
101   GNUNET_assert (element->size == sizeof (struct GNUNET_HashCode));
102   switch (status)
103   {
104     case GNUNET_SET_STATUS_DONE:
105     case GNUNET_SET_STATUS_HALF_DONE:
106       a_done = GNUNET_YES;
107       GNUNET_CONTAINER_multihashmap_iterate (map_c, map_remove_iterator, map_a_received);
108       GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (map_a_received));
109       return;
110     case GNUNET_SET_STATUS_FAILURE:
111       GNUNET_assert (0);
112       return;
113     case GNUNET_SET_STATUS_OK:
114       break;
115     default:
116       GNUNET_assert (0);
117   }
118   GNUNET_CONTAINER_multihashmap_put (map_a_received,
119                                      element->data, NULL,
120                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
121 }
122
123
124 static void
125 set_result_cb_2 (void *cls,
126                  const struct GNUNET_SET_Element *element,
127                  enum GNUNET_SET_Status status)
128 {
129   GNUNET_assert (GNUNET_NO == b_done);
130   GNUNET_assert (element->size == sizeof (struct GNUNET_HashCode));
131   switch (status)
132   {
133     case GNUNET_SET_STATUS_DONE:
134     case GNUNET_SET_STATUS_HALF_DONE:
135       b_done = GNUNET_YES;
136       GNUNET_CONTAINER_multihashmap_iterate (map_c, map_remove_iterator, map_b_received);
137       GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (map_b_received));
138       return;
139     case GNUNET_SET_STATUS_FAILURE:
140       GNUNET_assert (0);
141       return;
142     case GNUNET_SET_STATUS_OK:
143       break;
144     default:
145       GNUNET_assert (0);
146   }
147   GNUNET_CONTAINER_multihashmap_put (map_b_received,
148                                      element->data, NULL,
149                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
150 }
151
152
153 static void
154 set_listen_cb (void *cls,
155                const struct GNUNET_PeerIdentity *other_peer,
156                const struct GNUNET_MessageHeader *context_msg,
157                struct GNUNET_SET_Request *request)
158 {
159   GNUNET_assert (NULL == set_oh2);
160   set_oh2 = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED,
161                                set_result_cb_2, NULL);
162   GNUNET_SET_commit (set_oh2, set_b);
163 }
164
165
166
167 static int
168 set_insert_iterator (void *cls,
169                      const struct GNUNET_HashCode *key,
170                      void *value)
171 {
172   struct GNUNET_SET_Handle *set = cls;
173   struct GNUNET_SET_Element *el;
174
175   el = GNUNET_malloc (sizeof *el + sizeof *key);
176   el->type = 0;
177   memcpy (&el[1], key, sizeof *key);
178   el->data = &el[1];
179   el->size = sizeof *key;
180   GNUNET_SET_add_element (set, el, NULL, NULL);
181   GNUNET_free (el);
182   return GNUNET_YES;
183 }
184
185
186 /**
187  * Signature of the 'main' function for a (single-peer) testcase that
188  * is run using 'GNUNET_TESTING_peer_run'.
189  * 
190  * @param cls closure
191  * @param cfg configuration of the peer that was started
192  * @param peer identity of the peer that was created
193  */
194 static void
195 test_main (void *cls,
196           const struct GNUNET_CONFIGURATION_Handle *cfg,
197           struct GNUNET_TESTING_Peer *peer)
198 {
199   unsigned int i;
200   struct GNUNET_HashCode hash;
201
202   config = cfg;
203
204   if (GNUNET_OK != GNUNET_CRYPTO_get_host_identity (cfg, &local_peer))
205   {
206     GNUNET_assert (0);
207     return;
208   }
209   
210   map_a = GNUNET_CONTAINER_multihashmap_create (num_a, GNUNET_NO);
211   map_b = GNUNET_CONTAINER_multihashmap_create (num_b, GNUNET_NO);
212   map_c = GNUNET_CONTAINER_multihashmap_create (num_c, GNUNET_NO);
213
214   for (i = 0; i < num_a; i++)
215   {
216     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
217     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
218     {
219       i--;
220       continue;
221     }
222     GNUNET_CONTAINER_multihashmap_put (map_a, &hash, &hash,
223                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
224   }
225
226   for (i = 0; i < num_b; i++)
227   {
228     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
229     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
230     {
231       i--;
232       continue;
233     }
234     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_b, &hash))
235     {
236       i--;
237       continue;
238     }
239     GNUNET_CONTAINER_multihashmap_put (map_b, &hash, NULL,
240                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
241   }
242
243   for (i = 0; i < num_c; i++)
244   {
245     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
246     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
247     {
248       i--;
249       continue;
250     }
251     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_b, &hash))
252     {
253       i--;
254       continue;
255     }
256     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_c, &hash))
257     {
258       i--;
259       continue;
260     }
261     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
262     GNUNET_CONTAINER_multihashmap_put (map_c, &hash, NULL,
263                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
264   }
265
266   /* use last hash for app id */
267   app_id = hash;
268
269   /* FIXME: also implement intersection etc. */
270   set_a = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
271   set_b = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
272
273   GNUNET_CONTAINER_multihashmap_iterate (map_a, set_insert_iterator, set_a);
274   GNUNET_CONTAINER_multihashmap_iterate (map_b, set_insert_iterator, set_b);
275   GNUNET_CONTAINER_multihashmap_iterate (map_c, set_insert_iterator, set_a);
276   GNUNET_CONTAINER_multihashmap_iterate (map_c, set_insert_iterator, set_b);
277
278   set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
279                                     &app_id, set_listen_cb, NULL);
280
281   set_oh1 = GNUNET_SET_prepare (&local_peer, &app_id, NULL, salt, GNUNET_SET_RESULT_ADDED,
282                        set_result_cb_1, NULL);
283   GNUNET_SET_commit (set_oh1, set_a);
284 }
285
286 static void
287 run (void *cls, char *const *args, const char *cfgfile,
288      const struct GNUNET_CONFIGURATION_Handle *cfg)
289 {
290
291   ret = GNUNET_TESTING_peer_run ("test_set_api",
292                                  "test_set.conf",
293                                  &test_main, NULL);
294 }
295
296
297 int
298 main (int argc, char **argv)
299 {
300    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
301       { 'A', "num-first", NULL,
302         gettext_noop ("number of values"),
303         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
304       { 'B', "num-second", NULL,
305         gettext_noop ("number of values"),
306         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
307       { 'B', "num-common", NULL,
308         gettext_noop ("number of values"),
309         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
310       { 'x', "operation", NULL,
311         gettext_noop ("oeration to execute"),
312         GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
313       GNUNET_GETOPT_OPTION_END
314   };
315   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus",
316                       "help",
317                       options, &run, NULL, GNUNET_YES);
318   return ret;
319 }
320