Skip to content

Commit bb02a88

Browse files
kwvgPastaPastaPasta
authored andcommitted
merge bitcoin#21170: Add benchmark to write JSON into a string
1 parent 3d1de43 commit bb02a88

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

src/bench/rpc_blockchain.cpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,49 @@
1515

1616
#include <univalue.h>
1717

18-
static void BlockToJsonVerbose(benchmark::Bench& bench) {
18+
namespace {
19+
20+
struct TestBlockAndIndex {
1921
TestingSetup test_setup{};
22+
CBlock block{};
23+
uint256 blockHash{};
24+
CBlockIndex blockindex{};
25+
26+
TestBlockAndIndex()
27+
{
28+
CDataStream stream(benchmark::data::block813851, SER_NETWORK, PROTOCOL_VERSION);
29+
char a = '\0';
30+
stream.write(&a, 1); // Prevent compaction
2031

21-
CDataStream stream(benchmark::data::block813851, SER_NETWORK, PROTOCOL_VERSION);
22-
char a = '\0';
23-
stream.write(&a, 1); // Prevent compaction
32+
stream >> block;
2433

25-
CBlock block;
26-
stream >> block;
34+
blockHash = block.GetHash();
35+
blockindex.phashBlock = &blockHash;
36+
blockindex.nBits = 403014710;
37+
}
38+
};
2739

28-
CBlockIndex blockindex;
29-
const uint256 blockHash = block.GetHash();
30-
blockindex.phashBlock = &blockHash;
31-
blockindex.nBits = 403014710;
40+
} // namespace
3241

42+
static void BlockToJsonVerbose(benchmark::Bench& bench)
43+
{
44+
TestBlockAndIndex data;
3345
bench.run([&] {
34-
(void)blockToJSON(block, &blockindex, &blockindex, *llmq::chainLocksHandler, *llmq::quorumInstantSendManager, /*verbose*/ true);
46+
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, *llmq::chainLocksHandler, *llmq::quorumInstantSendManager, /*verbose*/ true);
47+
ankerl::nanobench::doNotOptimizeAway(univalue);
3548
});
3649
}
3750

3851
BENCHMARK(BlockToJsonVerbose);
52+
53+
static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
54+
{
55+
TestBlockAndIndex data;
56+
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, *llmq::chainLocksHandler, *llmq::quorumInstantSendManager, /*verbose*/ true);
57+
bench.run([&] {
58+
auto str = univalue.write();
59+
ankerl::nanobench::doNotOptimizeAway(str);
60+
});
61+
}
62+
63+
BENCHMARK(BlockToJsonVerboseWrite);

0 commit comments

Comments
 (0)