|
| 1 | +// Copyright (c) 2022 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 <node/context.h> |
| 8 | +#include <test/util/mining.h> |
| 9 | +#include <test/util/setup_common.h> |
| 10 | +#include <test/util/wallet.h> |
| 11 | +#include <util/translation.h> |
| 12 | +#include <validationinterface.h> |
| 13 | +#include <wallet/context.h> |
| 14 | +#include <wallet/receive.h> |
| 15 | +#include <wallet/wallet.h> |
| 16 | + |
| 17 | +#include <optional> |
| 18 | + |
| 19 | +using wallet::CWallet; |
| 20 | +using wallet::DatabaseOptions; |
| 21 | +using wallet::DatabaseStatus; |
| 22 | +using wallet::ISMINE_SPENDABLE; |
| 23 | +using wallet::MakeWalletDatabase; |
| 24 | +using wallet::WALLET_FLAG_DESCRIPTORS; |
| 25 | +using wallet::WalletContext; |
| 26 | + |
| 27 | +static const std::shared_ptr<CWallet> BenchLoadWallet(WalletContext& context, DatabaseOptions& options) |
| 28 | +{ |
| 29 | + DatabaseStatus status; |
| 30 | + bilingual_str error; |
| 31 | + std::vector<bilingual_str> warnings; |
| 32 | + auto database = MakeWalletDatabase("", options, status, error); |
| 33 | + assert(database); |
| 34 | + auto wallet = CWallet::Create(context, "", std::move(database), options.create_flags, error, warnings); |
| 35 | + NotifyWalletLoaded(context, wallet); |
| 36 | + if (context.chain) { |
| 37 | + wallet->postInitProcess(); |
| 38 | + } |
| 39 | + return wallet; |
| 40 | +} |
| 41 | + |
| 42 | +static void BenchUnloadWallet(std::shared_ptr<CWallet>&& wallet) |
| 43 | +{ |
| 44 | + SyncWithValidationInterfaceQueue(); |
| 45 | + wallet->m_chain_notifications_handler.reset(); |
| 46 | + UnloadWallet(std::move(wallet)); |
| 47 | +} |
| 48 | + |
| 49 | +static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet) |
| 50 | +{ |
| 51 | + const auto test_setup = MakeNoLogFileContext<TestingSetup>(); |
| 52 | + |
| 53 | + WalletContext context; |
| 54 | + context.args = &test_setup->m_args; |
| 55 | + context.chain = test_setup->m_node.chain.get(); |
| 56 | + |
| 57 | + // Setup the wallet |
| 58 | + // Loading the wallet will also create it |
| 59 | + DatabaseOptions options; |
| 60 | + if (!legacy_wallet) options.create_flags = WALLET_FLAG_DESCRIPTORS; |
| 61 | + auto wallet = BenchLoadWallet(context, options); |
| 62 | + |
| 63 | + // Generate a bunch of transactions and addresses to put into the wallet |
| 64 | + for (int i = 0; i < 5000; ++i) { |
| 65 | + generatetoaddress(test_setup->m_node, getnewaddress(*wallet)); |
| 66 | + } |
| 67 | + |
| 68 | + // reload the wallet for the actual benchmark |
| 69 | + BenchUnloadWallet(std::move(wallet)); |
| 70 | + |
| 71 | + bench.minEpochIterations(10).run([&] { |
| 72 | + wallet = BenchLoadWallet(context, options); |
| 73 | + |
| 74 | + // Cleanup |
| 75 | + BenchUnloadWallet(std::move(wallet)); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +static void WalletLoadingLegacy(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/true); } |
| 80 | +static void WalletLoadingDescriptors(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/false); } |
| 81 | + |
| 82 | +BENCHMARK(WalletLoadingLegacy); |
| 83 | +BENCHMARK(WalletLoadingDescriptors); |
0 commit comments