/ 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 pos{blockman.WriteBlock(CreateTestBlock(), 413'567)};
46      CBlock block;
47      bench.run([&] {
48          const auto success{blockman.ReadBlock(block, pos)};
49          assert(success);
50      });
51  }
52  
53  static void ReadRawBlockBench(benchmark::Bench& bench)
54  {
55      const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
56      auto& blockman{testing_setup->m_node.chainman->m_blockman};
57      const auto pos{blockman.WriteBlock(CreateTestBlock(), 413'567)};
58      std::vector<uint8_t> block_data;
59      blockman.ReadRawBlock(block_data, pos); // warmup
60      bench.run([&] {
61          const auto success{blockman.ReadRawBlock(block_data, pos)};
62          assert(success);
63      });
64  }
65  
66  BENCHMARK(WriteBlockBench, benchmark::PriorityLevel::HIGH);
67  BENCHMARK(ReadBlockBench, benchmark::PriorityLevel::HIGH);
68  BENCHMARK(ReadRawBlockBench, benchmark::PriorityLevel::HIGH);