Skip to content

Commit 76d2f06

Browse files
committed
Benchmark BnB in the worst case where it exhausts
1 parent 6a34ff5 commit 76d2f06

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/bench/coin_selection.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <bench/bench.h>
66
#include <wallet/wallet.h>
7+
#include <wallet/coinselection.h>
78

89
#include <set>
910

@@ -61,4 +62,47 @@ static void CoinSelection(benchmark::State& state)
6162
}
6263
}
6364

65+
typedef std::set<CInputCoin> CoinSet;
66+
67+
// Copied from src/wallet/test/coinselector_tests.cpp
68+
static void add_coin(const CAmount& nValue, int nInput, std::vector<CInputCoin>& set)
69+
{
70+
CMutableTransaction tx;
71+
tx.vout.resize(nInput + 1);
72+
tx.vout[nInput].nValue = nValue;
73+
set.emplace_back(MakeTransactionRef(tx), nInput);
74+
}
75+
// Copied from src/wallet/test/coinselector_tests.cpp
76+
static CAmount make_hard_case(int utxos, std::vector<CInputCoin>& utxo_pool)
77+
{
78+
utxo_pool.clear();
79+
CAmount target = 0;
80+
for (int i = 0; i < utxos; ++i) {
81+
target += (CAmount)1 << (utxos+i);
82+
add_coin((CAmount)1 << (utxos+i), 2*i, utxo_pool);
83+
add_coin(((CAmount)1 << (utxos+i)) + ((CAmount)1 << (utxos-1-i)), 2*i + 1, utxo_pool);
84+
}
85+
return target;
86+
}
87+
88+
static void BnBExhaustion(benchmark::State& state)
89+
{
90+
// Setup
91+
std::vector<CInputCoin> utxo_pool;
92+
CoinSet selection;
93+
CAmount value_ret = 0;
94+
CAmount not_input_fees = 0;
95+
96+
while (state.KeepRunning()) {
97+
// Benchmark
98+
CAmount target = make_hard_case(17, utxo_pool);
99+
SelectCoinsBnB(utxo_pool, target, 0, selection, value_ret, not_input_fees); // Should exhaust
100+
101+
// Cleanup
102+
utxo_pool.clear();
103+
selection.clear();
104+
}
105+
}
106+
64107
BENCHMARK(CoinSelection, 650);
108+
BENCHMARK(BnBExhaustion, 650);

0 commit comments

Comments
 (0)