|
4 | 4 |
|
5 | 5 | #include <bench/bench.h>
|
6 | 6 | #include <wallet/wallet.h>
|
| 7 | +#include <wallet/coinselection.h> |
7 | 8 |
|
8 | 9 | #include <set>
|
9 | 10 |
|
@@ -61,4 +62,47 @@ static void CoinSelection(benchmark::State& state)
|
61 | 62 | }
|
62 | 63 | }
|
63 | 64 |
|
| 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 | + |
64 | 107 | BENCHMARK(CoinSelection, 650);
|
| 108 | +BENCHMARK(BnBExhaustion, 650); |
0 commit comments