| 78 | */ |
| 79 | template <typename Cache> |
| 80 | double test_cache(size_t megabytes, double load) |
| 81 | { |
| 82 | insecure_rand = FastRandomContext(true); |
| 83 | std::vector<uint256> hashes; |
| 84 | Cache set{}; |
| 85 | size_t bytes = megabytes * (1 << 20); |
| 86 | set.setup_bytes(bytes); |
| 87 | uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); |
| 88 | hashes.resize(n_insert); |
| 89 | for (uint32_t i = 0; i < n_insert; ++i) { |
| 90 | uint32_t* ptr = (uint32_t*)hashes[i].begin(); |
| 91 | for (uint8_t j = 0; j < 8; ++j) |
| 92 | *(ptr++) = insecure_rand.rand32(); |
| 93 | } |
| 94 | /** We make a copy of the hashes because future optimizations of the |
| 95 | * cuckoocache may overwrite the inserted element, so the test is |
| 96 | * "future proofed". |
| 97 | */ |
| 98 | std::vector<uint256> hashes_insert_copy = hashes; |
| 99 | /** Do the insert */ |
| 100 | for (uint256& h : hashes_insert_copy) |
| 101 | set.insert(h); |
| 102 | /** Count the hits */ |
| 103 | uint32_t count = 0; |
| 104 | for (uint256& h : hashes) |
| 105 | count += set.contains(h, false); |
| 106 | double hit_rate = ((double)count) / ((double)n_insert); |
| 107 | return hit_rate; |
| 108 | } |
| 109 | |
| 110 | /** The normalized hit rate for a given load. |
| 111 | * |
nothing calls this directly
no test coverage detected