1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 #include <boost/test/unit_test.hpp>
5 #include "cuckoocache.h"
6 #include "test/test_bitcoin.h"
9 #include <boost/thread.hpp>
12 /** Test Suite for CuckooCache
14 * 1) All tests should have a deterministic result (using insecure rand
15 * with deterministic seeds)
16 * 2) Some test methods are templated to allow for easier testing
17 * against new versions / comparing
18 * 3) Results should be treated as a regression test, i.e., did the behavior
19 * change significantly from what was expected. This can be OK, depending on
20 * the nature of the change, but requires updating the tests to reflect the new
21 * expected behavior. For example improving the hit rate may cause some tests
22 * using BOOST_CHECK_CLOSE to fail.
25 FastRandomContext
insecure_rand(true);
27 BOOST_AUTO_TEST_SUITE(cuckoocache_tests
);
30 /** insecure_GetRandHash fills in a uint256 from insecure_rand
32 void insecure_GetRandHash(uint256
& t
)
34 uint32_t* ptr
= (uint32_t*)t
.begin();
35 for (uint8_t j
= 0; j
< 8; ++j
)
36 *(ptr
++) = insecure_rand
.rand32();
39 /** Definition copied from /src/script/sigcache.cpp
44 template <uint8_t hash_select
>
45 uint32_t operator()(const uint256
& key
) const
47 static_assert(hash_select
<8, "SignatureCacheHasher only has 8 hashes available.");
49 std::memcpy(&u
, key
.begin() + 4 * hash_select
, 4);
55 /* Test that no values not inserted into the cache are read out of it.
57 * There are no repeats in the first 200000 insecure_GetRandHash calls
59 BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes
)
61 insecure_rand
= FastRandomContext(true);
62 CuckooCache::cache
<uint256
, uint256Hasher
> cc
{};
63 cc
.setup_bytes(32 << 20);
65 for (int x
= 0; x
< 100000; ++x
) {
66 insecure_GetRandHash(v
);
69 for (int x
= 0; x
< 100000; ++x
) {
70 insecure_GetRandHash(v
);
71 BOOST_CHECK(!cc
.contains(v
, false));
75 /** This helper returns the hit rate when megabytes*load worth of entries are
76 * inserted into a megabytes sized cache
78 template <typename Cache
>
79 double test_cache(size_t megabytes
, double load
)
81 insecure_rand
= FastRandomContext(true);
82 std::vector
<uint256
> hashes
;
84 size_t bytes
= megabytes
* (1 << 20);
85 set
.setup_bytes(bytes
);
86 uint32_t n_insert
= static_cast<uint32_t>(load
* (bytes
/ sizeof(uint256
)));
87 hashes
.resize(n_insert
);
88 for (uint32_t i
= 0; i
< n_insert
; ++i
) {
89 uint32_t* ptr
= (uint32_t*)hashes
[i
].begin();
90 for (uint8_t j
= 0; j
< 8; ++j
)
91 *(ptr
++) = insecure_rand
.rand32();
93 /** We make a copy of the hashes because future optimizations of the
94 * cuckoocache may overwrite the inserted element, so the test is
97 std::vector
<uint256
> hashes_insert_copy
= hashes
;
99 for (uint256
& h
: hashes_insert_copy
)
101 /** Count the hits */
103 for (uint256
& h
: hashes
)
104 count
+= set
.contains(h
, false);
105 double hit_rate
= ((double)count
) / ((double)n_insert
);
109 /** The normalized hit rate for a given load.
111 * The semantics are a little confusing, so please see the below
116 * 1) at load 0.5, we expect a perfect hit rate, so we multiply by
118 * 2) at load 2.0, we expect to see half the entries, so a perfect hit rate
119 * would be 0.5. Therefore, if we see a hit rate of 0.4, 0.4*2.0 = 0.8 is the
120 * normalized hit rate.
122 * This is basically the right semantics, but has a bit of a glitch depending on
123 * how you measure around load 1.0 as after load 1.0 your normalized hit rate
124 * becomes effectively perfect, ignoring freshness.
126 double normalize_hit_rate(double hits
, double load
)
128 return hits
* std::max(load
, 1.0);
131 /** Check the hit rate on loads ranging from 0.1 to 2.0 */
132 BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok
)
134 /** Arbitrarily selected Hit Rate threshold that happens to work for this test
135 * as a lower bound on performance.
137 double HitRateThresh
= 0.98;
138 size_t megabytes
= 32;
139 for (double load
= 0.1; load
< 2; load
*= 2) {
140 double hits
= test_cache
<CuckooCache::cache
<uint256
, uint256Hasher
>>(megabytes
, load
);
141 BOOST_CHECK(normalize_hit_rate(hits
, load
) > HitRateThresh
);
146 /** This helper checks that erased elements are preferentially inserted onto and
147 * that the hit rate of "fresher" keys is reasonable*/
148 template <typename Cache
>
149 void test_cache_erase(size_t megabytes
)
152 insecure_rand
= FastRandomContext(true);
153 std::vector
<uint256
> hashes
;
155 size_t bytes
= megabytes
* (1 << 20);
156 set
.setup_bytes(bytes
);
157 uint32_t n_insert
= static_cast<uint32_t>(load
* (bytes
/ sizeof(uint256
)));
158 hashes
.resize(n_insert
);
159 for (uint32_t i
= 0; i
< n_insert
; ++i
) {
160 uint32_t* ptr
= (uint32_t*)hashes
[i
].begin();
161 for (uint8_t j
= 0; j
< 8; ++j
)
162 *(ptr
++) = insecure_rand
.rand32();
164 /** We make a copy of the hashes because future optimizations of the
165 * cuckoocache may overwrite the inserted element, so the test is
168 std::vector
<uint256
> hashes_insert_copy
= hashes
;
170 /** Insert the first half */
171 for (uint32_t i
= 0; i
< (n_insert
/ 2); ++i
)
172 set
.insert(hashes_insert_copy
[i
]);
173 /** Erase the first quarter */
174 for (uint32_t i
= 0; i
< (n_insert
/ 4); ++i
)
175 set
.contains(hashes
[i
], true);
176 /** Insert the second half */
177 for (uint32_t i
= (n_insert
/ 2); i
< n_insert
; ++i
)
178 set
.insert(hashes_insert_copy
[i
]);
180 /** elements that we marked erased but that are still there */
181 size_t count_erased_but_contained
= 0;
182 /** elements that we did not erase but are older */
183 size_t count_stale
= 0;
184 /** elements that were most recently inserted */
185 size_t count_fresh
= 0;
187 for (uint32_t i
= 0; i
< (n_insert
/ 4); ++i
)
188 count_erased_but_contained
+= set
.contains(hashes
[i
], false);
189 for (uint32_t i
= (n_insert
/ 4); i
< (n_insert
/ 2); ++i
)
190 count_stale
+= set
.contains(hashes
[i
], false);
191 for (uint32_t i
= (n_insert
/ 2); i
< n_insert
; ++i
)
192 count_fresh
+= set
.contains(hashes
[i
], false);
194 double hit_rate_erased_but_contained
= double(count_erased_but_contained
) / (double(n_insert
) / 4.0);
195 double hit_rate_stale
= double(count_stale
) / (double(n_insert
) / 4.0);
196 double hit_rate_fresh
= double(count_fresh
) / (double(n_insert
) / 2.0);
198 // Check that our hit_rate_fresh is perfect
199 BOOST_CHECK_EQUAL(hit_rate_fresh
, 1.0);
200 // Check that we have a more than 2x better hit rate on stale elements than
202 BOOST_CHECK(hit_rate_stale
> 2 * hit_rate_erased_but_contained
);
205 BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok
)
207 size_t megabytes
= 32;
208 test_cache_erase
<CuckooCache::cache
<uint256
, uint256Hasher
>>(megabytes
);
211 template <typename Cache
>
212 void test_cache_erase_parallel(size_t megabytes
)
215 insecure_rand
= FastRandomContext(true);
216 std::vector
<uint256
> hashes
;
218 size_t bytes
= megabytes
* (1 << 20);
219 set
.setup_bytes(bytes
);
220 uint32_t n_insert
= static_cast<uint32_t>(load
* (bytes
/ sizeof(uint256
)));
221 hashes
.resize(n_insert
);
222 for (uint32_t i
= 0; i
< n_insert
; ++i
) {
223 uint32_t* ptr
= (uint32_t*)hashes
[i
].begin();
224 for (uint8_t j
= 0; j
< 8; ++j
)
225 *(ptr
++) = insecure_rand
.rand32();
227 /** We make a copy of the hashes because future optimizations of the
228 * cuckoocache may overwrite the inserted element, so the test is
231 std::vector
<uint256
> hashes_insert_copy
= hashes
;
232 boost::shared_mutex mtx
;
235 /** Grab lock to make sure we release inserts */
236 boost::unique_lock
<boost::shared_mutex
> l(mtx
);
237 /** Insert the first half */
238 for (uint32_t i
= 0; i
< (n_insert
/ 2); ++i
)
239 set
.insert(hashes_insert_copy
[i
]);
242 /** Spin up 3 threads to run contains with erase.
244 std::vector
<std::thread
> threads
;
245 /** Erase the first quarter */
246 for (uint32_t x
= 0; x
< 3; ++x
)
247 /** Each thread is emplaced with x copy-by-value
249 threads
.emplace_back([&, x
] {
250 boost::shared_lock
<boost::shared_mutex
> l(mtx
);
251 size_t ntodo
= (n_insert
/4)/3;
252 size_t start
= ntodo
*x
;
253 size_t end
= ntodo
*(x
+1);
254 for (uint32_t i
= start
; i
< end
; ++i
)
255 set
.contains(hashes
[i
], true);
258 /** Wait for all threads to finish
260 for (std::thread
& t
: threads
)
262 /** Grab lock to make sure we observe erases */
263 boost::unique_lock
<boost::shared_mutex
> l(mtx
);
264 /** Insert the second half */
265 for (uint32_t i
= (n_insert
/ 2); i
< n_insert
; ++i
)
266 set
.insert(hashes_insert_copy
[i
]);
268 /** elements that we marked erased but that are still there */
269 size_t count_erased_but_contained
= 0;
270 /** elements that we did not erase but are older */
271 size_t count_stale
= 0;
272 /** elements that were most recently inserted */
273 size_t count_fresh
= 0;
275 for (uint32_t i
= 0; i
< (n_insert
/ 4); ++i
)
276 count_erased_but_contained
+= set
.contains(hashes
[i
], false);
277 for (uint32_t i
= (n_insert
/ 4); i
< (n_insert
/ 2); ++i
)
278 count_stale
+= set
.contains(hashes
[i
], false);
279 for (uint32_t i
= (n_insert
/ 2); i
< n_insert
; ++i
)
280 count_fresh
+= set
.contains(hashes
[i
], false);
282 double hit_rate_erased_but_contained
= double(count_erased_but_contained
) / (double(n_insert
) / 4.0);
283 double hit_rate_stale
= double(count_stale
) / (double(n_insert
) / 4.0);
284 double hit_rate_fresh
= double(count_fresh
) / (double(n_insert
) / 2.0);
286 // Check that our hit_rate_fresh is perfect
287 BOOST_CHECK_EQUAL(hit_rate_fresh
, 1.0);
288 // Check that we have a more than 2x better hit rate on stale elements than
290 BOOST_CHECK(hit_rate_stale
> 2 * hit_rate_erased_but_contained
);
292 BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok
)
294 size_t megabytes
= 32;
295 test_cache_erase_parallel
<CuckooCache::cache
<uint256
, uint256Hasher
>>(megabytes
);
299 template <typename Cache
>
300 void test_cache_generations()
302 // This test checks that for a simulation of network activity, the fresh hit
303 // rate is never below 99%, and the number of times that it is worse than
304 // 99.9% are less than 1% of the time.
305 double min_hit_rate
= 0.99;
306 double tight_hit_rate
= 0.999;
307 double max_rate_less_than_tight_hit_rate
= 0.01;
308 // A cache that meets this specification is therefore shown to have a hit
309 // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) +
310 // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == 99.89%
311 // hit rate with low variance.
313 // We use deterministic values, but this test has also passed on many
314 // iterations with non-deterministic values, so it isn't "overfit" to the
315 // specific entropy in FastRandomContext(true) and implementation of the
317 insecure_rand
= FastRandomContext(true);
319 // block_activity models a chunk of network activity. n_insert elements are
320 // adde to the cache. The first and last n/4 are stored for removal later
321 // and the middle n/2 are not stored. This models a network which uses half
322 // the signatures of recently (since the last block) added transactions
323 // immediately and never uses the other half.
324 struct block_activity
{
325 std::vector
<uint256
> reads
;
326 block_activity(uint32_t n_insert
, Cache
& c
) : reads()
328 std::vector
<uint256
> inserts
;
329 inserts
.resize(n_insert
);
330 reads
.reserve(n_insert
/ 2);
331 for (uint32_t i
= 0; i
< n_insert
; ++i
) {
332 uint32_t* ptr
= (uint32_t*)inserts
[i
].begin();
333 for (uint8_t j
= 0; j
< 8; ++j
)
334 *(ptr
++) = insecure_rand
.rand32();
336 for (uint32_t i
= 0; i
< n_insert
/ 4; ++i
)
337 reads
.push_back(inserts
[i
]);
338 for (uint32_t i
= n_insert
- (n_insert
/ 4); i
< n_insert
; ++i
)
339 reads
.push_back(inserts
[i
]);
340 for (auto h
: inserts
)
345 const uint32_t BLOCK_SIZE
= 10000;
346 // We expect window size 60 to perform reasonably given that each epoch
347 // stores 45% of the cache size (~472k).
348 const uint32_t WINDOW_SIZE
= 60;
349 const uint32_t POP_AMOUNT
= (BLOCK_SIZE
/ WINDOW_SIZE
) / 2;
350 const double load
= 10;
351 const size_t megabytes
= 32;
352 const size_t bytes
= megabytes
* (1 << 20);
353 const uint32_t n_insert
= static_cast<uint32_t>(load
* (bytes
/ sizeof(uint256
)));
355 std::vector
<block_activity
> hashes
;
357 set
.setup_bytes(bytes
);
358 hashes
.reserve(n_insert
/ BLOCK_SIZE
);
359 std::deque
<block_activity
> last_few
;
360 uint32_t out_of_tight_tolerance
= 0;
361 uint32_t total
= n_insert
/ BLOCK_SIZE
;
362 // we use the deque last_few to model a sliding window of blocks. at each
363 // step, each of the last WINDOW_SIZE block_activities checks the cache for
364 // POP_AMOUNT of the hashes that they inserted, and marks these erased.
365 for (uint32_t i
= 0; i
< total
; ++i
) {
366 if (last_few
.size() == WINDOW_SIZE
)
367 last_few
.pop_front();
368 last_few
.emplace_back(BLOCK_SIZE
, set
);
370 for (auto& act
: last_few
)
371 for (uint32_t k
= 0; k
< POP_AMOUNT
; ++k
) {
372 count
+= set
.contains(act
.reads
.back(), true);
373 act
.reads
.pop_back();
375 // We use last_few.size() rather than WINDOW_SIZE for the correct
376 // behavior on the first WINDOW_SIZE iterations where the deque is not
378 double hit
= (double(count
)) / (last_few
.size() * POP_AMOUNT
);
379 // Loose Check that hit rate is above min_hit_rate
380 BOOST_CHECK(hit
> min_hit_rate
);
381 // Tighter check, count number of times we are less than tight_hit_rate
382 // (and implicityly, greater than min_hit_rate)
383 out_of_tight_tolerance
+= hit
< tight_hit_rate
;
385 // Check that being out of tolerance happens less than
386 // max_rate_less_than_tight_hit_rate of the time
387 BOOST_CHECK(double(out_of_tight_tolerance
) / double(total
) < max_rate_less_than_tight_hit_rate
);
389 BOOST_AUTO_TEST_CASE(cuckoocache_generations
)
391 test_cache_generations
<CuckooCache::cache
<uint256
, uint256Hasher
>>();
394 BOOST_AUTO_TEST_SUITE_END();