blob: ef03f565e1d20e3581f17ec0d7e2f4effdb574cc [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include <vppinfra/time.h>
16#include <vppinfra/cache.h>
17#include <vppinfra/error.h>
18
19#include <vppinfra/bihash_8_8.h>
20#include <vppinfra/bihash_template.h>
21
22#include <vppinfra/bihash_template.c>
23
Dave Barachc3799992016-08-15 11:12:27 -040024typedef struct
25{
Filip Tehlar397fd7d2016-10-26 14:31:24 +020026 u64 seed;
Ed Warnickecb9cada2015-12-08 15:45:58 -070027 u32 nbuckets;
28 u32 nitems;
29 u32 search_iter;
30 int careful_delete_tests;
31 int verbose;
32 int non_random_keys;
Dave Barachc3799992016-08-15 11:12:27 -040033 uword *key_hash;
34 u64 *keys;
35 BVT (clib_bihash) hash;
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 clib_time_t clib_time;
37
Dave Barachc3799992016-08-15 11:12:27 -040038 unformat_input_t *input;
39
Ed Warnickecb9cada2015-12-08 15:45:58 -070040} test_main_t;
41
42test_main_t test_main;
43
Dave Barachc3799992016-08-15 11:12:27 -040044uword
45vl (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
Dave Barachc3799992016-08-15 11:12:27 -040047 return vec_len (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -070048}
49
Dave Barachc3799992016-08-15 11:12:27 -040050static clib_error_t *
51test_bihash (test_main_t * tm)
Ed Warnickecb9cada2015-12-08 15:45:58 -070052{
53 int i, j;
Dave Barachc3799992016-08-15 11:12:27 -040054 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 uword total_searches;
56 f64 before, delta;
Dave Barachc3799992016-08-15 11:12:27 -040057 BVT (clib_bihash) * h;
58 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
60 h = &tm->hash;
61
Dave Barachc3799992016-08-15 11:12:27 -040062 BV (clib_bihash_init) (h, "test", tm->nbuckets, 3ULL << 30);
63
64 fformat (stdout, "Pick %lld unique %s keys...\n",
65 tm->nitems, tm->non_random_keys ? "non-random" : "random");
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
67 for (i = 0; i < tm->nitems; i++)
68 {
69 u64 rndkey;
70
71 if (tm->non_random_keys == 0)
Dave Barachc3799992016-08-15 11:12:27 -040072 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
Dave Barachc3799992016-08-15 11:12:27 -040074 again:
75 rndkey = random_u64 (&tm->seed);
76
77 p = hash_get (tm->key_hash, rndkey);
78 if (p)
79 goto again;
80 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 else
Dave Barachc3799992016-08-15 11:12:27 -040082 rndkey = (u64) (i + 1) << 16;
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
Dave Barachc3799992016-08-15 11:12:27 -040084 hash_set (tm->key_hash, rndkey, i + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 vec_add1 (tm->keys, rndkey);
86 }
87
88 fformat (stdout, "Add items...\n");
89 for (i = 0; i < tm->nitems; i++)
90 {
91 kv.key = tm->keys[i];
Dave Barachc3799992016-08-15 11:12:27 -040092 kv.value = i + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Dave Barachc3799992016-08-15 11:12:27 -040094 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 if (tm->verbose > 1)
Dave Barachc3799992016-08-15 11:12:27 -040097 {
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 */ );
103 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 }
105
Dave Barachc3799992016-08-15 11:12:27 -0400106 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
108 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
109
110 before = clib_time_now (&tm->clib_time);
111
112 for (j = 0; j < tm->search_iter; j++)
113 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 for (i = 0; i < tm->nitems; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400115 {
Dave Barachc3799992016-08-15 11:12:27 -0400116 kv.key = tm->keys[i];
117 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
Dave Barach5e6b9582016-12-12 15:37:29 -0500118 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
119 clib_warning ("[%d] search for key %lld failed unexpectedly\n",
120 i, tm->keys[i]);
Dave Barachc3799992016-08-15 11:12:27 -0400121 if (kv.value != (u64) (i + 1))
Dave Barach5e6b9582016-12-12 15:37:29 -0500122 clib_warning
123 ("[%d] search for key %lld returned %lld, not %lld\n", i,
124 tm->keys, kv.value, (u64) (i + 1));
Dave Barachc3799992016-08-15 11:12:27 -0400125 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 }
127
128 delta = clib_time_now (&tm->clib_time) - before;
Dave Barachc3799992016-08-15 11:12:27 -0400129 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131 if (delta > 0)
132 fformat (stdout, "%.f searches per second\n",
Dave Barachc3799992016-08-15 11:12:27 -0400133 ((f64) total_searches) / delta);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
136
Dave Barachc3799992016-08-15 11:12:27 -0400137 fformat (stdout, "Standard E-hash search for items %d times...\n",
138 tm->search_iter);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
140 before = clib_time_now (&tm->clib_time);
141
142 for (j = 0; j < tm->search_iter; j++)
143 {
144 for (i = 0; i < tm->nitems; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400145 {
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]);
149 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 }
151
152 delta = clib_time_now (&tm->clib_time) - before;
Dave Barachc3799992016-08-15 11:12:27 -0400153 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
155 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
156
157 if (delta > 0)
158 fformat (stdout, "%.f searches per second\n",
Dave Barachc3799992016-08-15 11:12:27 -0400159 ((f64) total_searches) / delta);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
161 fformat (stdout, "Delete items...\n");
162
163 for (i = 0; i < tm->nitems; i++)
164 {
165 int j;
166 int rv;
167
168 kv.key = tm->keys[i];
Dave Barachc3799992016-08-15 11:12:27 -0400169 kv.value = (u64) (i + 1);
170 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172 if (rv < 0)
Dave Barachc3799992016-08-15 11:12:27 -0400173 clib_warning ("delete key %lld not ok but should be", tm->keys[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
175 if (tm->careful_delete_tests)
Dave Barachc3799992016-08-15 11:12:27 -0400176 {
177 for (j = 0; j < tm->nitems; j++)
178 {
179 kv.key = tm->keys[j];
180 rv = BV (clib_bihash_search) (h, &kv, &kv);
181 if (j <= i && rv >= 0)
182 {
183 clib_warning
184 ("i %d j %d search ok but should not be, value %lld",
185 i, j, kv.value);
186 }
187 if (j > i && rv < 0)
188 {
189 clib_warning ("i %d j %d search not ok but should be",
190 i, j);
191 }
192 }
193 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 }
195
196 fformat (stdout, "After deletions, should be empty...\n");
197
Dave Barachc3799992016-08-15 11:12:27 -0400198 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 return 0;
200}
201
Dave Barachc3799992016-08-15 11:12:27 -0400202clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203test_bihash_main (test_main_t * tm)
204{
Dave Barachc3799992016-08-15 11:12:27 -0400205 unformat_input_t *i = tm->input;
206 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
208 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
209 {
210 if (unformat (i, "seed %u", &tm->seed))
Dave Barachc3799992016-08-15 11:12:27 -0400211 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
213 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
Dave Barachc3799992016-08-15 11:12:27 -0400214 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 else if (unformat (i, "non-random-keys"))
Dave Barachc3799992016-08-15 11:12:27 -0400216 tm->non_random_keys = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 else if (unformat (i, "nitems %d", &tm->nitems))
Dave Barachc3799992016-08-15 11:12:27 -0400218 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
Dave Barachc3799992016-08-15 11:12:27 -0400220 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221 else if (unformat (i, "verbose %d", &tm->verbose))
Dave Barachc3799992016-08-15 11:12:27 -0400222 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 else if (unformat (i, "search %d", &tm->search_iter))
Dave Barachc3799992016-08-15 11:12:27 -0400224 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 else if (unformat (i, "verbose"))
Dave Barachc3799992016-08-15 11:12:27 -0400226 tm->verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227 else
Dave Barachc3799992016-08-15 11:12:27 -0400228 return clib_error_return (0, "unknown input '%U'",
229 format_unformat_error, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 }
Dave Barachc3799992016-08-15 11:12:27 -0400231
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 error = test_bihash (tm);
233
234 return error;
235}
236
237#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400238int
239main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240{
241 unformat_input_t i;
Dave Barachc3799992016-08-15 11:12:27 -0400242 clib_error_t *error;
243 test_main_t *tm = &test_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
Dave Barachc3799992016-08-15 11:12:27 -0400245 clib_mem_init (0, 3ULL << 30);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
247 tm->input = &i;
248 tm->seed = 0xdeaddabe;
249
250 tm->nbuckets = 2;
251 tm->nitems = 5;
252 tm->verbose = 1;
253 tm->search_iter = 1;
254 tm->careful_delete_tests = 0;
255 tm->key_hash = hash_create (0, sizeof (uword));
256 clib_time_init (&tm->clib_time);
257
258 unformat_init_command_line (&i, argv);
259 error = test_bihash_main (tm);
260 unformat_free (&i);
261
262 if (error)
263 {
264 clib_error_report (error);
265 return 1;
266 }
267 return 0;
268}
269#endif /* CLIB_UNIX */
Dave Barachc3799992016-08-15 11:12:27 -0400270
271/*
272 * fd.io coding-style-patch-verification: ON
273 *
274 * Local Variables:
275 * eval: (c-set-style "gnu")
276 * End:
277 */