MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / test_cache_generations

Function test_cache_generations

src/test/cuckoocache_tests.cpp:286–374  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

284
285template <typename Cache>
286static void test_cache_generations()
287{
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.
298
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);
304
305 // block_activity models a chunk of network activity. n_insert elements are
306 // added 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()
313 {
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();
321 }
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);
328 }
329 };
330
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)));
340
341 std::vector<block_activity> hashes;
342 Cache set{};
343 set.setup_bytes(bytes);

Callers

nothing calls this directly

Calls 6

FastRandomContextClass · 0.85
setup_bytesMethod · 0.45
reserveMethod · 0.45
sizeMethod · 0.45
containsMethod · 0.45
pop_backMethod · 0.45

Tested by

no test coverage detected