| 63 | */ |
| 64 | template <typename Cache> |
| 65 | static double test_cache(size_t megabytes, double load) |
| 66 | { |
| 67 | local_rand_ctx = FastRandomContext(true); |
| 68 | std::vector<uint256> hashes; |
| 69 | Cache set{}; |
| 70 | size_t bytes = megabytes * (1 << 20); |
| 71 | set.setup_bytes(bytes); |
| 72 | uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); |
| 73 | hashes.resize(n_insert); |
| 74 | for (uint32_t i = 0; i < n_insert; ++i) { |
| 75 | uint32_t* ptr = (uint32_t*)hashes[i].begin(); |
| 76 | for (uint8_t j = 0; j < 8; ++j) |
| 77 | *(ptr++) = local_rand_ctx.rand32(); |
| 78 | } |
| 79 | /** We make a copy of the hashes because future optimizations of the |
| 80 | * cuckoocache may overwrite the inserted element, so the test is |
| 81 | * "future proofed". |
| 82 | */ |
| 83 | std::vector<uint256> hashes_insert_copy = hashes; |
| 84 | /** Do the insert */ |
| 85 | for (uint256& h : hashes_insert_copy) |
| 86 | set.insert(h); |
| 87 | /** Count the hits */ |
| 88 | uint32_t count = 0; |
| 89 | for (uint256& h : hashes) |
| 90 | count += set.contains(h, false); |
| 91 | double hit_rate = ((double)count) / ((double)n_insert); |
| 92 | return hit_rate; |
| 93 | } |
| 94 | |
| 95 | /** The normalized hit rate for a given load. |
| 96 | * |
nothing calls this directly
no test coverage detected