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 set_oh2 = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED,
161 set_result_cb_2, NULL);
162 GNUNET_SET_commit (set_oh2, set_b);
168 set_insert_iterator (void *cls,
169 const struct GNUNET_HashCode *key,
172 struct GNUNET_SET_Handle *set = cls;
173 struct GNUNET_SET_Element *el;
175 el = GNUNET_malloc (sizeof *el + sizeof *key);
177 memcpy (&el[1], key, sizeof *key);
179 el->size = sizeof *key;
180 GNUNET_SET_add_element (set, el, NULL, NULL);
187 * Signature of the 'main' function for a (single-peer) testcase that
188 * is run using 'GNUNET_TESTING_peer_run'.
191 * @param cfg configuration of the peer that was started
192 * @param peer identity of the peer that was created
195 test_main (void *cls,
196 const struct GNUNET_CONFIGURATION_Handle *cfg,
197 struct GNUNET_TESTING_Peer *peer)
200 struct GNUNET_HashCode hash;
204 if (GNUNET_OK != GNUNET_CRYPTO_get_host_identity (cfg, &local_peer))
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);
214 for (i = 0; i < num_a; i++)
216 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
217 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
222 GNUNET_CONTAINER_multihashmap_put (map_a, &hash, &hash,
223 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
226 for (i = 0; i < num_b; i++)
228 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
229 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
234 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_b, &hash))
239 GNUNET_CONTAINER_multihashmap_put (map_b, &hash, NULL,
240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
243 for (i = 0; i < num_c; i++)
245 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, &hash);
246 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_a, &hash))
251 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_b, &hash))
256 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (map_c, &hash))
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);
266 /* use last hash for app id */
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);
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);
278 set_listener = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
279 &app_id, set_listen_cb, NULL);
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);
287 run (void *cls, char *const *args, const char *cfgfile,
288 const struct GNUNET_CONFIGURATION_Handle *cfg)
291 ret = GNUNET_TESTING_peer_run ("test_set_api",
298 main (int argc, char **argv)
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
315 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus",
317 options, &run, NULL, GNUNET_YES);