11 #include <boost/test/unit_test.hpp>
15 #include <shared_mutex>
44 for (
int x = 0; x < 100000; ++x) {
47 for (
int x = 0; x < 100000; ++x) {
55 template <
typename Cache>
59 std::vector<uint256> hashes;
61 size_t bytes = megabytes * (1 << 20);
62 set.setup_bytes(bytes);
63 uint32_t n_insert =
static_cast<uint32_t
>(load * (bytes /
sizeof(
uint256)));
64 hashes.resize(n_insert);
65 for (uint32_t i = 0; i < n_insert; ++i) {
66 uint32_t* ptr = (uint32_t*)hashes[i].begin();
67 for (uint8_t j = 0; j < 8; ++j)
74 std::vector<uint256> hashes_insert_copy = hashes;
76 for (
const uint256& h : hashes_insert_copy)
81 count += set.contains(h,
false);
82 double hit_rate = ((double)
count) / ((double)n_insert);
105 return hits * std::max(load, 1.0);
114 double HitRateThresh = 0.98;
115 size_t megabytes = 4;
116 for (
double load = 0.1; load < 2; load *= 2) {
117 double hits = test_cache<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes, load);
125 template <
typename Cache>
130 std::vector<uint256> hashes;
132 size_t bytes = megabytes * (1 << 20);
133 set.setup_bytes(bytes);
134 uint32_t n_insert =
static_cast<uint32_t
>(load * (bytes /
sizeof(
uint256)));
135 hashes.resize(n_insert);
136 for (uint32_t i = 0; i < n_insert; ++i) {
137 uint32_t* ptr = (uint32_t*)hashes[i].begin();
138 for (uint8_t j = 0; j < 8; ++j)
145 std::vector<uint256> hashes_insert_copy = hashes;
148 for (uint32_t i = 0; i < (n_insert / 2); ++i)
149 set.insert(hashes_insert_copy[i]);
151 for (uint32_t i = 0; i < (n_insert / 4); ++i)
154 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
155 set.insert(hashes_insert_copy[i]);
158 size_t count_erased_but_contained = 0;
160 size_t count_stale = 0;
162 size_t count_fresh = 0;
164 for (uint32_t i = 0; i < (n_insert / 4); ++i)
165 count_erased_but_contained += set.contains(hashes[i],
false);
166 for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
167 count_stale += set.contains(hashes[i],
false);
168 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
169 count_fresh += set.contains(hashes[i],
false);
171 double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
172 double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
173 double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
179 BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
184 size_t megabytes = 4;
185 test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
188 template <
typename Cache>
193 std::vector<uint256> hashes;
195 size_t bytes = megabytes * (1 << 20);
196 set.setup_bytes(bytes);
197 uint32_t n_insert =
static_cast<uint32_t
>(load * (bytes /
sizeof(
uint256)));
198 hashes.resize(n_insert);
199 for (uint32_t i = 0; i < n_insert; ++i) {
200 uint32_t* ptr = (uint32_t*)hashes[i].begin();
201 for (uint8_t j = 0; j < 8; ++j)
208 std::vector<uint256> hashes_insert_copy = hashes;
209 std::shared_mutex mtx;
213 std::unique_lock<std::shared_mutex> l(mtx);
215 for (uint32_t i = 0; i < (n_insert / 2); ++i)
216 set.insert(hashes_insert_copy[i]);
221 std::vector<std::thread> threads;
223 for (uint32_t x = 0; x < 3; ++x)
226 threads.emplace_back([&, x] {
227 std::shared_lock<std::shared_mutex> l(mtx);
228 size_t ntodo = (n_insert/4)/3;
229 size_t start = ntodo*x;
230 size_t end = ntodo*(x+1);
231 for (uint32_t i = start; i < end; ++i) {
232 bool contains = set.contains(hashes[i],
true);
239 for (std::thread& t : threads)
242 std::unique_lock<std::shared_mutex> l(mtx);
244 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
245 set.insert(hashes_insert_copy[i]);
248 size_t count_erased_but_contained = 0;
250 size_t count_stale = 0;
252 size_t count_fresh = 0;
254 for (uint32_t i = 0; i < (n_insert / 4); ++i)
255 count_erased_but_contained += set.contains(hashes[i],
false);
256 for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
257 count_stale += set.contains(hashes[i],
false);
258 for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
259 count_fresh += set.contains(hashes[i],
false);
261 double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
262 double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
263 double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
269 BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
273 size_t megabytes = 4;
274 test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
278 template <
typename Cache>
284 double min_hit_rate = 0.99;
285 double tight_hit_rate = 0.999;
286 double max_rate_less_than_tight_hit_rate = 0.01;
303 struct block_activity {
304 std::vector<uint256> reads;
305 block_activity(uint32_t n_insert, Cache& c) : reads()
307 std::vector<uint256> inserts;
308 inserts.resize(n_insert);
309 reads.reserve(n_insert / 2);
310 for (uint32_t i = 0; i < n_insert; ++i) {
311 uint32_t* ptr = (uint32_t*)inserts[i].begin();
312 for (uint8_t j = 0; j < 8; ++j)
315 for (uint32_t i = 0; i < n_insert / 4; ++i)
316 reads.push_back(inserts[i]);
317 for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i)
318 reads.push_back(inserts[i]);
319 for (
const auto& h : inserts)
324 const uint32_t BLOCK_SIZE = 1000;
327 const uint32_t WINDOW_SIZE = 60;
328 const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2;
329 const double load = 10;
330 const size_t megabytes = 4;
331 const size_t bytes = megabytes * (1 << 20);
332 const uint32_t n_insert =
static_cast<uint32_t
>(load * (bytes /
sizeof(
uint256)));
334 std::vector<block_activity> hashes;
336 set.setup_bytes(bytes);
337 hashes.reserve(n_insert / BLOCK_SIZE);
338 std::deque<block_activity> last_few;
339 uint32_t out_of_tight_tolerance = 0;
340 uint32_t total = n_insert / BLOCK_SIZE;
344 for (uint32_t i = 0; i < total; ++i) {
345 if (last_few.size() == WINDOW_SIZE)
346 last_few.pop_front();
347 last_few.emplace_back(BLOCK_SIZE, set);
349 for (
auto& act : last_few)
350 for (uint32_t
k = 0;
k < POP_AMOUNT; ++
k) {
351 count += set.contains(act.reads.back(),
true);
352 act.reads.pop_back();
357 double hit = (double(
count)) / (last_few.size() * POP_AMOUNT);
362 out_of_tight_tolerance += hit < tight_hit_rate;
366 BOOST_CHECK(
double(out_of_tight_tolerance) /
double(total) < max_rate_less_than_tight_hit_rate);
370 test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
cache implements a cache with properties similar to a cuckoo-set.
std::optional< 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...
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
static void test_cache_erase_parallel(size_t megabytes)
static void test_cache_erase(size_t megabytes)
This helper checks that erased elements are preferentially inserted onto and that the hit rate of "fr...
static void test_cache_generations()
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static 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.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
@ ZEROS
Seed with a compile time constant of zeros.
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
static uint256 InsecureRand256()
static uint32_t InsecureRand32()