/ src / bench / readwriteblock.cpp
readwriteblock.cpp
 1  // Copyright (c) 2023 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/block413567.raw.h>
 7  #include <flatfile.h>
 8  #include <node/blockstorage.h>
 9  #include <primitives/block.h>
10  #include <primitives/transaction.h>
11  #include <serialize.h>
12  #include <span.h>
13  #include <streams.h>
14  #include <test/util/setup_common.h>
15  #include <validation.h>
16  
17  #include <cassert>
18  #include <cstdint>
19  #include <memory>
20  #include <vector>
21  
22  static CBlock CreateTestBlock()
23  {
24      DataStream stream{benchmark::data::block413567};
25      CBlock block;
26      stream >> TX_WITH_WITNESS(block);
27      return block;
28  }
29  
30  static void WriteBlockBench(benchmark::Bench& bench)
31  {
32      const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
33      auto& blockman{testing_setup->m_node.chainman->m_blockman};
34      const CBlock block{CreateTestBlock()};
35      bench.run([&] {
36          const auto pos{blockman.WriteBlock(block, 413'567)};
37          assert(!pos.IsNull());
38      });
39  }
40  
41  static void ReadBlockBench(benchmark::Bench& bench)
42  {
43      const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
44      auto& blockman{testing_setup->m_node.chainman->m_blockman};
45      const auto& test_block{CreateTestBlock()};
46      const auto& expected_hash{test_block.GetHash()};
47      const auto& pos{blockman.WriteBlock(test_block, 413'567)};
48      bench.run([&] {
49          CBlock block;
50          const auto success{blockman.ReadBlock(block, pos, expected_hash)};
51          assert(success);
52      });
53  }
54  
55  static void ReadRawBlockBench(benchmark::Bench& bench)
56  {
57      const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
58      auto& blockman{testing_setup->m_node.chainman->m_blockman};
59      const auto pos{blockman.WriteBlock(CreateTestBlock(), 413'567)};
60      std::vector<std::byte> block_data;
61      blockman.ReadRawBlock(block_data, pos); // warmup
62      bench.run([&] {
63          const auto success{blockman.ReadRawBlock(block_data, pos)};
64          assert(success);
65      });
66  }
67  
68  BENCHMARK(WriteBlockBench, benchmark::PriorityLevel::HIGH);
69  BENCHMARK(ReadBlockBench, benchmark::PriorityLevel::HIGH);
70  BENCHMARK(ReadRawBlockBench, benchmark::PriorityLevel::HIGH);