wallet_loading.cpp raw

   1  // Copyright (c) 2022 The Limenka 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 <addresstype.h>
   6  #include <bench/bench.h>
   7  #include <limenka-build-config.h> // IWYU pragma: keep
   8  #include <consensus/amount.h>
   9  #include <outputtype.h>
  10  #include <primitives/transaction.h>
  11  #include <test/util/setup_common.h>
  12  #include <util/check.h>
  13  #include <wallet/context.h>
  14  #include <wallet/db.h>
  15  #include <wallet/test/util.h>
  16  #include <wallet/transaction.h>
  17  #include <wallet/wallet.h>
  18  #include <wallet/walletutil.h>
  19  
  20  #include <cstdint>
  21  #include <memory>
  22  #include <utility>
  23  #include <vector>
  24  
  25  namespace wallet{
  26  static void AddTx(CWallet& wallet)
  27  {
  28      CMutableTransaction mtx;
  29      mtx.vout.emplace_back(COIN, GetScriptForDestination(*Assert(wallet.GetNewDestination(OutputType::BECH32, ""))));
  30      mtx.vin.emplace_back();
  31  
  32      wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});
  33  }
  34  
  35  static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
  36  {
  37      const auto test_setup = MakeNoLogFileContext<TestingSetup>();
  38  
  39      WalletContext context;
  40      context.args = &test_setup->m_args;
  41      context.chain = test_setup->m_node.chain.get();
  42  
  43      // Setup the wallet
  44      // Loading the wallet will also create it
  45      uint64_t create_flags = 0;
  46      if (!legacy_wallet) {
  47          create_flags = WALLET_FLAG_DESCRIPTORS;
  48      }
  49      auto database = CreateMockableWalletDatabase();
  50      auto wallet = TestLoadWallet(std::move(database), context, create_flags);
  51  
  52      // Generate a bunch of transactions and addresses to put into the wallet
  53      for (int i = 0; i < 1000; ++i) {
  54          AddTx(*wallet);
  55      }
  56  
  57      database = DuplicateMockDatabase(wallet->GetDatabase());
  58  
  59      // reload the wallet for the actual benchmark
  60      TestUnloadWallet(std::move(wallet));
  61  
  62      bench.epochs(5).run([&] {
  63          wallet = TestLoadWallet(std::move(database), context, create_flags);
  64  
  65          // Cleanup
  66          database = DuplicateMockDatabase(wallet->GetDatabase());
  67          TestUnloadWallet(std::move(wallet));
  68      });
  69  }
  70  
  71  #ifdef USE_BDB
  72  static void WalletLoadingLegacy(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/true); }
  73  BENCHMARK(WalletLoadingLegacy, benchmark::PriorityLevel::HIGH);
  74  #endif
  75  
  76  #ifdef USE_SQLITE
  77  static void WalletLoadingDescriptors(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/false); }
  78  BENCHMARK(WalletLoadingDescriptors, benchmark::PriorityLevel::HIGH);
  79  #endif
  80  } // namespace wallet
  81