1 // Copyright (c) 2016-2020 The Bitcoin Core 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.h>
7 
8 #include <rpc/blockchain.h>
9 #include <streams.h>
10 #include <test/util/setup_common.h>
11 #include <validation.h>
12 
13 #include <univalue.h>
14 
15 namespace {
16 
17 struct TestBlockAndIndex {
18     const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN)};
19     CBlock block{};
20     uint256 blockHash{};
21     CBlockIndex blockindex{};
22 
TestBlockAndIndex__anon754950430111::TestBlockAndIndex23     TestBlockAndIndex()
24     {
25         CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
26         char a = '\0';
27         stream.write(&a, 1); // Prevent compaction
28 
29         stream >> block;
30 
31         blockHash = block.GetHash();
32         blockindex.phashBlock = &blockHash;
33         blockindex.nBits = 403014710;
34     }
35 };
36 
37 } // namespace
38 
BlockToJsonVerbose(benchmark::Bench & bench)39 static void BlockToJsonVerbose(benchmark::Bench& bench)
40 {
41     TestBlockAndIndex data;
42     bench.run([&] {
43         auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, /*verbose*/ true);
44         ankerl::nanobench::doNotOptimizeAway(univalue);
45     });
46 }
47 
48 BENCHMARK(BlockToJsonVerbose);
49 
BlockToJsonVerboseWrite(benchmark::Bench & bench)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);
61