mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-13 11:25:02 -05:00
![Wladimir J. van der Laan](/assets/img/avatar_default.png)
78c312c983
Replace current benchmarking framework with nanobench (Martin Ankerl) Pull request description: Replace current benchmarking framework with nanobench This replaces the current benchmarking framework with nanobench [1], an MIT licensed single-header benchmarking library, of which I am the autor. This has in my opinion several advantages, especially on Linux: * fast: Running all benchmarks takes ~6 seconds instead of 4m13s on an Intel i7-8700 CPU @ 3.20GHz. * accurate: I ran e.g. the benchmark for SipHash_32b 10 times and calculate standard deviation / mean = coefficient of variation: * 0.57% CV for old benchmarking framework * 0.20% CV for nanobench So the benchmark results with nanobench seem to vary less than with the old framework. * It automatically determines runtime based on clock precision, no need to specify number of evaluations. * measure instructions, cycles, branches, instructions per cycle, branch misses (only Linux, when performance counters are available) * output in markdown table format. * Warn about unstable environment (frequency scaling, turbo, ...) * For better profiling, it is possible to set the environment variable NANOBENCH_ENDLESS to force endless running of a particular benchmark without the need to recompile. This makes it to e.g. run "perf top" and look at hotspots. Here is an example copy & pasted from the terminal output: | ns/byte | byte/s | err% | ins/byte | cyc/byte | IPC | bra/byte | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 2.52 | 396,529,415.94 | 0.6% | 25.42 | 8.02 | 3.169 | 0.06 | 0.0% | 0.03 | `bench/crypto_hash.cpp RIPEMD160` | 1.87 | 535,161,444.83 | 0.3% | 21.36 | 5.95 | 3.589 | 0.06 | 0.0% | 0.02 | `bench/crypto_hash.cpp SHA1` | 3.22 | 310,344,174.79 | 1.1% | 36.80 | 10.22 | 3.601 | 0.09 | 0.0% | 0.04 | `bench/crypto_hash.cpp SHA256` | 2.01 | 496,375,796.23 | 0.0% | 18.72 | 6.43 | 2.911 | 0.01 | 1.0% | 0.00 | `bench/crypto_hash.cpp SHA256D64_1024` | 7.23 | 138,263,519.35 | 0.1% | 82.66 | 23.11 | 3.577 | 1.63 | 0.1% | 0.00 | `bench/crypto_hash.cpp SHA256_32b` | 3.04 | 328,780,166.40 | 0.3% | 35.82 | 9.69 | 3.696 | 0.03 | 0.0% | 0.03 | `bench/crypto_hash.cpp SHA512` [1] https://github.com/martinus/nanobench ACKs for top commit: laanwj: ACK78c312c983
Tree-SHA512: 9e18770b18b6f95a7d0105a4a5497d31cf4eb5efe6574f4482f6f1b4c88d7e0946b9a4a1e9e8e6ecbf41a3f2d7571240677dcb45af29a6f0584e89b25f32e49e
115 lines
4.2 KiB
C++
115 lines
4.2 KiB
C++
// Copyright (c) 2012-2019 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <bench/bench.h>
|
|
#include <interfaces/chain.h>
|
|
#include <node/context.h>
|
|
#include <wallet/coinselection.h>
|
|
#include <wallet/wallet.h>
|
|
|
|
#include <set>
|
|
|
|
static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<std::unique_ptr<CWalletTx>>& wtxs)
|
|
{
|
|
static int nextLockTime = 0;
|
|
CMutableTransaction tx;
|
|
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
|
|
tx.vout.resize(1);
|
|
tx.vout[0].nValue = nValue;
|
|
wtxs.push_back(MakeUnique<CWalletTx>(&wallet, MakeTransactionRef(std::move(tx))));
|
|
}
|
|
|
|
// Simple benchmark for wallet coin selection. Note that it maybe be necessary
|
|
// to build up more complicated scenarios in order to get meaningful
|
|
// measurements of performance. From laanwj, "Wallet coin selection is probably
|
|
// the hardest, as you need a wider selection of scenarios, just testing the
|
|
// same one over and over isn't too useful. Generating random isn't useful
|
|
// either for measurements."
|
|
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
|
|
static void CoinSelection(benchmark::Bench& bench)
|
|
{
|
|
NodeContext node;
|
|
auto chain = interfaces::MakeChain(node);
|
|
CWallet wallet(chain.get(), WalletLocation(), CreateDummyWalletDatabase());
|
|
wallet.SetupLegacyScriptPubKeyMan();
|
|
std::vector<std::unique_ptr<CWalletTx>> wtxs;
|
|
LOCK(wallet.cs_wallet);
|
|
|
|
// Add coins.
|
|
for (int i = 0; i < 1000; ++i) {
|
|
addCoin(1000 * COIN, wallet, wtxs);
|
|
}
|
|
addCoin(3 * COIN, wallet, wtxs);
|
|
|
|
// Create groups
|
|
std::vector<OutputGroup> groups;
|
|
for (const auto& wtx : wtxs) {
|
|
COutput output(wtx.get(), 0 /* iIn */, 6 * 24 /* nDepthIn */, true /* spendable */, true /* solvable */, true /* safe */);
|
|
groups.emplace_back(output.GetInputCoin(), 6, false, 0, 0);
|
|
}
|
|
|
|
const CoinEligibilityFilter filter_standard(1, 6, 0);
|
|
const CoinSelectionParams coin_selection_params(true, 34, 148, CFeeRate(0), 0);
|
|
bench.run([&] {
|
|
std::set<CInputCoin> setCoinsRet;
|
|
CAmount nValueRet;
|
|
bool bnb_used;
|
|
bool success = wallet.SelectCoinsMinConf(1003 * COIN, filter_standard, groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used);
|
|
assert(success);
|
|
assert(nValueRet == 1003 * COIN);
|
|
assert(setCoinsRet.size() == 2);
|
|
});
|
|
}
|
|
|
|
typedef std::set<CInputCoin> CoinSet;
|
|
static NodeContext testNode;
|
|
static auto testChain = interfaces::MakeChain(testNode);
|
|
static CWallet testWallet(testChain.get(), WalletLocation(), CreateDummyWalletDatabase());
|
|
std::vector<std::unique_ptr<CWalletTx>> wtxn;
|
|
|
|
// Copied from src/wallet/test/coinselector_tests.cpp
|
|
static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>& set)
|
|
{
|
|
CMutableTransaction tx;
|
|
tx.vout.resize(nInput + 1);
|
|
tx.vout[nInput].nValue = nValue;
|
|
std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
|
|
set.emplace_back(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0);
|
|
wtxn.emplace_back(std::move(wtx));
|
|
}
|
|
// Copied from src/wallet/test/coinselector_tests.cpp
|
|
static CAmount make_hard_case(int utxos, std::vector<OutputGroup>& utxo_pool)
|
|
{
|
|
utxo_pool.clear();
|
|
CAmount target = 0;
|
|
for (int i = 0; i < utxos; ++i) {
|
|
target += (CAmount)1 << (utxos+i);
|
|
add_coin((CAmount)1 << (utxos+i), 2*i, utxo_pool);
|
|
add_coin(((CAmount)1 << (utxos+i)) + ((CAmount)1 << (utxos-1-i)), 2*i + 1, utxo_pool);
|
|
}
|
|
return target;
|
|
}
|
|
|
|
static void BnBExhaustion(benchmark::Bench& bench)
|
|
{
|
|
// Setup
|
|
testWallet.SetupLegacyScriptPubKeyMan();
|
|
std::vector<OutputGroup> utxo_pool;
|
|
CoinSet selection;
|
|
CAmount value_ret = 0;
|
|
CAmount not_input_fees = 0;
|
|
|
|
bench.run([&] {
|
|
// Benchmark
|
|
CAmount target = make_hard_case(17, utxo_pool);
|
|
SelectCoinsBnB(utxo_pool, target, 0, selection, value_ret, not_input_fees); // Should exhaust
|
|
|
|
// Cleanup
|
|
utxo_pool.clear();
|
|
selection.clear();
|
|
});
|
|
}
|
|
|
|
BENCHMARK(CoinSelection);
|
|
BENCHMARK(BnBExhaustion);
|