2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vppinfra/time.h>
16 #include <vppinfra/cache.h>
17 #include <vppinfra/error.h>
19 #include <vppinfra/bihash_vec8_8.h>
20 #include <vppinfra/bihash_template.h>
22 #include <vppinfra/bihash_template.c>
30 int careful_delete_tests;
35 BVT (clib_bihash) hash;
36 clib_time_t clib_time;
38 unformat_input_t *input;
42 test_main_t test_main;
51 test_bihash (test_main_t * tm)
57 BVT (clib_bihash) * h;
58 BVT (clib_bihash_kv) kv;
62 BV (clib_bihash_init) (h, "test", tm->nbuckets, 3ULL << 30);
64 fformat (stdout, "Pick %lld unique %s keys...\n",
65 tm->nitems, tm->non_random_keys ? "non-random" : "random");
67 for (i = 0; i < tm->nitems; i++)
71 if (tm->non_random_keys == 0)
78 len = random_u32 (&tm->seed);
83 random_vec8 = random_string (&tm->seed, len);
84 vec_add1 (random_vec8, 0);
86 p = hash_get_mem (tm->key_hash, random_vec8);
92 random_vec8 = format (0, "String%d%c", i, 0);
95 hash_set_mem (tm->key_hash, random_vec8, i + 1);
96 vec_add1 (tm->keys, random_vec8);
99 fformat (stdout, "Add items...\n");
100 for (i = 0; i < tm->nitems; i++)
102 kv.key = (u64) tm->keys[i];
105 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
109 fformat (stdout, "--------------------\n");
110 fformat (stdout, "After adding key %llu value %lld...\n",
111 tm->keys[i], (u64) (i + 1));
112 fformat (stdout, "%U", BV (format_bihash), h,
113 2 /* very verbose */ );
117 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
119 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
121 before = clib_time_now (&tm->clib_time);
123 for (j = 0; j < tm->search_iter; j++)
125 for (i = 0; i < tm->nitems; i++)
127 kv.key = (u64) tm->keys[i];
128 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
129 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
130 clib_warning ("[%d] search for key %lld failed unexpectedly\n",
132 if (kv.value != (u64) (i + 1))
134 ("[%d] search for key %lld returned %lld, not %lld\n", i,
135 tm->keys, kv.value, (u64) (i + 1));
139 delta = clib_time_now (&tm->clib_time) - before;
140 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
143 fformat (stdout, "%.f searches per second\n",
144 ((f64) total_searches) / delta);
146 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
148 fformat (stdout, "Standard E-hash search for items %d times...\n",
151 before = clib_time_now (&tm->clib_time);
153 for (j = 0; j < tm->search_iter; j++)
155 for (i = 0; i < tm->nitems; i++)
157 p = hash_get_mem (tm->key_hash, tm->keys[i]);
158 if (p == 0 || p[0] != (uword) (i + 1))
159 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
163 delta = clib_time_now (&tm->clib_time) - before;
164 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
166 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
169 fformat (stdout, "%.f searches per second\n",
170 ((f64) total_searches) / delta);
172 fformat (stdout, "Delete items...\n");
174 for (i = 0; i < tm->nitems; i++)
179 kv.key = (u64) tm->keys[i];
180 kv.value = (u64) (i + 1);
181 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
184 clib_warning ("delete key %lld not ok but should be", tm->keys[i]);
186 if (tm->careful_delete_tests)
188 for (j = 0; j < tm->nitems; j++)
190 kv.key = (u64) tm->keys[j];
191 rv = BV (clib_bihash_search) (h, &kv, &kv);
192 if (j <= i && rv >= 0)
195 ("i %d j %d search ok but should not be, value %lld",
200 clib_warning ("i %d j %d search not ok but should be",
207 fformat (stdout, "After deletions, should be empty...\n");
209 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
214 test_bihash_main (test_main_t * tm)
216 unformat_input_t *i = tm->input;
220 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
222 if (unformat (i, "seed %u", &tm->seed))
225 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
227 else if (unformat (i, "non-random-keys"))
228 tm->non_random_keys = 1;
229 else if (unformat (i, "nitems %d", &tm->nitems))
231 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
233 else if (unformat (i, "verbose %d", &tm->verbose))
235 else if (unformat (i, "search %d", &tm->search_iter))
237 else if (unformat (i, "vec64"))
239 else if (unformat (i, "cache"))
242 else if (unformat (i, "verbose"))
245 return clib_error_return (0, "unknown input '%U'",
246 format_unformat_error, i);
252 error = test_bihash (tm);
256 return clib_error_return (0, "no such test?");
264 main (int argc, char *argv[])
268 test_main_t *tm = &test_main;
270 clib_mem_init (0, 3ULL << 30);
273 tm->seed = 0xdeaddabe;
279 tm->careful_delete_tests = 0;
280 tm->key_hash = hash_create_string (0, sizeof (uword));
281 clib_time_init (&tm->clib_time);
283 unformat_init_command_line (&i, argv);
284 error = test_bihash_main (tm);
289 clib_error_report (error);
294 #endif /* CLIB_UNIX */
297 * fd.io coding-style-patch-verification: ON
300 * eval: (c-set-style "gnu")