2 This file is part of GNUnet.
3 (C) 2012 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 3, 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-ibf-profiler.c
23 * @brief tool for profiling the invertible bloom filter implementation
24 * @author Florian Dold
28 #include "gnunet_util_lib.h"
32 static unsigned int asize = 10;
33 static unsigned int bsize = 10;
34 static unsigned int csize = 10;
35 static unsigned int hash_num = 4;
36 static unsigned int ibf_size = 80;
38 /* FIXME: add parameter for this */
39 static enum GNUNET_CRYPTO_Quality random_quality = GNUNET_CRYPTO_QUALITY_WEAK;
41 static struct GNUNET_CONTAINER_MultiHashMap *set_a;
42 static struct GNUNET_CONTAINER_MultiHashMap *set_b;
43 /* common elements in a and b */
44 static struct GNUNET_CONTAINER_MultiHashMap *set_c;
46 static struct GNUNET_CONTAINER_MultiHashMap *key_to_hashcode;
48 static struct InvertibleBloomFilter *ibf_a;
49 static struct InvertibleBloomFilter *ibf_b;
53 register_hashcode (struct GNUNET_HashCode *hash)
55 struct GNUNET_HashCode replicated;
57 key = ibf_key_from_hashcode (hash);
58 ibf_hashcode_from_key (key, &replicated);
59 (void) GNUNET_CONTAINER_multihashmap_put (key_to_hashcode,
61 GNUNET_memdup (hash, sizeof *hash),
62 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
67 iter_hashcodes (struct IBF_Key key,
68 GNUNET_CONTAINER_HashMapIterator iter,
71 struct GNUNET_HashCode replicated;
73 ibf_hashcode_from_key (key, &replicated);
74 GNUNET_CONTAINER_multihashmap_get_multiple (key_to_hashcode,
81 insert_iterator (void *cls,
82 const struct GNUNET_HashCode *key,
85 struct InvertibleBloomFilter *ibf = cls;
87 ibf_insert (ibf, ibf_key_from_hashcode (key));
93 remove_iterator (void *cls,
94 const struct GNUNET_HashCode *key,
97 struct GNUNET_CONTAINER_MultiHashMap *hashmap = cls;
98 /* if remove fails, there just was a collision with another key */
99 (void) GNUNET_CONTAINER_multihashmap_remove (hashmap, value, NULL);
105 run (void *cls, char *const *args, const char *cfgfile,
106 const struct GNUNET_CONFIGURATION_Handle *cfg)
108 struct GNUNET_HashCode id;
109 struct IBF_Key ibf_key;
113 struct GNUNET_TIME_Absolute start_time;
114 struct GNUNET_TIME_Relative delta_time;
116 set_a = GNUNET_CONTAINER_multihashmap_create (((asize == 0) ? 1 : (asize + csize)),
118 set_b = GNUNET_CONTAINER_multihashmap_create (((bsize == 0) ? 1 : (bsize + csize)),
120 set_c = GNUNET_CONTAINER_multihashmap_create (((csize == 0) ? 1 : csize),
123 key_to_hashcode = GNUNET_CONTAINER_multihashmap_create (((asize+bsize+csize == 0) ? 1 : (asize+bsize+csize)),
126 printf ("hash-num=%u, size=%u, #(A-B)=%u, #(B-A)=%u, #(A&B)=%u\n",
127 hash_num, ibf_size, asize, bsize, csize);
132 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
133 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
135 GNUNET_break (GNUNET_OK ==
136 GNUNET_CONTAINER_multihashmap_put (set_a, &id, NULL,
137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
138 register_hashcode (&id);
144 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
145 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
147 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
149 GNUNET_break (GNUNET_OK ==
150 GNUNET_CONTAINER_multihashmap_put (set_b, &id, NULL,
151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
152 register_hashcode (&id);
158 GNUNET_CRYPTO_hash_create_random (random_quality, &id);
159 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
161 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
163 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_c, &id))
165 GNUNET_break (GNUNET_OK ==
166 GNUNET_CONTAINER_multihashmap_put (set_c, &id, NULL,
167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
168 register_hashcode (&id);
172 ibf_a = ibf_create (ibf_size, hash_num);
173 ibf_b = ibf_create (ibf_size, hash_num);
175 printf ("generated sets\n");
177 start_time = GNUNET_TIME_absolute_get ();
179 GNUNET_CONTAINER_multihashmap_iterate (set_a, &insert_iterator, ibf_a);
180 GNUNET_CONTAINER_multihashmap_iterate (set_b, &insert_iterator, ibf_b);
181 GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_a);
182 GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_b);
184 delta_time = GNUNET_TIME_absolute_get_duration (start_time);
186 printf ("encoded in: %s\n",
187 GNUNET_STRINGS_relative_time_to_string (delta_time,
190 ibf_subtract (ibf_a, ibf_b);
193 start_time = GNUNET_TIME_absolute_get ();
195 for (i = 0; i <= asize + bsize; i++)
197 res = ibf_decode (ibf_a, &side, &ibf_key);
198 if (GNUNET_SYSERR == res)
200 printf ("decode failed, %u/%u elements left\n",
201 GNUNET_CONTAINER_multihashmap_size (set_a) + GNUNET_CONTAINER_multihashmap_size (set_b),
205 if (GNUNET_NO == res)
207 if ((0 == GNUNET_CONTAINER_multihashmap_size (set_b)) &&
208 (0 == GNUNET_CONTAINER_multihashmap_size (set_a)))
210 delta_time = GNUNET_TIME_absolute_get_duration (start_time);
211 printf ("decoded successfully in: %s\n",
212 GNUNET_STRINGS_relative_time_to_string (delta_time,
217 printf ("decode missed elements (should never happen)\n");
223 iter_hashcodes (ibf_key, remove_iterator, set_a);
225 iter_hashcodes (ibf_key, remove_iterator, set_b);
227 printf("cyclic IBF, %u/%u elements left\n",
228 GNUNET_CONTAINER_multihashmap_size (set_a) + GNUNET_CONTAINER_multihashmap_size (set_b),
233 main (int argc, char **argv)
235 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
237 gettext_noop ("number of element in set A-B"), 1,
238 &GNUNET_GETOPT_set_uint, &asize},
240 gettext_noop ("number of element in set B-A"), 1,
241 &GNUNET_GETOPT_set_uint, &bsize},
243 gettext_noop ("number of common elements in A and B"), 1,
244 &GNUNET_GETOPT_set_uint, &csize},
245 {'k', "hash-num", NULL,
246 gettext_noop ("hash num"), 1,
247 &GNUNET_GETOPT_set_uint, &hash_num},
248 {'s', "ibf-size", NULL,
249 gettext_noop ("ibf size"), 1,
250 &GNUNET_GETOPT_set_uint, &ibf_size},
251 GNUNET_GETOPT_OPTION_END
253 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-ibf",
255 options, &run, NULL, GNUNET_YES);