readwriteblock.cpp
1 // Copyright (c) 2023-present 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 CBlock block; 25 SpanReader{benchmark::data::block413567} >> TX_WITH_WITNESS(block); 26 return block; 27 } 28 29 static void WriteBlockBench(benchmark::Bench& bench) 30 { 31 const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)}; 32 auto& blockman{testing_setup->m_node.chainman->m_blockman}; 33 const CBlock block{CreateTestBlock()}; 34 bench.run([&] { 35 const auto pos{blockman.WriteBlock(block, 413'567)}; 36 assert(!pos.IsNull()); 37 }); 38 } 39 40 static void ReadBlockBench(benchmark::Bench& bench) 41 { 42 const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)}; 43 auto& blockman{testing_setup->m_node.chainman->m_blockman}; 44 const auto& test_block{CreateTestBlock()}; 45 const auto& expected_hash{test_block.GetHash()}; 46 const auto& pos{blockman.WriteBlock(test_block, 413'567)}; 47 bench.run([&] { 48 CBlock block; 49 const auto success{blockman.ReadBlock(block, pos, expected_hash)}; 50 assert(success); 51 }); 52 } 53 54 static void ReadRawBlockBench(benchmark::Bench& bench) 55 { 56 const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)}; 57 auto& blockman{testing_setup->m_node.chainman->m_blockman}; 58 const auto pos{blockman.WriteBlock(CreateTestBlock(), 413'567)}; 59 bench.run([&] { 60 const auto res{blockman.ReadRawBlock(pos)}; 61 assert(res); 62 }); 63 } 64 65 BENCHMARK(WriteBlockBench); 66 BENCHMARK(ReadBlockBench); 67 BENCHMARK(ReadRawBlockBench);