Skip to content

Commit 4af3547

Browse files
committed
bench: Use mock wallet database for wallet loading benchmark
Using in-memory only databases speeds up the benchmark, at the cost of real world accuracy.
1 parent 49910f2 commit 4af3547

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

src/bench/wallet_loading.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,19 @@
1717
#include <optional>
1818

1919
using wallet::CWallet;
20+
using wallet::DatabaseFormat;
2021
using wallet::DatabaseOptions;
21-
using wallet::DatabaseStatus;
2222
using wallet::ISMINE_SPENDABLE;
2323
using wallet::MakeWalletDatabase;
2424
using wallet::TxStateInactive;
2525
using wallet::WALLET_FLAG_DESCRIPTORS;
2626
using wallet::WalletContext;
27+
using wallet::WalletDatabase;
2728

28-
static const std::shared_ptr<CWallet> BenchLoadWallet(WalletContext& context, DatabaseOptions& options)
29+
static const std::shared_ptr<CWallet> BenchLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, DatabaseOptions& options)
2930
{
30-
DatabaseStatus status;
3131
bilingual_str error;
3232
std::vector<bilingual_str> warnings;
33-
auto database = MakeWalletDatabase("", options, status, error);
34-
assert(database);
3533
auto wallet = CWallet::Create(context, "", std::move(database), options.create_flags, error, warnings);
3634
NotifyWalletLoaded(context, wallet);
3735
if (context.chain) {
@@ -60,6 +58,30 @@ static void AddTx(CWallet& wallet)
6058
wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});
6159
}
6260

61+
static std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database, DatabaseOptions& options)
62+
{
63+
auto new_database = CreateMockWalletDatabase(options);
64+
65+
// Get a cursor to the original database
66+
auto batch = database.MakeBatch();
67+
batch->StartCursor();
68+
69+
// Get a batch for the new database
70+
auto new_batch = new_database->MakeBatch();
71+
72+
// Read all records from the original database and write them to the new one
73+
while (true) {
74+
CDataStream key(SER_DISK, CLIENT_VERSION);
75+
CDataStream value(SER_DISK, CLIENT_VERSION);
76+
bool complete;
77+
batch->ReadAtCursor(key, value, complete);
78+
if (complete) break;
79+
new_batch->Write(key, value);
80+
}
81+
82+
return new_database;
83+
}
84+
6385
static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
6486
{
6587
const auto test_setup = MakeNoLogFileContext<TestingSetup>();
@@ -72,21 +94,30 @@ static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
7294
// Setup the wallet
7395
// Loading the wallet will also create it
7496
DatabaseOptions options;
75-
if (!legacy_wallet) options.create_flags = WALLET_FLAG_DESCRIPTORS;
76-
auto wallet = BenchLoadWallet(context, options);
97+
if (legacy_wallet) {
98+
options.require_format = DatabaseFormat::BERKELEY;
99+
} else {
100+
options.create_flags = WALLET_FLAG_DESCRIPTORS;
101+
options.require_format = DatabaseFormat::SQLITE;
102+
}
103+
auto database = CreateMockWalletDatabase(options);
104+
auto wallet = BenchLoadWallet(std::move(database), context, options);
77105

78106
// Generate a bunch of transactions and addresses to put into the wallet
79107
for (int i = 0; i < 1000; ++i) {
80108
AddTx(*wallet);
81109
}
82110

111+
database = DuplicateMockDatabase(wallet->GetDatabase(), options);
112+
83113
// reload the wallet for the actual benchmark
84114
BenchUnloadWallet(std::move(wallet));
85115

86116
bench.epochs(5).run([&] {
87-
wallet = BenchLoadWallet(context, options);
117+
wallet = BenchLoadWallet(std::move(database), context, options);
88118

89119
// Cleanup
120+
database = DuplicateMockDatabase(wallet->GetDatabase(), options);
90121
BenchUnloadWallet(std::move(wallet));
91122
});
92123
}

0 commit comments

Comments
 (0)