/ src / bench / wallet_create.cpp
wallet_create.cpp
 1  // Copyright (c) 2023-present The Bitcoin Core developers
 2  // Distributed under the MIT software license, see the accompanying
 3  // file COPYING or https://www.opensource.org/licenses/mit-license.php.
 4  
 5  #include <bench/bench.h>
 6  #include <random.h>
 7  #include <support/allocators/secure.h>
 8  #include <test/util/setup_common.h>
 9  #include <uint256.h>
10  #include <util/fs.h>
11  #include <util/translation.h>
12  #include <wallet/context.h>
13  #include <wallet/db.h>
14  #include <wallet/wallet.h>
15  #include <wallet/walletutil.h>
16  
17  #include <cassert>
18  #include <memory>
19  #include <optional>
20  #include <string>
21  #include <utility>
22  #include <vector>
23  
24  namespace wallet {
25  static void WalletCreate(benchmark::Bench& bench, bool encrypted)
26  {
27      auto test_setup = MakeNoLogFileContext<TestingSetup>();
28      FastRandomContext random;
29  
30      WalletContext context;
31      context.args = &test_setup->m_args;
32      context.chain = test_setup->m_node.chain.get();
33  
34      DatabaseOptions options;
35      options.require_format = DatabaseFormat::SQLITE;
36      options.require_create = true;
37      options.create_flags = WALLET_FLAG_DESCRIPTORS;
38  
39      if (encrypted) {
40          options.create_passphrase = random.rand256().ToString();
41      }
42  
43      DatabaseStatus status;
44      bilingual_str error_string;
45      std::vector<bilingual_str> warnings;
46  
47      const auto wallet_path = test_setup->m_path_root / "test_wallet";
48      const auto wallet_name = fs::PathToString(wallet_path);
49  
50      bench.run([&] {
51          auto wallet = CreateWallet(context, wallet_name, /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
52          assert(status == DatabaseStatus::SUCCESS);
53          assert(wallet != nullptr);
54  
55          // Release wallet
56          RemoveWallet(context, wallet, /*load_on_start=*/ std::nullopt);
57          WaitForDeleteWallet(std::move(wallet));
58          fs::remove(wallet_path / "wallet.dat");
59          fs::remove(wallet_path);
60      });
61  }
62  
63  static void WalletCreatePlain(benchmark::Bench& bench) { WalletCreate(bench, /*encrypted=*/false); }
64  static void WalletCreateEncrypted(benchmark::Bench& bench) { WalletCreate(bench, /*encrypted=*/true); }
65  
66  BENCHMARK(WalletCreatePlain);
67  BENCHMARK(WalletCreateEncrypted);
68  
69  } // namespace wallet