2016-12-31 19:01:21 +01:00
|
|
|
// Copyright (c) 2012-2016 The Bitcoin Core developers
|
2016-10-02 23:38:48 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <bench/bench.h>
|
|
|
|
#include <wallet/wallet.h>
|
2016-10-02 23:38:48 +02:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
2016-12-05 08:03:53 +01:00
|
|
|
static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<COutput>& vCoins)
|
2016-10-02 23:38:48 +02:00
|
|
|
{
|
|
|
|
int nInput = 0;
|
|
|
|
|
|
|
|
static int nextLockTime = 0;
|
|
|
|
CMutableTransaction tx;
|
|
|
|
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
|
|
|
|
tx.vout.resize(nInput + 1);
|
|
|
|
tx.vout[nInput].nValue = nValue;
|
2016-11-12 01:54:51 +01:00
|
|
|
CWalletTx* wtx = new CWalletTx(&wallet, MakeTransactionRef(std::move(tx)));
|
2016-10-02 23:38:48 +02:00
|
|
|
|
|
|
|
int nAge = 6 * 24;
|
2017-02-23 17:20:16 +01:00
|
|
|
COutput output(wtx, nInput, nAge, true /* spendable */, true /* solvable */, true /* safe */);
|
2016-10-02 23:38:48 +02:00
|
|
|
vCoins.push_back(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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::State& state)
|
|
|
|
{
|
|
|
|
const CWallet wallet;
|
2016-12-05 08:03:53 +01:00
|
|
|
std::vector<COutput> vCoins;
|
2016-10-02 23:38:48 +02:00
|
|
|
LOCK(wallet.cs_wallet);
|
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
// Empty wallet.
|
2017-06-02 03:18:57 +02:00
|
|
|
for (COutput output : vCoins)
|
2016-10-02 23:38:48 +02:00
|
|
|
delete output.tx;
|
|
|
|
vCoins.clear();
|
|
|
|
|
|
|
|
// Add coins.
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
addCoin(1000 * COIN, wallet, vCoins);
|
|
|
|
addCoin(3 * COIN, wallet, vCoins);
|
|
|
|
|
2017-04-07 11:38:33 +02:00
|
|
|
std::set<CInputCoin> setCoinsRet;
|
2016-10-02 23:38:48 +02:00
|
|
|
CAmount nValueRet;
|
2016-12-02 21:29:20 +01:00
|
|
|
bool success = wallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, setCoinsRet, nValueRet);
|
2016-10-02 23:38:48 +02:00
|
|
|
assert(success);
|
|
|
|
assert(nValueRet == 1003 * COIN);
|
|
|
|
assert(setCoinsRet.size() == 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:48:02 +02:00
|
|
|
BENCHMARK(CoinSelection, 650);
|