| 299 | |
| 300 | template <typename Cache> |
| 301 | void test_cache_generations() |
| 302 | { |
| 303 | // This test checks that for a simulation of network activity, the fresh hit |
| 304 | // rate is never below 99%, and the number of times that it is worse than |
| 305 | // 99.9% are less than 1% of the time. |
| 306 | double min_hit_rate = 0.99; |
| 307 | double tight_hit_rate = 0.999; |
| 308 | double max_rate_less_than_tight_hit_rate = 0.01; |
| 309 | // A cache that meets this specification is therefore shown to have a hit |
| 310 | // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) + |
| 311 | // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == 99.89% |
| 312 | // hit rate with low variance. |
| 313 | |
| 314 | // We use deterministic values, but this test has also passed on many |
| 315 | // iterations with non-deterministic values, so it isn't "overfit" to the |
| 316 | // specific entropy in FastRandomContext(true) and implementation of the |
| 317 | // cache. |
| 318 | insecure_rand = FastRandomContext(true); |
| 319 | |
| 320 | // block_activity models a chunk of network activity. n_insert elements are |
| 321 | // adde to the cache. The first and last n/4 are stored for removal later |
| 322 | // and the middle n/2 are not stored. This models a network which uses half |
| 323 | // the signatures of recently (since the last block) added transactions |
| 324 | // immediately and never uses the other half. |
| 325 | struct block_activity { |
| 326 | std::vector<uint256> reads; |
| 327 | block_activity(uint32_t n_insert, Cache& c) : reads() |
| 328 | { |
| 329 | std::vector<uint256> inserts; |
| 330 | inserts.resize(n_insert); |
| 331 | reads.reserve(n_insert / 2); |
| 332 | for (uint32_t i = 0; i < n_insert; ++i) { |
| 333 | uint32_t* ptr = (uint32_t*)inserts[i].begin(); |
| 334 | for (uint8_t j = 0; j < 8; ++j) |
| 335 | *(ptr++) = insecure_rand.rand32(); |
| 336 | } |
| 337 | for (uint32_t i = 0; i < n_insert / 4; ++i) |
| 338 | reads.push_back(inserts[i]); |
| 339 | for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i) |
| 340 | reads.push_back(inserts[i]); |
| 341 | for (auto h : inserts) |
| 342 | c.insert(h); |
| 343 | } |
| 344 | }; |
| 345 | |
| 346 | const uint32_t BLOCK_SIZE = 10000; |
| 347 | // We expect window size 60 to perform reasonably given that each epoch |
| 348 | // stores 45% of the cache size (~472k). |
| 349 | const uint32_t WINDOW_SIZE = 60; |
| 350 | const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2; |
| 351 | const double load = 10; |
| 352 | const size_t megabytes = 32; |
| 353 | const size_t bytes = megabytes * (1 << 20); |
| 354 | const uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); |
| 355 | |
| 356 | std::vector<block_activity> hashes; |
| 357 | Cache set{}; |
| 358 | set.setup_bytes(bytes); |