Changeset View
Changeset View
Standalone View
Standalone View
src/test/cuckoocache_tests.cpp
Show All 25 Lines | |||||
FastRandomContext local_rand_ctx(true); | FastRandomContext local_rand_ctx(true); | ||||
BOOST_AUTO_TEST_SUITE(cuckoocache_tests); | BOOST_AUTO_TEST_SUITE(cuckoocache_tests); | ||||
/** | /** | ||||
* insecure_GetRandHash fills in a uint256 from local_rand_ctx | * insecure_GetRandHash fills in a uint256 from local_rand_ctx | ||||
*/ | */ | ||||
void insecure_GetRandHash(uint256 &t) { | void insecure_GetRandHash(uint256 &t) { | ||||
uint32_t *ptr = (uint32_t *)t.begin(); | uint8_t *ptr = t.begin(); | ||||
for (uint8_t j = 0; j < 8; ++j) { | for (uint8_t j = 0; j < 32; ++j) { | ||||
*(ptr++) = local_rand_ctx.rand32(); | *(ptr++) = uint8_t(local_rand_ctx.randbits(8)); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Test that no values not inserted into the cache are read out of it. | * Test that no values not inserted into the cache are read out of it. | ||||
* | * | ||||
* There are no repeats in the first 200000 insecure_GetRandHash calls | * There are no repeats in the first 200000 insecure_GetRandHash calls | ||||
*/ | */ | ||||
Show All 20 Lines | template <typename Cache> double test_cache(size_t megabytes, double load) { | ||||
local_rand_ctx = FastRandomContext(true); | local_rand_ctx = FastRandomContext(true); | ||||
std::vector<uint256> hashes; | std::vector<uint256> hashes; | ||||
Cache set{}; | Cache set{}; | ||||
size_t bytes = megabytes * (1 << 20); | size_t bytes = megabytes * (1 << 20); | ||||
set.setup_bytes(bytes); | set.setup_bytes(bytes); | ||||
uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); | uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); | ||||
hashes.resize(n_insert); | hashes.resize(n_insert); | ||||
for (uint32_t i = 0; i < n_insert; ++i) { | for (uint32_t i = 0; i < n_insert; ++i) { | ||||
uint32_t *ptr = (uint32_t *)hashes[i].begin(); | uint8_t *ptr = hashes[i].begin(); | ||||
for (uint8_t j = 0; j < 8; ++j) { | for (uint8_t j = 0; j < 32; ++j) { | ||||
*(ptr++) = local_rand_ctx.rand32(); | *(ptr++) = uint8_t(local_rand_ctx.randbits(8)); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* We make a copy of the hashes because future optimizations of the | * We make a copy of the hashes because future optimizations of the | ||||
* cuckoocache may overwrite the inserted element, so the test is "future | * cuckoocache may overwrite the inserted element, so the test is "future | ||||
* proofed". | * proofed". | ||||
*/ | */ | ||||
std::vector<uint256> hashes_insert_copy = hashes; | std::vector<uint256> hashes_insert_copy = hashes; | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | template <typename Cache> void test_cache_erase(size_t megabytes) { | ||||
local_rand_ctx = FastRandomContext(true); | local_rand_ctx = FastRandomContext(true); | ||||
std::vector<uint256> hashes; | std::vector<uint256> hashes; | ||||
Cache set{}; | Cache set{}; | ||||
size_t bytes = megabytes * (1 << 20); | size_t bytes = megabytes * (1 << 20); | ||||
set.setup_bytes(bytes); | set.setup_bytes(bytes); | ||||
uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); | uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); | ||||
hashes.resize(n_insert); | hashes.resize(n_insert); | ||||
for (uint32_t i = 0; i < n_insert; ++i) { | for (uint32_t i = 0; i < n_insert; ++i) { | ||||
uint32_t *ptr = (uint32_t *)hashes[i].begin(); | uint8_t *ptr = hashes[i].begin(); | ||||
for (uint8_t j = 0; j < 8; ++j) { | for (uint8_t j = 0; j < 32; ++j) { | ||||
*(ptr++) = local_rand_ctx.rand32(); | *(ptr++) = uint8_t(local_rand_ctx.randbits(8)); | ||||
} | } | ||||
} | } | ||||
/** We make a copy of the hashes because future optimizations of the | /** We make a copy of the hashes because future optimizations of the | ||||
* cuckoocache may overwrite the inserted element, so the test is | * cuckoocache may overwrite the inserted element, so the test is | ||||
* "future proofed". | * "future proofed". | ||||
*/ | */ | ||||
std::vector<uint256> hashes_insert_copy = hashes; | std::vector<uint256> hashes_insert_copy = hashes; | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | template <typename Cache> void test_cache_erase_parallel(size_t megabytes) { | ||||
local_rand_ctx = FastRandomContext(true); | local_rand_ctx = FastRandomContext(true); | ||||
std::vector<uint256> hashes; | std::vector<uint256> hashes; | ||||
Cache set{}; | Cache set{}; | ||||
size_t bytes = megabytes * (1 << 20); | size_t bytes = megabytes * (1 << 20); | ||||
set.setup_bytes(bytes); | set.setup_bytes(bytes); | ||||
uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); | uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256))); | ||||
hashes.resize(n_insert); | hashes.resize(n_insert); | ||||
for (uint32_t i = 0; i < n_insert; ++i) { | for (uint32_t i = 0; i < n_insert; ++i) { | ||||
uint32_t *ptr = (uint32_t *)hashes[i].begin(); | uint8_t *ptr = hashes[i].begin(); | ||||
for (uint8_t j = 0; j < 8; ++j) { | for (uint8_t j = 0; j < 32; ++j) { | ||||
*(ptr++) = local_rand_ctx.rand32(); | *(ptr++) = uint8_t(local_rand_ctx.randbits(8)); | ||||
} | } | ||||
} | } | ||||
/** We make a copy of the hashes because future optimizations of the | /** We make a copy of the hashes because future optimizations of the | ||||
* cuckoocache may overwrite the inserted element, so the test is | * cuckoocache may overwrite the inserted element, so the test is | ||||
* "future proofed". | * "future proofed". | ||||
*/ | */ | ||||
std::vector<uint256> hashes_insert_copy = hashes; | std::vector<uint256> hashes_insert_copy = hashes; | ||||
boost::shared_mutex mtx; | boost::shared_mutex mtx; | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | template <typename Cache> void test_cache_generations() { | ||||
// immediately and never uses the other half. | // immediately and never uses the other half. | ||||
struct block_activity { | struct block_activity { | ||||
std::vector<uint256> reads; | std::vector<uint256> reads; | ||||
block_activity(uint32_t n_insert, Cache &c) : reads() { | block_activity(uint32_t n_insert, Cache &c) : reads() { | ||||
std::vector<uint256> inserts; | std::vector<uint256> inserts; | ||||
inserts.resize(n_insert); | inserts.resize(n_insert); | ||||
reads.reserve(n_insert / 2); | reads.reserve(n_insert / 2); | ||||
for (uint32_t i = 0; i < n_insert; ++i) { | for (uint32_t i = 0; i < n_insert; ++i) { | ||||
uint32_t *ptr = (uint32_t *)inserts[i].begin(); | uint32_t *ptr = | ||||
reinterpret_cast<uint32_t *>(inserts[i].begin()); | |||||
for (uint8_t j = 0; j < 8; ++j) { | for (uint8_t j = 0; j < 8; ++j) { | ||||
*(ptr++) = local_rand_ctx.rand32(); | *(ptr++) = local_rand_ctx.rand32(); | ||||
} | } | ||||
} | } | ||||
for (uint32_t i = 0; i < n_insert / 4; ++i) { | for (uint32_t i = 0; i < n_insert / 4; ++i) { | ||||
reads.push_back(inserts[i]); | reads.push_back(inserts[i]); | ||||
} | } | ||||
for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i) { | for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i) { | ||||
▲ Show 20 Lines • Show All 59 Lines • Show Last 20 Lines |