Bitcoin Core
31.99.0
P2P Digital Currency
src
bench
readwriteblock.cpp
Go to the documentation of this file.
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 <
streams.h
>
13
#include <
sync.h
>
14
#include <
test/util/setup_common.h
>
15
#include <
uint256.h
>
16
#include <
util/check.h
>
17
#include <validation.h>
18
19
#include <memory>
20
#include <optional>
21
#include <span>
22
23
static
CBlock
CreateTestBlock
()
24
{
25
CBlock
block;
26
SpanReader
{benchmark::data::block413567} >>
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
LOCK
(
::cs_main
);
37
const
auto
pos{blockman.WriteBlock(block, 413'567)};
38
assert
(!pos.IsNull());
39
});
40
}
41
42
static
void
ReadBlockBench
(
benchmark::Bench
& bench)
43
{
44
const
auto
testing_setup{MakeNoLogFileContext<const TestingSetup>(
ChainType::MAIN
)};
45
auto
& blockman{testing_setup->m_node.chainman->m_blockman};
46
const
auto
& test_block{
CreateTestBlock
()};
47
const
auto
& expected_hash{test_block.
GetHash
()};
48
const
auto
& pos{
WITH_LOCK
(
::cs_main
,
return
blockman.WriteBlock(test_block, 413'567))};
49
bench.
run
([&] {
50
CBlock
block;
51
const
auto
success{blockman.ReadBlock(block, pos, expected_hash)};
52
assert
(success);
53
});
54
}
55
56
static
void
ReadRawBlockBench
(
benchmark::Bench
& bench)
57
{
58
const
auto
testing_setup{MakeNoLogFileContext<const TestingSetup>(
ChainType::MAIN
)};
59
auto
& blockman{testing_setup->m_node.chainman->m_blockman};
60
const
auto
pos{
WITH_LOCK
(
::cs_main
,
return
blockman.WriteBlock(
CreateTestBlock
(), 413'567))};
61
bench.
run
([&] {
62
const
auto
res{blockman.ReadRawBlock(pos)};
63
assert
(res);
64
});
65
}
66
67
BENCHMARK
(
WriteBlockBench
);
68
BENCHMARK
(
ReadBlockBench
);
69
BENCHMARK
(
ReadRawBlockBench
);
bench.h
block.h
blockstorage.h
ChainType::MAIN
@ MAIN
check.h
CBlockHeader::GetHash
uint256 GetHash() const
Definition:
block.cpp:14
CBlock
Definition:
block.h:74
SpanReader
Minimal stream for reading from an existing byte array by std::span.
Definition:
streams.h:83
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition:
nanobench.h:649
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition:
nanobench.h:1308
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition:
cs_main.cpp:8
flatfile.h
transaction.h
TX_WITH_WITNESS
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition:
transaction.h:180
CreateTestBlock
static CBlock CreateTestBlock()
Definition:
readwriteblock.cpp:23
BENCHMARK
BENCHMARK(WriteBlockBench)
ReadRawBlockBench
static void ReadRawBlockBench(benchmark::Bench &bench)
Definition:
readwriteblock.cpp:56
WriteBlockBench
static void WriteBlockBench(benchmark::Bench &bench)
Definition:
readwriteblock.cpp:30
ReadBlockBench
static void ReadBlockBench(benchmark::Bench &bench)
Definition:
readwriteblock.cpp:42
serialize.h
setup_common.h
streams.h
sync.h
LOCK
#define LOCK(cs)
Definition:
sync.h:268
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition:
sync.h:299
uint256.h
assert
assert(!tx.IsCoinBase())
Generated on Tue Jun 23 2026 20:06:25 for Bitcoin Core by
1.9.4