mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-26 11:13:23 -03:00
Add benchmark to write JSON into a string
The benchmark BlockToJsonVerbose only tests generating (and destroying) the JSON data structure, but serializing into a string is also a performance critical aspect of the RPC calls. Also, use ankerl::nanobench::doNotOptimizeAway to make sure the compiler can't optimize the result of the calls away.
This commit is contained in:
parent
bf3189eda6
commit
e3e0a2432c
1 changed files with 39 additions and 15 deletions
|
@ -12,25 +12,49 @@
|
||||||
|
|
||||||
#include <univalue.h>
|
#include <univalue.h>
|
||||||
|
|
||||||
static void BlockToJsonVerbose(benchmark::Bench& bench)
|
namespace {
|
||||||
{
|
|
||||||
TestingSetup test_setup{};
|
|
||||||
|
|
||||||
|
struct TestBlockAndIndex {
|
||||||
|
TestingSetup test_setup{};
|
||||||
|
CBlock block{};
|
||||||
|
uint256 blockHash{};
|
||||||
|
CBlockIndex blockindex{};
|
||||||
|
|
||||||
|
TestBlockAndIndex()
|
||||||
|
{
|
||||||
CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
|
||||||
char a = '\0';
|
char a = '\0';
|
||||||
stream.write(&a, 1); // Prevent compaction
|
stream.write(&a, 1); // Prevent compaction
|
||||||
|
|
||||||
CBlock block;
|
|
||||||
stream >> block;
|
stream >> block;
|
||||||
|
|
||||||
CBlockIndex blockindex;
|
blockHash = block.GetHash();
|
||||||
const uint256 blockHash = block.GetHash();
|
|
||||||
blockindex.phashBlock = &blockHash;
|
blockindex.phashBlock = &blockHash;
|
||||||
blockindex.nBits = 403014710;
|
blockindex.nBits = 403014710;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
static void BlockToJsonVerbose(benchmark::Bench& bench)
|
||||||
|
{
|
||||||
|
TestBlockAndIndex data;
|
||||||
bench.run([&] {
|
bench.run([&] {
|
||||||
(void)blockToJSON(block, &blockindex, &blockindex, /*verbose*/ true);
|
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, /*verbose*/ true);
|
||||||
|
ankerl::nanobench::doNotOptimizeAway(univalue);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
BENCHMARK(BlockToJsonVerbose);
|
BENCHMARK(BlockToJsonVerbose);
|
||||||
|
|
||||||
|
static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
|
||||||
|
{
|
||||||
|
TestBlockAndIndex data;
|
||||||
|
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, /*verbose*/ true);
|
||||||
|
bench.run([&] {
|
||||||
|
auto str = univalue.write();
|
||||||
|
ankerl::nanobench::doNotOptimizeAway(str);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
BENCHMARK(BlockToJsonVerboseWrite);
|
||||||
|
|
Loading…
Add table
Reference in a new issue