Skip to content

Commit

Permalink
bench: Move generated data to a dedicated translation unit
Browse files Browse the repository at this point in the history
Idea coming from btc@3d60a03a7cfb2d46b5f10633e9f6a9a36b8cb76f
  • Loading branch information
furszy committed Jun 11, 2021
1 parent d5f4dc0 commit 3110fa4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ bench_bench_pivx_SOURCES = \
bench/base58.cpp \
bench/checkblock.cpp \
bench/checkqueue.cpp \
bench/data.h \
bench/data.cpp \
bench/chacha20.cpp \
bench/crypto_hash.cpp \
bench/lockedpool.cpp \
Expand Down Expand Up @@ -55,7 +57,7 @@ CLEAN_BITCOIN_BENCH = bench/*.gcda bench/*.gcno $(GENERATED_TEST_FILES)

CLEANFILES += $(CLEAN_BITCOIN_BENCH)

bench/checkblock.cpp: bench/data/block2680960.raw.h
bench/data.cpp: bench/data/block2680960.raw.h

bitcoin_bench: $(BENCH_BINARY)

Expand Down
25 changes: 10 additions & 15 deletions src/bench/checkblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,42 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "bench.h"
#include "bench/bench.h"
#include "bench/data.h"

#include "validation.h"

namespace block_bench {
#include "bench/data/block2680960.raw.h"
}

// These are the two major time-sinks which happen after we have fully received
// a block off the wire, but before we can relay the block on to peers using
// compact block relay.

static void DeserializeBlockTest(benchmark::State& state)
{
CDataStream stream((const char*)block_bench::block2680960,
(const char*)&block_bench::block2680960[sizeof(block_bench::block2680960)],
SER_NETWORK, PROTOCOL_VERSION);
char a;
CDataStream stream(benchmark::data::block2680960, SER_NETWORK, PROTOCOL_VERSION);
char a = '\0';
stream.write(&a, 1); // Prevent compaction

while (state.KeepRunning()) {
CBlock block;
stream >> block;
assert(stream.Rewind(sizeof(block_bench::block2680960)));
bool rewound = stream.Rewind(benchmark::data::block2680960.size());
assert(rewound);
}
}

static void DeserializeAndCheckBlockTest(benchmark::State& state)
{
CDataStream stream((const char*)block_bench::block2680960,
(const char*)&block_bench::block2680960[sizeof(block_bench::block2680960)],
SER_NETWORK, PROTOCOL_VERSION);
char a;
CDataStream stream(benchmark::data::block2680960, SER_NETWORK, PROTOCOL_VERSION);
char a = '\0';
stream.write(&a, 1); // Prevent compaction

SelectParams(CBaseChainParams::MAIN);

while (state.KeepRunning()) {
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
stream >> block;
assert(stream.Rewind(sizeof(block_bench::block2680960)));
bool rewound = stream.Rewind(benchmark::data::block2680960.size());
assert(rewound);

CValidationState state;
assert(CheckBlock(block, state));
Expand Down
14 changes: 14 additions & 0 deletions src/bench/data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2019 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/data.h>

namespace benchmark {
namespace data {

#include <bench/data/block2680960.raw.h>
const std::vector<uint8_t> block2680960{block2680960_raw, block2680960_raw + sizeof(block2680960_raw) / sizeof(block2680960_raw[0])};

} // namespace data
} // namespace benchmark
19 changes: 19 additions & 0 deletions src/bench/data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_BENCH_DATA_H
#define BITCOIN_BENCH_DATA_H

#include <cstdint>
#include <vector>

namespace benchmark {
namespace data {

extern const std::vector<uint8_t> block2680960;

} // namespace data
} // namespace benchmark

#endif // BITCOIN_BENCH_DATA_H

0 comments on commit 3110fa4

Please sign in to comment.