11#include <boost/test/unit_test.hpp>
15#include <shared_mutex>
44 for (
int x = 0; x < 100000; ++x) {
45 cc.insert(m_rng.rand256());
47 for (
int x = 0; x < 100000; ++x) {
56template <
typename Cache>
60 std::vector<uint256> hashes;
62 size_t bytes = megabytes * (1 << 20);
63 set.setup_bytes(bytes);
64 uint32_t n_insert =
static_cast<uint32_t
>(load * (bytes /
sizeof(
uint256)));
65 hashes.resize(n_insert);
66 for (uint32_t i = 0; i < n_insert; ++i) {
67 uint32_t* ptr = (uint32_t*)hashes[i].begin();
68 for (uint8_t j = 0; j < 8; ++j)
75 std::vector<uint256> hashes_insert_copy = hashes;
77 for (
const uint256& h : hashes_insert_copy)
82 count += set.contains(h,
false);
83 double hit_rate = ((double)
count) / ((double)n_insert);
106 return hits * std::max(load, 1.0);
116 double HitRateThresh = 0.98;
117 size_t megabytes = 4;
118 for (
double load = 0.1; load < 2; load *= 2) {
119 double hits = test_cache<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes, load);
120 BOOST_CHECK(normalize_hit_rate(hits, load) > HitRateThresh);
128template <
typename Cache>
133 std::vector<uint256> hashes;
135 size_t bytes = megabytes * (1 << 20);
136 set.setup_bytes(bytes);
137 uint32_t n_insert =
static_cast<uint32_t
>(load * (bytes /
sizeof(
uint256)));
138 hashes.resize(n_insert);
139 for (uint32_t i = 0; i < n_insert; ++i) {
140 uint32_t* ptr = (uint32_t*)hashes[i].begin();
141 for (uint8_t j = 0; j < 8; ++j)
148 std::vector<uint256> hashes_insert_copy = hashes;
151 for (uint32_t i = 0; i < (n_insert / 2); ++i)
152 set.insert(hashes_insert_copy[i]);
154 for (uint32_t i = 0; i < (n_insert / 4); ++i)
157 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
158 set.insert(hashes_insert_copy[i]);
161 size_t count_erased_but_contained = 0;
163 size_t count_stale = 0;
165 size_t count_fresh = 0;
167 for (uint32_t i = 0; i < (n_insert / 4); ++i)
168 count_erased_but_contained += set.contains(hashes[i],
false);
169 for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
170 count_stale += set.contains(hashes[i],
false);
171 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
172 count_fresh += set.contains(hashes[i],
false);
174 double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
175 double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
176 double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
182 BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
188 size_t megabytes = 4;
189 test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
193template <
typename Cache>
198 std::vector<uint256> hashes;
200 size_t bytes = megabytes * (1 << 20);
201 set.setup_bytes(bytes);
202 uint32_t n_insert =
static_cast<uint32_t
>(load * (bytes /
sizeof(
uint256)));
203 hashes.resize(n_insert);
204 for (uint32_t i = 0; i < n_insert; ++i) {
205 uint32_t* ptr = (uint32_t*)hashes[i].begin();
206 for (uint8_t j = 0; j < 8; ++j)
213 std::vector<uint256> hashes_insert_copy = hashes;
214 std::shared_mutex mtx;
218 std::unique_lock<std::shared_mutex> l(mtx);
220 for (uint32_t i = 0; i < (n_insert / 2); ++i)
221 set.insert(hashes_insert_copy[i]);
226 std::vector<std::thread> threads;
229 for (uint32_t x = 0; x < 3; ++x)
232 threads.emplace_back([&, x] {
233 std::shared_lock<std::shared_mutex> l(mtx);
234 size_t ntodo = (n_insert/4)/3;
235 size_t start = ntodo*x;
236 size_t end = ntodo*(x+1);
237 for (uint32_t i = start; i < end; ++i) {
238 bool contains = set.contains(hashes[i],
true);
245 for (std::thread&
t : threads)
248 std::unique_lock<std::shared_mutex> l(mtx);
250 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
251 set.insert(hashes_insert_copy[i]);
254 size_t count_erased_but_contained = 0;
256 size_t count_stale = 0;
258 size_t count_fresh = 0;
260 for (uint32_t i = 0; i < (n_insert / 4); ++i)
261 count_erased_but_contained += set.contains(hashes[i],
false);
262 for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
263 count_stale += set.contains(hashes[i],
false);
264 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
265 count_fresh += set.contains(hashes[i],
false);
267 double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
268 double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
269 double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
275 BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
280 size_t megabytes = 4;
281 test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
286template <
typename Cache>
292 double min_hit_rate = 0.99;
293 double tight_hit_rate = 0.999;
294 double max_rate_less_than_tight_hit_rate = 0.01;
311 struct block_activity {
312 std::vector<uint256> reads;
315 std::vector<uint256> inserts;
316 inserts.resize(n_insert);
317 reads.reserve(n_insert / 2);
318 for (uint32_t i = 0; i < n_insert; ++i) {
319 uint32_t* ptr = (uint32_t*)inserts[i].begin();
320 for (uint8_t j = 0; j < 8; ++j)
323 for (uint32_t i = 0; i < n_insert / 4; ++i)
324 reads.push_back(inserts[i]);
325 for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i)
326 reads.push_back(inserts[i]);
327 for (
const auto& h : inserts)
332 const uint32_t BLOCK_SIZE = 1000;
335 const uint32_t WINDOW_SIZE = 60;
336 const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2;
337 const double load = 10;
338 const size_t megabytes = 4;
339 const size_t bytes = megabytes * (1 << 20);
340 const uint32_t n_insert =
static_cast<uint32_t
>(load * (bytes /
sizeof(
uint256)));
342 std::vector<block_activity> hashes;
344 set.setup_bytes(bytes);
345 hashes.reserve(n_insert / BLOCK_SIZE);
346 std::deque<block_activity> last_few;
347 uint32_t out_of_tight_tolerance = 0;
348 uint32_t total = n_insert / BLOCK_SIZE;
352 for (uint32_t i = 0; i < total; ++i) {
353 if (last_few.size() == WINDOW_SIZE)
354 last_few.pop_front();
355 last_few.emplace_back(BLOCK_SIZE,
m_rng, set);
357 for (
auto& act : last_few)
358 for (uint32_t
k = 0;
k < POP_AMOUNT; ++
k) {
359 count += set.contains(act.reads.back(),
true);
360 act.reads.pop_back();
365 double hit = (double(
count)) / (last_few.size() * POP_AMOUNT);
370 out_of_tight_tolerance += hit < tight_hit_rate;
374 BOOST_CHECK(
double(out_of_tight_tolerance) /
double(total) < max_rate_less_than_tight_hit_rate);
379 test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
std::pair< uint32_t, size_t > setup_bytes(size_t bytes)
setup_bytes is a convenience function which accounts for internal memory usage when deciding how many...
uint32_t rand32() noexcept
Generate a random 32-bit integer.
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
BOOST_FIXTURE_TEST_CASE(cuckoocache_hit_rate_ok, HitRateTest)
Check the hit rate on loads ranging from 0.1 to 1.6.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
void SeedRandomForTest(SeedRand seed)
Seed the global RNG state and m_rng for testing and log the seed value.
void test_cache_erase_parallel(size_t megabytes)
void test_cache_erase(size_t megabytes)
This helper checks that erased elements are preferentially inserted onto and that the hit rate of "fr...
void test_cache_generations()
double test_cache(size_t megabytes, double load)
This helper returns the hit rate when megabytes*load worth of entries are inserted into a megabytes s...
static double normalize_hit_rate(double hits, double load)
The normalized hit rate for a given load.
@ ZEROS
Seed with a compile time constant of zeros.