Changeset View
Changeset View
Standalone View
Standalone View
src/bench/coin_selection.cpp
// Copyright (c) 2012-2016 The Bitcoin Core developers | // Copyright (c) 2012-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <bench/bench.h> | #include <bench/bench.h> | ||||
#include <chainparams.h> | #include <chainparams.h> | ||||
#include <wallet/coinselection.h> | |||||
#include <wallet/wallet.h> | #include <wallet/wallet.h> | ||||
#include <set> | #include <set> | ||||
static void addCoin(const Amount nValue, const CWallet &wallet, | static void addCoin(const Amount nValue, const CWallet &wallet, | ||||
std::vector<COutput> &vCoins) { | std::vector<COutput> &vCoins) { | ||||
int nInput = 0; | int nInput = 0; | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | while (state.KeepRunning()) { | ||||
// Empty wallet. | // Empty wallet. | ||||
for (COutput output : vCoins) { | for (COutput output : vCoins) { | ||||
delete output.tx; | delete output.tx; | ||||
} | } | ||||
vCoins.clear(); | vCoins.clear(); | ||||
} | } | ||||
} | } | ||||
typedef std::set<CInputCoin> CoinSet; | |||||
// Copied from src/wallet/test/coinselector_tests.cpp | |||||
static void add_coin(const Amount nValue, int nInput, | |||||
std::vector<CInputCoin> &set) { | |||||
CMutableTransaction tx; | |||||
tx.vout.resize(nInput + 1); | |||||
tx.vout[nInput].nValue = nValue; | |||||
set.emplace_back(MakeTransactionRef(tx), nInput); | |||||
} | |||||
// Copied from src/wallet/test/coinselector_tests.cpp | |||||
static Amount make_hard_case(int utxos, std::vector<CInputCoin> &utxo_pool) { | |||||
utxo_pool.clear(); | |||||
Amount target = Amount::zero(); | |||||
for (int i = 0; i < utxos; ++i) { | |||||
const Amount base = (int64_t(1) << (utxos + i)) * SATOSHI; | |||||
target += base; | |||||
add_coin(base, 2 * i, utxo_pool); | |||||
add_coin(base + (int64_t(1) << (utxos - 1 - i)) * SATOSHI, 2 * i + 1, | |||||
utxo_pool); | |||||
} | |||||
return target; | |||||
} | |||||
static void BnBExhaustion(benchmark::State &state) { | |||||
// Setup | |||||
std::vector<CInputCoin> utxo_pool; | |||||
CoinSet selection; | |||||
Amount value_ret = Amount::zero(); | |||||
Amount not_input_fees = Amount::zero(); | |||||
while (state.KeepRunning()) { | |||||
// Benchmark | |||||
Amount target = make_hard_case(17, utxo_pool); | |||||
// Should exhaust | |||||
SelectCoinsBnB(utxo_pool, target, Amount::zero(), selection, value_ret, | |||||
not_input_fees); | |||||
// Cleanup | |||||
utxo_pool.clear(); | |||||
selection.clear(); | |||||
} | |||||
} | |||||
BENCHMARK(CoinSelection, 650); | BENCHMARK(CoinSelection, 650); | ||||
BENCHMARK(BnBExhaustion, 650); |