Changeset View
Changeset View
Standalone View
Standalone View
src/bench/addrman.cpp
Show First 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | |||||
static void FillAddrMan(CAddrMan &addrman) { | static void FillAddrMan(CAddrMan &addrman) { | ||||
CreateAddresses(); | CreateAddresses(); | ||||
AddAddressesToAddrMan(addrman); | AddAddressesToAddrMan(addrman); | ||||
} | } | ||||
/* Benchmarks */ | /* Benchmarks */ | ||||
static void AddrManAdd(benchmark::State &state) { | static void AddrManAdd(benchmark::Bench &bench) { | ||||
CreateAddresses(); | CreateAddresses(); | ||||
CAddrMan addrman; | CAddrMan addrman; | ||||
while (state.KeepRunning()) { | bench.run([&] { | ||||
AddAddressesToAddrMan(addrman); | AddAddressesToAddrMan(addrman); | ||||
addrman.Clear(); | addrman.Clear(); | ||||
} | }); | ||||
} | } | ||||
static void AddrManSelect(benchmark::State &state) { | static void AddrManSelect(benchmark::Bench &bench) { | ||||
CAddrMan addrman; | CAddrMan addrman; | ||||
FillAddrMan(addrman); | FillAddrMan(addrman); | ||||
while (state.KeepRunning()) { | bench.run([&] { | ||||
const auto &address = addrman.Select(); | const auto &address = addrman.Select(); | ||||
assert(address.GetPort() > 0); | assert(address.GetPort() > 0); | ||||
} | }); | ||||
} | } | ||||
static void AddrManGetAddr(benchmark::State &state) { | static void AddrManGetAddr(benchmark::Bench &bench) { | ||||
CAddrMan addrman; | CAddrMan addrman; | ||||
FillAddrMan(addrman); | FillAddrMan(addrman); | ||||
while (state.KeepRunning()) { | bench.run([&] { | ||||
const auto &addresses = addrman.GetAddr(); | const auto &addresses = addrman.GetAddr(); | ||||
assert(addresses.size() > 0); | assert(addresses.size() > 0); | ||||
} | }); | ||||
} | } | ||||
static void AddrManGood(benchmark::State &state) { | static void AddrManGood(benchmark::Bench &bench) { | ||||
/* | /* | ||||
* Create many CAddrMan objects - one to be modified at each loop iteration. | * Create many CAddrMan objects - one to be modified at each loop iteration. | ||||
* This is necessary because the CAddrMan::Good() method modifies the | * This is necessary because the CAddrMan::Good() method modifies the | ||||
* object, affecting the timing of subsequent calls to the same method and | * object, affecting the timing of subsequent calls to the same method and | ||||
* we want to do the same amount of work in every loop iteration. | * we want to do the same amount of work in every loop iteration. | ||||
*/ | */ | ||||
const uint64_t numLoops = state.m_num_iters * state.m_num_evals; | bench.epochs(5).epochIterations(1); | ||||
std::vector<CAddrMan> addrmans(numLoops); | std::vector<CAddrMan> addrmans(bench.epochs() * bench.epochIterations()); | ||||
for (auto &addrman : addrmans) { | for (auto &addrman : addrmans) { | ||||
FillAddrMan(addrman); | FillAddrMan(addrman); | ||||
} | } | ||||
auto markSomeAsGood = [](CAddrMan &addrman) { | auto markSomeAsGood = [](CAddrMan &addrman) { | ||||
for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) { | for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) { | ||||
for (size_t addr_i = 0; addr_i < NUM_ADDRESSES_PER_SOURCE; | for (size_t addr_i = 0; addr_i < NUM_ADDRESSES_PER_SOURCE; | ||||
++addr_i) { | ++addr_i) { | ||||
if (addr_i % 32 == 0) { | if (addr_i % 32 == 0) { | ||||
addrman.Good(g_addresses[source_i][addr_i]); | addrman.Good(g_addresses[source_i][addr_i]); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
uint64_t i = 0; | uint64_t i = 0; | ||||
while (state.KeepRunning()) { | bench.run([&] { | ||||
markSomeAsGood(addrmans.at(i)); | markSomeAsGood(addrmans.at(i)); | ||||
++i; | ++i; | ||||
} | }); | ||||
} | } | ||||
BENCHMARK(AddrManAdd, 5); | BENCHMARK(AddrManAdd); | ||||
BENCHMARK(AddrManSelect, 1000000); | BENCHMARK(AddrManSelect); | ||||
BENCHMARK(AddrManGetAddr, 500); | BENCHMARK(AddrManGetAddr); | ||||
BENCHMARK(AddrManGood, 2); | BENCHMARK(AddrManGood); |