aboutsummaryrefslogtreecommitdiff
path: root/src/bench/rpc_blockchain.cpp
diff options
context:
space:
mode:
authorMartin Ankerl <martin.ankerl@gmail.com>2021-02-13 10:17:31 +0100
committerMartin Ankerl <martin.ankerl@gmail.com>2021-02-13 13:14:16 +0100
commite3e0a2432c587ee06e469c37ffae133b7ac55c77 (patch)
tree828097bd7ecf9e83450140729633274feeb8c4c4 /src/bench/rpc_blockchain.cpp
parentbf3189eda65d269fee6a70681ab390bd7cebf7d7 (diff)
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.
Diffstat (limited to 'src/bench/rpc_blockchain.cpp')
-rw-r--r--src/bench/rpc_blockchain.cpp48
1 files changed, 36 insertions, 12 deletions
diff --git a/src/bench/rpc_blockchain.cpp b/src/bench/rpc_blockchain.cpp
index 45ed9f60dc..78f8c6e6dc 100644
--- a/src/bench/rpc_blockchain.cpp
+++ b/src/bench/rpc_blockchain.cpp
@@ -12,25 +12,49 @@
#include <univalue.h>
-static void BlockToJsonVerbose(benchmark::Bench& bench)
-{
+namespace {
+
+struct TestBlockAndIndex {
TestingSetup test_setup{};
+ CBlock block{};
+ uint256 blockHash{};
+ CBlockIndex blockindex{};
- CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
- char a = '\0';
- stream.write(&a, 1); // Prevent compaction
+ TestBlockAndIndex()
+ {
+ CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
+ char a = '\0';
+ stream.write(&a, 1); // Prevent compaction
- CBlock block;
- stream >> block;
+ stream >> block;
- CBlockIndex blockindex;
- const uint256 blockHash = block.GetHash();
- blockindex.phashBlock = &blockHash;
- blockindex.nBits = 403014710;
+ blockHash = block.GetHash();
+ blockindex.phashBlock = &blockHash;
+ blockindex.nBits = 403014710;
+ }
+};
+} // namespace
+
+static void BlockToJsonVerbose(benchmark::Bench& bench)
+{
+ TestBlockAndIndex data;
bench.run([&] {
- (void)blockToJSON(block, &blockindex, &blockindex, /*verbose*/ true);
+ auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, /*verbose*/ true);
+ ankerl::nanobench::doNotOptimizeAway(univalue);
});
}
BENCHMARK(BlockToJsonVerbose);
+
+static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
+{
+ TestBlockAndIndex data;
+ auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, /*verbose*/ true);
+ bench.run([&] {
+ auto str = univalue.write();
+ ankerl::nanobench::doNotOptimizeAway(str);
+ });
+}
+
+BENCHMARK(BlockToJsonVerboseWrite);