2 * Copyright (c) 2015 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_8_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)
75 rndkey = random_u64 (&tm->seed);
77 p = hash_get (tm->key_hash, rndkey);
82 rndkey = (u64) (i + 1) << 16;
84 hash_set (tm->key_hash, rndkey, i + 1);
85 vec_add1 (tm->keys, rndkey);
88 fformat (stdout, "Add items...\n");
89 for (i = 0; i < tm->nitems; i++)
94 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
98 fformat (stdout, "--------------------\n");
99 fformat (stdout, "After adding key %llu value %lld...\n",
100 tm->keys[i], (u64) (i + 1));
101 fformat (stdout, "%U", BV (format_bihash), h,
102 2 /* very verbose */ );
106 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
108 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
110 before = clib_time_now (&tm->clib_time);
112 for (j = 0; j < tm->search_iter; j++)
114 for (i = 0; i < tm->nitems; i++)
116 kv.key = tm->keys[i];
117 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
118 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
119 clib_warning ("[%d] search for key %lld failed unexpectedly\n",
121 if (kv.value != (u64) (i + 1))
123 ("[%d] search for key %lld returned %lld, not %lld\n", i,
124 tm->keys, kv.value, (u64) (i + 1));
128 delta = clib_time_now (&tm->clib_time) - before;
129 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
132 fformat (stdout, "%.f searches per second\n",
133 ((f64) total_searches) / delta);
135 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
137 fformat (stdout, "Standard E-hash search for items %d times...\n",
140 before = clib_time_now (&tm->clib_time);
142 for (j = 0; j < tm->search_iter; j++)
144 for (i = 0; i < tm->nitems; i++)
146 p = hash_get (tm->key_hash, tm->keys[i]);
147 if (p == 0 || p[0] != (uword) (i + 1))
148 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
152 delta = clib_time_now (&tm->clib_time) - before;
153 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
155 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
158 fformat (stdout, "%.f searches per second\n",
159 ((f64) total_searches) / delta);
161 fformat (stdout, "Delete items...\n");
163 for (i = 0; i < tm->nitems; i++)
168 kv.key = tm->keys[i];
169 kv.value = (u64) (i + 1);
170 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
173 clib_warning ("delete key %lld not ok but should be", tm->keys[i]);
175 if (tm->careful_delete_tests)
177 for (j = 0; j < tm->nitems; j++)
179 kv.key = tm->keys[j];
180 rv = BV (clib_bihash_search) (h, &kv, &kv);
181 if (j <= i && rv >= 0)
184 ("i %d j %d search ok but should not be, value %lld",
189 clib_warning ("i %d j %d search not ok but should be",
196 fformat (stdout, "After deletions, should be empty...\n");
198 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
203 test_bihash_main (test_main_t * tm)
205 unformat_input_t *i = tm->input;
208 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
210 if (unformat (i, "seed %u", &tm->seed))
213 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
215 else if (unformat (i, "non-random-keys"))
216 tm->non_random_keys = 1;
217 else if (unformat (i, "nitems %d", &tm->nitems))
219 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
221 else if (unformat (i, "verbose %d", &tm->verbose))
223 else if (unformat (i, "search %d", &tm->search_iter))
225 else if (unformat (i, "verbose"))
228 return clib_error_return (0, "unknown input '%U'",
229 format_unformat_error, i);
232 error = test_bihash (tm);
239 main (int argc, char *argv[])
243 test_main_t *tm = &test_main;
245 clib_mem_init (0, 3ULL << 30);
248 tm->seed = 0xdeaddabe;
254 tm->careful_delete_tests = 0;
255 tm->key_hash = hash_create (0, sizeof (uword));
256 clib_time_init (&tm->clib_time);
258 unformat_init_command_line (&i, argv);
259 error = test_bihash_main (tm);
264 clib_error_report (error);
269 #endif /* CLIB_UNIX */
272 * fd.io coding-style-patch-verification: ON
275 * eval: (c-set-style "gnu")