Files
bitcoin/src/bench/rpc_blockchain.cpp
Lőrinc b8eb6c2081 refactor: use SpanReader in TestBlockAndIndex
`TestBlockAndIndex` still deserialized its fixed block fixture through `DataStream` and appended a dummy byte to avoid compaction after full consumption.

Use `SpanReader` for that fixture instead.
This removes the leftover dummy-byte workaround and reads the immutable fixture through a read-only view.
2026-04-21 12:25:44 +02:00

86 lines
2.5 KiB
C++

// Copyright (c) 2016-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bench/bench.h>
#include <bench/data/block413567.raw.h>
#include <chain.h>
#include <core_io.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <rpc/blockchain.h>
#include <serialize.h>
#include <span.h>
#include <streams.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <univalue.h>
#include <validation.h>
#include <cstddef>
#include <memory>
#include <vector>
namespace {
struct TestBlockAndIndex {
const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
CBlock block{};
uint256 blockHash{};
CBlockIndex blockindex{};
TestBlockAndIndex()
{
SpanReader stream{benchmark::data::block413567};
stream >> TX_WITH_WITNESS(block);
blockHash = block.GetHash();
blockindex.phashBlock = &blockHash;
blockindex.nBits = 403014710;
}
};
} // namespace
static void BlockToJson(benchmark::Bench& bench, TxVerbosity verbosity)
{
TestBlockAndIndex data;
const uint256 pow_limit{data.testing_setup->m_node.chainman->GetParams().GetConsensus().powLimit};
bench.run([&] {
auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, verbosity, pow_limit);
ankerl::nanobench::doNotOptimizeAway(univalue);
});
}
static void BlockToJsonVerbosity1(benchmark::Bench& bench)
{
BlockToJson(bench, TxVerbosity::SHOW_TXID);
}
static void BlockToJsonVerbosity2(benchmark::Bench& bench)
{
BlockToJson(bench, TxVerbosity::SHOW_DETAILS);
}
static void BlockToJsonVerbosity3(benchmark::Bench& bench)
{
BlockToJson(bench, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
}
BENCHMARK(BlockToJsonVerbosity1);
BENCHMARK(BlockToJsonVerbosity2);
BENCHMARK(BlockToJsonVerbosity3);
static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
{
TestBlockAndIndex data;
const uint256 pow_limit{data.testing_setup->m_node.chainman->GetParams().GetConsensus().powLimit};
auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT, pow_limit);
bench.run([&] {
auto str = univalue.write();
ankerl::nanobench::doNotOptimizeAway(str);
});
}
BENCHMARK(BlockToJsonVerboseWrite);