2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file set/gnunet-set-profiler.c
23 * @brief profiling tool for set
24 * @author Florian Dold
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_set_service.h"
30 #include "gnunet_testbed_service.h"
35 static unsigned int num_a = 5;
36 static unsigned int num_b = 5;
37 static unsigned int num_c = 20;
39 static unsigned int salt = 42;
41 static char *op_str = "union";
43 const static struct GNUNET_CONFIGURATION_Handle *config;
45 struct GNUNET_CONTAINER_MultiHashMap *map_a;
46 struct GNUNET_CONTAINER_MultiHashMap *map_b;
47 struct GNUNET_CONTAINER_MultiHashMap *map_c;
51 * Elements that set a received, should match map_c
54 struct GNUNET_CONTAINER_MultiHashMap *map_a_received;
57 * Elements that set b received, should match map_c
60 struct GNUNET_CONTAINER_MultiHashMap *map_b_received;
62 struct GNUNET_SET_Handle *set_a;
63 struct GNUNET_SET_Handle *set_b;
65 struct GNUNET_HashCode app_id;
67 struct GNUNET_PeerIdentity local_peer;
69 struct GNUNET_SET_ListenHandle *set_listener;
71 struct GNUNET_SET_OperationHandle *set_oh1;
72 struct GNUNET_SET_OperationHandle *set_oh2;
81 map_remove_iterator (void *cls,
82 const struct GNUNET_HashCode *key,
85 struct GNUNET_CONTAINER_MultiHashMap *m = cls;
88 ret = GNUNET_CONTAINER_multihashmap_remove (m, key, NULL);
89 GNUNET_assert (GNUNET_OK == ret);
96 set_result_cb_1 (void *cls,
97 const struct GNUNET_SET_Element *element,
98 enum GNUNET_SET_Status status)
100 GNUNET_assert (GNUNET_NO == a_done);
101 GNUNET_assert (element->size == sizeof (struct GNUNET_HashCode));
104 case GNUNET_SET_STATUS_DONE:
105 case GNUNET_SET_STATUS_HALF_DONE:
107 GNUNET_CONTAINER_multihashmap_iterate (map_c, map_remove_iterator, map_a_received);
108 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (map_a_received));
110 case GNUNET_SET_STATUS_FAILURE:
113 case GNUNET_SET_STATUS_OK:
118 GNUNET_CONTAINER_multihashmap_put (map_a_received,
120 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
125 set_result_cb_2 (void *cls,
126 const struct GNUNET_SET_Element *element,
127 enum GNUNET_SET_Status status)
129 GNUNET_assert (GNUNET_NO == b_done);
130 GNUNET_assert (element->size == sizeof (struct GNUNET_HashCode));
133 case GNUNET_SET_STATUS_DONE:
134 case GNUNET_SET_STATUS_HALF_DONE:
136 GNUNET_CONTAINER_multihashmap_iterate (map_c, map_remove_iterator, map_b_received);
137 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (map_b_received));
139 case GNUNET_SET_STATUS_FAILURE:
142 case GNUNET_SET_STATUS_OK:
147 GNUNET_CONTAINER_multihashmap_put (map_b_received,
149 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
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)
159 GNUNET_assert (NULL == set_oh2);
160 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set listen cb called\n");
161 set_oh2 = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED,
162 set_result_cb_2, NULL);
163 GNUNET_SET_commit (set_oh2, set_b);
169 set_insert_iterator (void *cls,
170 const struct GNUNET_HashCode *key,
173 struct GNUNET_SET_Handle *set = cls;
174 struct GNUNET_SET_Element *el;
176 el = GNUNET_malloc (sizeof *el + sizeof *key);
178 memcpy (&el[1], key, sizeof *key);
180 el->size = sizeof *key;
181 GNUNET_SET_add_element (set, el, NULL, NULL);
188 run (void *cls, char *const *args, const char *cfgfile,
189 const struct GNUNET_CONFIGURATION_Handle *cfg)
192 struct GNUNET_HashCode hash;
196 if (GNUNET_OK != GNUNET_CRYPTO_get_host_identity (cfg, &local_peer))
202 map_a = GNUNET_CONTAINER_multihashmap_create (num_a, GNUNET_NO);
203 map_b = GNUNET_CONTAINER_multihashmap_create (num_b, GNUNET_NO);
204 map_c = GNUNET_CONTAINER_multihashmap_create (num_c, GNUNET_NO);
206 for (i = 0; i < num_a; i++)
208 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
209 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
214 GNUNET_CONTAINER_multihashmap_put (map_a, &hash, &hash,
215 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
218 for (i = 0; i < num_b; i++)
220 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
221 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
226 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_b, &hash))
231 GNUNET_CONTAINER_multihashmap_put (map_b, &hash, NULL,
232 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
235 for (i = 0; i < num_c; i++)
237 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
238 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
243 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_b, &hash))
248 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_c, &hash))
253 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
254 GNUNET_CONTAINER_multihashmap_put (map_c, &hash, NULL,
255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
258 /* use last hash for app id */
261 /* FIXME: also implement intersection etc. */
262 set_a = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
263 set_b = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
265 GNUNET_CONTAINER_multihashmap_iterate (map_a, set_insert_iterator, set_a);
266 GNUNET_CONTAINER_multihashmap_iterate (map_b, set_insert_iterator, set_b);
267 GNUNET_CONTAINER_multihashmap_iterate (map_c, set_insert_iterator, set_a);
268 GNUNET_CONTAINER_multihashmap_iterate (map_c, set_insert_iterator, set_b);
270 set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
271 &app_id, set_listen_cb, NULL);
273 set_oh1 = GNUNET_SET_prepare (&local_peer, &app_id, NULL, salt, GNUNET_SET_RESULT_ADDED,
274 set_result_cb_1, NULL);
275 GNUNET_SET_commit (set_oh1, set_a);
280 main (int argc, char **argv)
282 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
283 { 'A', "num-first", NULL,
284 gettext_noop ("number of values"),
285 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_a },
286 { 'B', "num-second", NULL,
287 gettext_noop ("number of values"),
288 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_b },
289 { 'C', "num-common", NULL,
290 gettext_noop ("number of values"),
291 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_c },
292 { 'x', "operation", NULL,
293 gettext_noop ("oeration to execute"),
294 GNUNET_YES, &GNUNET_GETOPT_set_string, &op_str },
295 GNUNET_GETOPT_OPTION_END
297 GNUNET_PROGRAM_run (argc, argv, "gnunet-set-profiler",
299 options, &run, NULL);