| 275 | |
| 276 | template <typename Cache> |
| 277 | static void test_cache_generations() |
| 278 | { |
| 279 | // This test checks that for a simulation of network activity, the fresh hit |
| 280 | // rate is never below 99%, and the number of times that it is worse than |
| 281 | // 99.9% are less than 1% of the time. |
| 282 | double min_hit_rate = 0.99; |
| 283 | double tight_hit_rate = 0.999; |
| 284 | double max_rate_less_than_tight_hit_rate = 0.01; |
| 285 | // A cache that meets this specification is therefore shown to have a hit |
| 286 | // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) + |
| 287 | // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == 99.89% |
| 288 | // hit rate with low variance. |
| 289 | |
| 290 | // We use deterministic values, but this test has also passed on many |
| 291 | // iterations with non-deterministic values, so it isn't "overfit" to the |
| 292 | // specific entropy in FastRandomContext(true) and implementation of the |
| 293 | // cache. |
| 294 | SeedInsecureRand(SeedRand::ZEROS); |
| 295 | |
| 296 | // block_activity models a chunk of network activity. n_insert elements are |
| 297 | // added to the cache. The first and last n/4 are stored for removal later |
| 298 | // and the middle n/2 are not stored. This models a network which uses half |
| 299 | // the signatures of recently (since the last block) added transactions |
| 300 | // immediately and never uses the other half. |
| 301 | struct block_activity { |
| 302 | std::vector<uint256> reads; |
| 303 | block_activity(uint32_t n_insert, Cache& c) : reads() |
| 304 | { |
| 305 | std::vector<uint256> inserts; |
| 306 | inserts.resize(n_insert); |
| 307 | reads.reserve(n_insert / 2); |
| 308 | for (uint32_t i = 0; i < n_insert; ++i) { |
| 309 | uint32_t* ptr = (uint32_t*)inserts[i].begin(); |
| 310 | for (uint8_t j = 0; j < 8; ++j) |
| 311 | *(ptr++) = InsecureRand32(); |
| 312 | } |
| 313 | for (uint32_t i = 0; i < n_insert / 4; ++i) |
| 314 | reads.push_back(inserts[i]); |
| 315 | for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i) |
| 316 | reads.push_back(inserts[i]); |
| 317 | for (const auto& h : inserts) |
| 318 | c.insert(h); |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | const uint32_t BLOCK_SIZE = 1000; |
| 323 | // We expect window size 60 to perform reasonably given that each epoch |
| 324 | // stores 45% of the cache size (~472k). |
| 325 | const uint32_t WINDOW_SIZE = 60; |
| 326 | const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2; |
| 327 | const double load = 10; |
| 328 | const size_t megabytes = 4; |
| 329 | const size_t bytes = megabytes * (1 << 20); |
| 330 | const uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); |
| 331 | |
| 332 | std::vector<block_activity> hashes; |
| 333 | Cache set{}; |
| 334 | set.setup_bytes(bytes); |
nothing calls this directly
no test coverage detected