rpc_blockchain.cpp raw
1 // Copyright (c) 2016-2022 The Limenka developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <bench/bench.h>
6 #include <bench/data/block413567.raw.h>
7 #include <chain.h>
8 #include <core_io.h>
9 #include <primitives/block.h>
10 #include <primitives/transaction.h>
11 #include <rpc/blockchain.h>
12 #include <serialize.h>
13 #include <span.h>
14 #include <streams.h>
15 #include <test/util/setup_common.h>
16 #include <uint256.h>
17 #include <univalue.h>
18 #include <validation.h>
19
20 #include <cstddef>
21 #include <memory>
22 #include <vector>
23
24 namespace {
25
26 struct TestBlockAndIndex {
27 const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
28 CBlock block{};
29 uint256 blockHash{};
30 CBlockIndex blockindex{};
31
32 TestBlockAndIndex()
33 {
34 DataStream stream{benchmark::data::block413567};
35 std::byte a{0};
36 stream.write({&a, 1}); // Prevent compaction
37
38 stream >> TX_WITH_WITNESS(block);
39
40 blockHash = block.GetHash();
41 blockindex.phashBlock = &blockHash;
42 blockindex.nBits = 403014710;
43 }
44 };
45
46 } // namespace
47
48 static void BlockToJson(benchmark::Bench& bench, TxVerbosity verbosity)
49 {
50 TestBlockAndIndex data;
51 const uint256 pow_limit{data.testing_setup->m_node.chainman->GetParams().GetConsensus().powLimit};
52 bench.run([&] {
53 auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, verbosity, pow_limit);
54 ankerl::nanobench::doNotOptimizeAway(univalue);
55 });
56 }
57
58 static void BlockToJsonVerbosity1(benchmark::Bench& bench)
59 {
60 BlockToJson(bench, TxVerbosity::SHOW_TXID);
61 }
62
63 static void BlockToJsonVerbosity2(benchmark::Bench& bench)
64 {
65 BlockToJson(bench, TxVerbosity::SHOW_DETAILS);
66 }
67
68 static void BlockToJsonVerbosity3(benchmark::Bench& bench)
69 {
70 BlockToJson(bench, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
71 }
72
73 BENCHMARK(BlockToJsonVerbosity1, benchmark::PriorityLevel::HIGH);
74 BENCHMARK(BlockToJsonVerbosity2, benchmark::PriorityLevel::HIGH);
75 BENCHMARK(BlockToJsonVerbosity3, benchmark::PriorityLevel::HIGH);
76
77 static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
78 {
79 TestBlockAndIndex data;
80 const uint256 pow_limit{data.testing_setup->m_node.chainman->GetParams().GetConsensus().powLimit};
81 auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT, pow_limit);
82 bench.run([&] {
83 auto str = univalue.write();
84 ankerl::nanobench::doNotOptimizeAway(str);
85 });
86 }
87
88 BENCHMARK(BlockToJsonVerboseWrite, benchmark::PriorityLevel::HIGH);
89