| 52 | */ |
| 53 | template <typename Cache> |
| 54 | static double test_cache(size_t megabytes, double load) |
| 55 | { |
| 56 | SeedInsecureRand(SeedRand::ZEROS); |
| 57 | std::vector<uint256> hashes; |
| 58 | Cache set{}; |
| 59 | size_t bytes = megabytes * (1 << 20); |
| 60 | set.setup_bytes(bytes); |
| 61 | uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); |
| 62 | hashes.resize(n_insert); |
| 63 | for (uint32_t i = 0; i < n_insert; ++i) { |
| 64 | uint32_t* ptr = (uint32_t*)hashes[i].begin(); |
| 65 | for (uint8_t j = 0; j < 8; ++j) |
| 66 | *(ptr++) = InsecureRand32(); |
| 67 | } |
| 68 | /** We make a copy of the hashes because future optimizations of the |
| 69 | * cuckoocache may overwrite the inserted element, so the test is |
| 70 | * "future proofed". |
| 71 | */ |
| 72 | std::vector<uint256> hashes_insert_copy = hashes; |
| 73 | /** Do the insert */ |
| 74 | for (const uint256& h : hashes_insert_copy) |
| 75 | set.insert(h); |
| 76 | /** Count the hits */ |
| 77 | uint32_t count = 0; |
| 78 | for (const uint256& h : hashes) |
| 79 | count += set.contains(h, false); |
| 80 | double hit_rate = ((double)count) / ((double)n_insert); |
| 81 | return hit_rate; |
| 82 | } |
| 83 | |
| 84 | /** The normalized hit rate for a given load. |
| 85 | * |
nothing calls this directly
no test coverage detected