Skip to content

Commit 05e821e

Browse files
committed
Merge #21170: bench: Add benchmark to write JSON into a string
e3e0a24 Add benchmark to write JSON into a string (Martin Ankerl) Pull request description: 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. Extracts test setup into a `struct TestBlockAndIndex`, and uses it in both `BlockToJsonVerbose` and `BlockToJsonVerboseWrite`. Also, use `ankerl::nanobench::doNotOptimizeAway` to make sure the compiler can't optimize the result of the calls away. Here are benchmark results on my Intel i7-8700: | ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 71,807,017.00 | 13.93 | 0.4% | 555,782,961.00 | 220,788,645.00 | 2.517 | 102,279,341.00 | 0.4% | 0.80 | `BlockToJsonVerbose` | 27,916,835.00 | 35.82 | 0.1% | 235,084,034.00 | 89,033,525.00 | 2.640 | 42,911,139.00 | 0.3% | 0.32 | `BlockToJsonVerboseWrite` ACKs for top commit: laanwj: Code review ACK e3e0a24 Tree-SHA512: bc4d6d1588d47d4bd7af8e7908e44b8561bc391a2d73eccd7c0aa37fc40e8a9ce1fa1f3c29b416eef24a73c6bce3036839c0bbfe1b8dbd6d1bba3718b7ca5383
2 parents ad89812 + e3e0a24 commit 05e821e

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)