Skip to content

Commit

Permalink
merge bitcoin#21170: Add benchmark to write JSON into a string
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jan 25, 2024
1 parent 7590822 commit ce9ff9b
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/bench/rpc_blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,49 @@

#include <univalue.h>

static void BlockToJsonVerbose(benchmark::Bench& bench)
{
namespace {

struct TestBlockAndIndex {
TestingSetup test_setup{};
CBlock block{};
uint256 blockHash{};
CBlockIndex blockindex{};

CDataStream stream(benchmark::data::block813851, SER_NETWORK, PROTOCOL_VERSION);
char a = '\0';
stream.write(&a, 1); // Prevent compaction
TestBlockAndIndex()
{
CDataStream stream(benchmark::data::block813851, SER_NETWORK, PROTOCOL_VERSION);
char a = '\0';
stream.write(&a, 1); // Prevent compaction

CBlock block;
stream >> block;
stream >> block;

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

} // namespace

static void BlockToJsonVerbose(benchmark::Bench& bench)
{
TestBlockAndIndex data;
bench.run([&] {
(void)blockToJSON(block, &blockindex, &blockindex, *llmq::chainLocksHandler, *llmq::quorumInstantSendManager, /*verbose*/ true);
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, *llmq::chainLocksHandler, *llmq::quorumInstantSendManager, /*verbose*/ true);
ankerl::nanobench::doNotOptimizeAway(univalue);
});
}

BENCHMARK(BlockToJsonVerbose);

static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
{
TestBlockAndIndex data;
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, *llmq::chainLocksHandler, *llmq::quorumInstantSendManager, /*verbose*/ true);
bench.run([&] {
auto str = univalue.write();
ankerl::nanobench::doNotOptimizeAway(str);
});
}

BENCHMARK(BlockToJsonVerboseWrite);

0 comments on commit ce9ff9b

Please sign in to comment.