/ 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  #if defined(HAVE_CONFIG_H)
 6  #include <config/bitcoin-config.h>
 7  #endif
 8  
 9  #include <bench/bench.h>
10  #include <node/context.h>
11  #include <random.h>
12  #include <test/util/setup_common.h>
13  #include <wallet/context.h>
14  #include <wallet/wallet.h>
15  
16  namespace wallet {
17  static void WalletCreate(benchmark::Bench& bench, bool encrypted)
18  {
19      auto test_setup = MakeNoLogFileContext<TestingSetup>();
20      FastRandomContext random;
21  
22      WalletContext context;
23      context.args = &test_setup->m_args;
24      context.chain = test_setup->m_node.chain.get();
25  
26      DatabaseOptions options;
27      options.require_format = DatabaseFormat::SQLITE;
28      options.require_create = true;
29      options.create_flags = WALLET_FLAG_DESCRIPTORS;
30  
31      if (encrypted) {
32          options.create_passphrase = random.rand256().ToString();
33      }
34  
35      DatabaseStatus status;
36      bilingual_str error_string;
37      std::vector<bilingual_str> warnings;
38  
39      fs::path wallet_path = test_setup->m_path_root / strprintf("test_wallet_%d", random.rand32()).c_str();
40      bench.run([&] {
41          auto wallet = CreateWallet(context, wallet_path.utf8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
42          assert(status == DatabaseStatus::SUCCESS);
43          assert(wallet != nullptr);
44  
45          // Cleanup
46          wallet.reset();
47          fs::remove_all(wallet_path);
48      });
49  }
50  
51  static void WalletCreatePlain(benchmark::Bench& bench) { WalletCreate(bench, /*encrypted=*/false); }
52  static void WalletCreateEncrypted(benchmark::Bench& bench) { WalletCreate(bench, /*encrypted=*/true); }
53  
54  #ifdef USE_SQLITE
55  BENCHMARK(WalletCreatePlain, benchmark::PriorityLevel::LOW);
56  BENCHMARK(WalletCreateEncrypted, benchmark::PriorityLevel::LOW);
57  #endif
58  
59  } // namespace wallet