Document assumptions that are being made to avoid NULL pointer dereferences
[bitcoinplatinum.git] / src / test / cuckoocache_tests.cpp
blob1004482224c8596cc3a35d862a5e1330c8bb9631
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 "script/sigcache.h"
7 #include "test/test_bitcoin.h"
8 #include "random.h"
9 #include <thread>
11 /** Test Suite for CuckooCache
13 * 1) All tests should have a deterministic result (using insecure rand
14 * with deterministic seeds)
15 * 2) Some test methods are templated to allow for easier testing
16 * against new versions / comparing
17 * 3) Results should be treated as a regression test, i.e., did the behavior
18 * change significantly from what was expected. This can be OK, depending on
19 * the nature of the change, but requires updating the tests to reflect the new
20 * expected behavior. For example improving the hit rate may cause some tests
21 * using BOOST_CHECK_CLOSE to fail.
24 FastRandomContext local_rand_ctx(true);
26 BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
29 /** insecure_GetRandHash fills in a uint256 from local_rand_ctx
31 void insecure_GetRandHash(uint256& t)
33 uint32_t* ptr = (uint32_t*)t.begin();
34 for (uint8_t j = 0; j < 8; ++j)
35 *(ptr++) = local_rand_ctx.rand32();
40 /* Test that no values not inserted into the cache are read out of it.
42 * There are no repeats in the first 200000 insecure_GetRandHash calls
44 BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
46 local_rand_ctx = FastRandomContext(true);
47 CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
48 size_t megabytes = 4;
49 cc.setup_bytes(megabytes << 20);
50 uint256 v;
51 for (int x = 0; x < 100000; ++x) {
52 insecure_GetRandHash(v);
53 cc.insert(v);
55 for (int x = 0; x < 100000; ++x) {
56 insecure_GetRandHash(v);
57 BOOST_CHECK(!cc.contains(v, false));
61 /** This helper returns the hit rate when megabytes*load worth of entries are
62 * inserted into a megabytes sized cache
64 template <typename Cache>
65 double test_cache(size_t megabytes, double load)
67 local_rand_ctx = FastRandomContext(true);
68 std::vector<uint256> hashes;
69 Cache set{};
70 size_t bytes = megabytes * (1 << 20);
71 set.setup_bytes(bytes);
72 uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
73 hashes.resize(n_insert);
74 for (uint32_t i = 0; i < n_insert; ++i) {
75 uint32_t* ptr = (uint32_t*)hashes[i].begin();
76 for (uint8_t j = 0; j < 8; ++j)
77 *(ptr++) = local_rand_ctx.rand32();
79 /** We make a copy of the hashes because future optimizations of the
80 * cuckoocache may overwrite the inserted element, so the test is
81 * "future proofed".
83 std::vector<uint256> hashes_insert_copy = hashes;
84 /** Do the insert */
85 for (uint256& h : hashes_insert_copy)
86 set.insert(h);
87 /** Count the hits */
88 uint32_t count = 0;
89 for (uint256& h : hashes)
90 count += set.contains(h, false);
91 double hit_rate = ((double)count) / ((double)n_insert);
92 return hit_rate;
95 /** The normalized hit rate for a given load.
97 * The semantics are a little confusing, so please see the below
98 * explanation.
100 * Examples:
102 * 1) at load 0.5, we expect a perfect hit rate, so we multiply by
103 * 1.0
104 * 2) at load 2.0, we expect to see half the entries, so a perfect hit rate
105 * would be 0.5. Therefore, if we see a hit rate of 0.4, 0.4*2.0 = 0.8 is the
106 * normalized hit rate.
108 * This is basically the right semantics, but has a bit of a glitch depending on
109 * how you measure around load 1.0 as after load 1.0 your normalized hit rate
110 * becomes effectively perfect, ignoring freshness.
112 double normalize_hit_rate(double hits, double load)
114 return hits * std::max(load, 1.0);
117 /** Check the hit rate on loads ranging from 0.1 to 2.0 */
118 BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
120 /** Arbitrarily selected Hit Rate threshold that happens to work for this test
121 * as a lower bound on performance.
123 double HitRateThresh = 0.98;
124 size_t megabytes = 4;
125 for (double load = 0.1; load < 2; load *= 2) {
126 double hits = test_cache<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes, load);
127 BOOST_CHECK(normalize_hit_rate(hits, load) > HitRateThresh);
132 /** This helper checks that erased elements are preferentially inserted onto and
133 * that the hit rate of "fresher" keys is reasonable*/
134 template <typename Cache>
135 void test_cache_erase(size_t megabytes)
137 double load = 1;
138 local_rand_ctx = FastRandomContext(true);
139 std::vector<uint256> hashes;
140 Cache set{};
141 size_t bytes = megabytes * (1 << 20);
142 set.setup_bytes(bytes);
143 uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
144 hashes.resize(n_insert);
145 for (uint32_t i = 0; i < n_insert; ++i) {
146 uint32_t* ptr = (uint32_t*)hashes[i].begin();
147 for (uint8_t j = 0; j < 8; ++j)
148 *(ptr++) = local_rand_ctx.rand32();
150 /** We make a copy of the hashes because future optimizations of the
151 * cuckoocache may overwrite the inserted element, so the test is
152 * "future proofed".
154 std::vector<uint256> hashes_insert_copy = hashes;
156 /** Insert the first half */
157 for (uint32_t i = 0; i < (n_insert / 2); ++i)
158 set.insert(hashes_insert_copy[i]);
159 /** Erase the first quarter */
160 for (uint32_t i = 0; i < (n_insert / 4); ++i)
161 set.contains(hashes[i], true);
162 /** Insert the second half */
163 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
164 set.insert(hashes_insert_copy[i]);
166 /** elements that we marked erased but that are still there */
167 size_t count_erased_but_contained = 0;
168 /** elements that we did not erase but are older */
169 size_t count_stale = 0;
170 /** elements that were most recently inserted */
171 size_t count_fresh = 0;
173 for (uint32_t i = 0; i < (n_insert / 4); ++i)
174 count_erased_but_contained += set.contains(hashes[i], false);
175 for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
176 count_stale += set.contains(hashes[i], false);
177 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
178 count_fresh += set.contains(hashes[i], false);
180 double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
181 double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
182 double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
184 // Check that our hit_rate_fresh is perfect
185 BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0);
186 // Check that we have a more than 2x better hit rate on stale elements than
187 // erased elements.
188 BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
191 BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok)
193 size_t megabytes = 4;
194 test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
197 template <typename Cache>
198 void test_cache_erase_parallel(size_t megabytes)
200 double load = 1;
201 local_rand_ctx = FastRandomContext(true);
202 std::vector<uint256> hashes;
203 Cache set{};
204 size_t bytes = megabytes * (1 << 20);
205 set.setup_bytes(bytes);
206 uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
207 hashes.resize(n_insert);
208 for (uint32_t i = 0; i < n_insert; ++i) {
209 uint32_t* ptr = (uint32_t*)hashes[i].begin();
210 for (uint8_t j = 0; j < 8; ++j)
211 *(ptr++) = local_rand_ctx.rand32();
213 /** We make a copy of the hashes because future optimizations of the
214 * cuckoocache may overwrite the inserted element, so the test is
215 * "future proofed".
217 std::vector<uint256> hashes_insert_copy = hashes;
218 boost::shared_mutex mtx;
221 /** Grab lock to make sure we release inserts */
222 boost::unique_lock<boost::shared_mutex> l(mtx);
223 /** Insert the first half */
224 for (uint32_t i = 0; i < (n_insert / 2); ++i)
225 set.insert(hashes_insert_copy[i]);
228 /** Spin up 3 threads to run contains with erase.
230 std::vector<std::thread> threads;
231 /** Erase the first quarter */
232 for (uint32_t x = 0; x < 3; ++x)
233 /** Each thread is emplaced with x copy-by-value
235 threads.emplace_back([&, x] {
236 boost::shared_lock<boost::shared_mutex> l(mtx);
237 size_t ntodo = (n_insert/4)/3;
238 size_t start = ntodo*x;
239 size_t end = ntodo*(x+1);
240 for (uint32_t i = start; i < end; ++i)
241 set.contains(hashes[i], true);
244 /** Wait for all threads to finish
246 for (std::thread& t : threads)
247 t.join();
248 /** Grab lock to make sure we observe erases */
249 boost::unique_lock<boost::shared_mutex> l(mtx);
250 /** Insert the second half */
251 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
252 set.insert(hashes_insert_copy[i]);
254 /** elements that we marked erased but that are still there */
255 size_t count_erased_but_contained = 0;
256 /** elements that we did not erase but are older */
257 size_t count_stale = 0;
258 /** elements that were most recently inserted */
259 size_t count_fresh = 0;
261 for (uint32_t i = 0; i < (n_insert / 4); ++i)
262 count_erased_but_contained += set.contains(hashes[i], false);
263 for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
264 count_stale += set.contains(hashes[i], false);
265 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
266 count_fresh += set.contains(hashes[i], false);
268 double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
269 double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
270 double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
272 // Check that our hit_rate_fresh is perfect
273 BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0);
274 // Check that we have a more than 2x better hit rate on stale elements than
275 // erased elements.
276 BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
278 BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok)
280 size_t megabytes = 4;
281 test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
285 template <typename Cache>
286 void test_cache_generations()
288 // This test checks that for a simulation of network activity, the fresh hit
289 // rate is never below 99%, and the number of times that it is worse than
290 // 99.9% are less than 1% of the time.
291 double min_hit_rate = 0.99;
292 double tight_hit_rate = 0.999;
293 double max_rate_less_than_tight_hit_rate = 0.01;
294 // A cache that meets this specification is therefore shown to have a hit
295 // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) +
296 // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == 99.89%
297 // hit rate with low variance.
299 // We use deterministic values, but this test has also passed on many
300 // iterations with non-deterministic values, so it isn't "overfit" to the
301 // specific entropy in FastRandomContext(true) and implementation of the
302 // cache.
303 local_rand_ctx = FastRandomContext(true);
305 // block_activity models a chunk of network activity. n_insert elements are
306 // adde to the cache. The first and last n/4 are stored for removal later
307 // and the middle n/2 are not stored. This models a network which uses half
308 // the signatures of recently (since the last block) added transactions
309 // immediately and never uses the other half.
310 struct block_activity {
311 std::vector<uint256> reads;
312 block_activity(uint32_t n_insert, Cache& c) : reads()
314 std::vector<uint256> inserts;
315 inserts.resize(n_insert);
316 reads.reserve(n_insert / 2);
317 for (uint32_t i = 0; i < n_insert; ++i) {
318 uint32_t* ptr = (uint32_t*)inserts[i].begin();
319 for (uint8_t j = 0; j < 8; ++j)
320 *(ptr++) = local_rand_ctx.rand32();
322 for (uint32_t i = 0; i < n_insert / 4; ++i)
323 reads.push_back(inserts[i]);
324 for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i)
325 reads.push_back(inserts[i]);
326 for (auto h : inserts)
327 c.insert(h);
331 const uint32_t BLOCK_SIZE = 1000;
332 // We expect window size 60 to perform reasonably given that each epoch
333 // stores 45% of the cache size (~472k).
334 const uint32_t WINDOW_SIZE = 60;
335 const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2;
336 const double load = 10;
337 const size_t megabytes = 4;
338 const size_t bytes = megabytes * (1 << 20);
339 const uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
341 std::vector<block_activity> hashes;
342 Cache set{};
343 set.setup_bytes(bytes);
344 hashes.reserve(n_insert / BLOCK_SIZE);
345 std::deque<block_activity> last_few;
346 uint32_t out_of_tight_tolerance = 0;
347 uint32_t total = n_insert / BLOCK_SIZE;
348 // we use the deque last_few to model a sliding window of blocks. at each
349 // step, each of the last WINDOW_SIZE block_activities checks the cache for
350 // POP_AMOUNT of the hashes that they inserted, and marks these erased.
351 for (uint32_t i = 0; i < total; ++i) {
352 if (last_few.size() == WINDOW_SIZE)
353 last_few.pop_front();
354 last_few.emplace_back(BLOCK_SIZE, set);
355 uint32_t count = 0;
356 for (auto& act : last_few)
357 for (uint32_t k = 0; k < POP_AMOUNT; ++k) {
358 count += set.contains(act.reads.back(), true);
359 act.reads.pop_back();
361 // We use last_few.size() rather than WINDOW_SIZE for the correct
362 // behavior on the first WINDOW_SIZE iterations where the deque is not
363 // full yet.
364 double hit = (double(count)) / (last_few.size() * POP_AMOUNT);
365 // Loose Check that hit rate is above min_hit_rate
366 BOOST_CHECK(hit > min_hit_rate);
367 // Tighter check, count number of times we are less than tight_hit_rate
368 // (and implicitly, greater than min_hit_rate)
369 out_of_tight_tolerance += hit < tight_hit_rate;
371 // Check that being out of tolerance happens less than
372 // max_rate_less_than_tight_hit_rate of the time
373 BOOST_CHECK(double(out_of_tight_tolerance) / double(total) < max_rate_less_than_tight_hit_rate);
375 BOOST_AUTO_TEST_CASE(cuckoocache_generations)
377 test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
380 BOOST_AUTO_TEST_SUITE_END();