Skip to content

Commit fa46ac3

Browse files
author
MarcoFalke
committed
bench: Add wallet_balance benchmarks
1 parent 78295e9 commit fa46ac3

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

build_msvc/bench_bitcoin/bench_bitcoin.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<ClCompile Include="..\..\src\bench\rpc_mempool.cpp" />
4040
<ClCompile Include="..\..\src\bench\merkle_root.cpp" />
4141
<ClCompile Include="..\..\src\bench\rollingbloom.cpp" />
42+
<ClCompile Include="..\..\src\bench\wallet_balance.cpp" />
4243
<ClCompile Include="..\..\src\bench\verify_script.cpp" />
4344
</ItemGroup>
4445
<ItemGroup>

src/Makefile.bench.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ endif
6464

6565
if ENABLE_WALLET
6666
bench_bench_bitcoin_SOURCES += bench/coin_selection.cpp
67+
bench_bench_bitcoin_SOURCES += bench/wallet_balance.cpp
6768
endif
6869

6970
bench_bench_bitcoin_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(CRYPTO_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(MINIUPNPC_LIBS)

src/bench/wallet_balance.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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);

src/test/util.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include <boost/thread.hpp>
2424

25+
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
26+
2527
#ifdef ENABLE_WALLET
2628
std::string getnewaddress(CWallet& w)
2729
{
@@ -75,7 +77,6 @@ CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
7577
return CTxIn{block->vtx[0]->GetHash(), 0};
7678
}
7779

78-
7980
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
8081
{
8182
auto block = std::make_shared<CBlock>(

src/test/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class CScript;
1313
class CTxIn;
1414
class CWallet;
1515

16+
// Constants //
17+
18+
extern const std::string ADDRESS_BCRT1_UNSPENDABLE;
19+
1620
// Lower-level utils //
1721

1822
/** Returns the generated coin */

0 commit comments

Comments
 (0)