Skip to content

Commit e3e0a24

Browse files
committed
Add benchmark to write JSON into a string
The benchmark BlockToJsonVerbose only tests generating (and destroying) the JSON data structure, but serializing into a string is also a performance critical aspect of the RPC calls. Also, use ankerl::nanobench::doNotOptimizeAway to make sure the compiler can't optimize the result of the calls away.
1 parent bf3189e commit e3e0a24

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/bench/rpc_blockchain.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,49 @@
1212

1313
#include <univalue.h>
1414

15-
static void BlockToJsonVerbose(benchmark::Bench& bench)
16-
{
15+
namespace {
16+
17+
struct TestBlockAndIndex {
1718
TestingSetup test_setup{};
19+
CBlock block{};
20+
uint256 blockHash{};
21+
CBlockIndex blockindex{};
1822

19-
CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
20-
char a = '\0';
21-
stream.write(&a, 1); // Prevent compaction
23+
TestBlockAndIndex()
24+
{
25+
CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
26+
char a = '\0';
27+
stream.write(&a, 1); // Prevent compaction
2228

23-
CBlock block;
24-
stream >> block;
29+
stream >> block;
2530

26-
CBlockIndex blockindex;
27-
const uint256 blockHash = block.GetHash();
28-
blockindex.phashBlock = &blockHash;
29-
blockindex.nBits = 403014710;
31+
blockHash = block.GetHash();
32+
blockindex.phashBlock = &blockHash;
33+
blockindex.nBits = 403014710;
34+
}
35+
};
3036

37+
} // namespace
38+
39+
static void BlockToJsonVerbose(benchmark::Bench& bench)
40+
{
41+
TestBlockAndIndex data;
3142
bench.run([&] {
32-
(void)blockToJSON(block, &blockindex, &blockindex, /*verbose*/ true);
43+
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, /*verbose*/ true);
44+
ankerl::nanobench::doNotOptimizeAway(univalue);
3345
});
3446
}
3547

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

0 commit comments

Comments
 (0)