Repository: bitcoin/bitcoin
File: src/bench/rpc_blockchain.cpp (C++)

Code before:
    TestBlockAndIndex()
    {
        DataStream stream{benchmark::data::block413567};
        std::byte a{0};
        stream.write({&a, 1}); // Prevent compaction

        stream >> TX_WITH_WITNESS(block);

        blockHash = block.GetHash();
        blockindex.phashBlock = &blockHash;
        blockindex.nBits = 403014710;
    }
};

} // namespace

static void BlockToJsonVerbose(benchmark::Bench& bench)
{
    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, TxVerbosity::SHOW_DETAILS_AND_PREVOUT, pow_limit);
        ankerl::nanobench::doNotOptimizeAway(univalue);
    });
}

BENCHMARK(BlockToJsonVerbose, benchmark::PriorityLevel::HIGH);

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, benchmark::PriorityLevel::HIGH);


Review comment (suggestion):
the real RPC also [reads the block from disk](https://github.com/bitcoin/bitcoin/blob/master/src/rest.cpp#L322), we could bench that as well - instead of assuming it's already in memory (this will also enable additional optimization opportunities).

Edit: note that this will reduce the difference between before/after state a lot, but the measurements will reflect a real call more.

Code after:
    TestBlockAndIndex()
    {
        DataStream stream{benchmark::data::block413567};
        std::byte a{0};
        stream.write({&a, 1}); // Prevent compaction

        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::PriorityLevel::HIGH);
BENCHMARK(BlockToJsonVerbosity2, benchmark::PriorityLevel::HIGH);
BENCHMARK(BlockToJsonVerbosity3, benchmark::PriorityLevel::HIGH);

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([&] {