|
| 1 | +// Copyright (c) 2012-2019 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <bench/bench.h> |
| 6 | +#include <interfaces/chain.h> |
| 7 | +#include <key_io.h> |
| 8 | +#include <optional.h> |
| 9 | +#include <test/util.h> |
| 10 | +#include <validationinterface.h> |
| 11 | +#include <wallet/wallet.h> |
| 12 | + |
| 13 | +struct WalletTestingSetup { |
| 14 | + std::unique_ptr<interfaces::Chain> m_chain = interfaces::MakeChain(); |
| 15 | + CWallet m_wallet; |
| 16 | + |
| 17 | + WalletTestingSetup() |
| 18 | + : m_wallet{m_chain.get(), WalletLocation(), WalletDatabase::CreateMock()} |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + void handleNotifications() |
| 23 | + { |
| 24 | + m_wallet.m_chain_notifications_handler = m_chain->handleNotifications(m_wallet); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +static void WalletBalance(benchmark::State& state, const bool set_dirty, const bool add_watchonly, const bool add_mine) |
| 29 | +{ |
| 30 | + const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE; |
| 31 | + |
| 32 | + WalletTestingSetup wallet_t{}; |
| 33 | + auto& wallet = wallet_t.m_wallet; |
| 34 | + { |
| 35 | + bool first_run; |
| 36 | + if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false); |
| 37 | + wallet_t.handleNotifications(); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + const Optional<std::string> address_mine{add_mine ? Optional<std::string>{getnewaddress(wallet)} : nullopt}; |
| 42 | + if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY); |
| 43 | + |
| 44 | + for (int i = 0; i < 100; ++i) { |
| 45 | + generatetoaddress(address_mine.get_value_or(ADDRESS_WATCHONLY)); |
| 46 | + generatetoaddress(ADDRESS_WATCHONLY); |
| 47 | + } |
| 48 | + SyncWithValidationInterfaceQueue(); |
| 49 | + |
| 50 | + auto bal = wallet.GetBalance(); // Cache |
| 51 | + |
| 52 | + while (state.KeepRunning()) { |
| 53 | + if (set_dirty) wallet.MarkDirty(); |
| 54 | + bal = wallet.GetBalance(); |
| 55 | + if (add_mine) assert(bal.m_mine_trusted > 0); |
| 56 | + if (add_watchonly) assert(bal.m_watchonly_trusted > 0); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +static void WalletBalanceDirty(benchmark::State& state) { WalletBalance(state, /* set_dirty */ true, /* add_watchonly */ true, /* add_mine */ true); } |
| 61 | +static void WalletBalanceClean(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ true); } |
| 62 | +static void WalletBalanceMine(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ false, /* add_mine */ true); } |
| 63 | +static void WalletBalanceWatch(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ false); } |
| 64 | + |
| 65 | +BENCHMARK(WalletBalanceDirty, 2500); |
| 66 | +BENCHMARK(WalletBalanceClean, 8000); |
| 67 | +BENCHMARK(WalletBalanceMine, 16000); |
| 68 | +BENCHMARK(WalletBalanceWatch, 8000); |
0 commit comments