Skip to content

Commit

Permalink
Merge #15788: test: Unify testing setups for fuzz, bench, and unit tests
Browse files Browse the repository at this point in the history
faf4000 scripted-diff: Bump copyright headers in test, bench (MarcoFalke)
fa82190 scripted-diff: Rename test_bitcoin to test/setup_common (MarcoFalke)
fa8685d test: Use test_bitcoin setup in bench, Add test utils (MarcoFalke)
666696b test: Have segwit always active in (Basic)TestingSetup (MarcoFalke)

Pull request description:

  Now that the fuzz tests can use the BasicTestingSetup [1], do the same for bench.

  Also move some duplicate code to a common "test/util" module.

  [1]:  fuzz: Link BasicTestingSetup (shared with unit tests) #15504

ACKs for commit faf400:
  jonatack:
    ACK faf4000

Tree-SHA512: 8ac5692e72cf50e460958f291643ae6b8bb04d5c1331ed50dce9eb4e9457e5a925144c532c42b360a26707e11eeece74aab27db8c76ab9a429b9dd7167e7cdc4
  • Loading branch information
MarcoFalke committed Apr 15, 2019
2 parents 2209b3b + faf4000 commit 78295e9
Show file tree
Hide file tree
Showing 94 changed files with 316 additions and 297 deletions.
4 changes: 4 additions & 0 deletions build_msvc/bench_bitcoin/bench_bitcoin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\test\util.h" />
<ClCompile Include="..\..\src\test\util.cpp" />
<ClCompile Include="..\..\src\test\setup_common.h" />
<ClCompile Include="..\..\src\test\setup_common.cpp" />
<ClCompile Include="..\..\src\bench\base58.cpp" />
<ClCompile Include="..\..\src\bench\bech32.cpp" />
<ClCompile Include="..\..\src\bench\bench.cpp" />
Expand Down
2 changes: 1 addition & 1 deletion build_msvc/test_bitcoin/test_bitcoin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<ClCompile Include="..\..\src\test\*_properties.cpp" />
<ClCompile Include="..\..\src\test\gen\*_gen.cpp" />
<ClCompile Include="..\..\src\wallet\test\*_tests.cpp" />
<ClCompile Include="..\..\src\test\test_bitcoin.cpp" />
<ClCompile Include="..\..\src\test\setup_common.cpp" />
<ClCompile Include="..\..\src\test\main.cpp" />
<ClCompile Include="..\..\src\wallet\test\*_fixture.cpp" />
</ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ bench_bench_bitcoin_SOURCES = \
bench/bech32.cpp \
bench/lockedpool.cpp \
bench/poly1305.cpp \
bench/prevector.cpp
bench/prevector.cpp \
test/setup_common.h \
test/setup_common.cpp \
test/util.h \
test/util.cpp

nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES)

Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.qttest.include
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ TEST_QT_H = \
qt/test/wallettests.h

TEST_BITCOIN_CPP = \
test/test_bitcoin.cpp
test/setup_common.cpp

TEST_BITCOIN_H = \
test/test_bitcoin.h
test/setup_common.h

qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)
Expand Down
8 changes: 4 additions & 4 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.r

BITCOIN_TEST_SUITE = \
test/main.cpp \
test/test_bitcoin.h \
test/test_bitcoin.cpp
test/setup_common.h \
test/setup_common.cpp

FUZZ_SUITE = \
test/test_bitcoin.h \
test/test_bitcoin.cpp \
test/setup_common.h \
test/setup_common.cpp \
test/fuzz/fuzz.cpp \
test/fuzz/fuzz.h

Expand Down
19 changes: 15 additions & 4 deletions src/bench/bench.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Copyright (c) 2015-2018 The Bitcoin Core developers
// Copyright (c) 2015-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/bench.h>

#include <chainparams.h>
#include <test/setup_common.h>
#include <validation.h>

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <regex>
#include <iostream>
#include <numeric>
#include <regex>

void benchmark::ConsolePrinter::header()
{
Expand Down Expand Up @@ -108,6 +112,13 @@ void benchmark::BenchRunner::RunAll(Printer& printer, uint64_t num_evals, double
printer.header();

for (const auto& p : benchmarks()) {
TestingSetup test{CBaseChainParams::REGTEST};
{
assert(::chainActive.Height() == 0);
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), Params().GetConsensus())};
assert(witness_enabled);
}

if (!std::regex_match(p.first, baseMatch, reFilter)) {
continue;
}
Expand Down
26 changes: 2 additions & 24 deletions src/bench/bench_bitcoin.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// Copyright (c) 2015-2018 The Bitcoin Core developers
// Copyright (c) 2015-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/bench.h>

#include <crypto/sha256.h>
#include <key.h>
#include <util/system.h>
#include <util/strencodings.h>
#include <validation.h>
#include <util/system.h>

#include <memory>

const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;

static const int64_t DEFAULT_BENCH_EVALUATIONS = 5;
static const char* DEFAULT_BENCH_FILTER = ".*";
static const char* DEFAULT_BENCH_SCALING = "1.0";
Expand All @@ -36,14 +33,6 @@ static void SetupBenchArgs()
gArgs.AddArg("-plot-height=<x>", strprintf("Plot height in pixel (default: %u)", DEFAULT_PLOT_HEIGHT), false, OptionsCategory::OPTIONS);
}

static fs::path SetDataDir()
{
fs::path ret = fs::temp_directory_path() / "bench_bitcoin" / fs::unique_path();
fs::create_directories(ret);
gArgs.ForceSetArg("-datadir", ret.string());
return ret;
}

int main(int argc, char** argv)
{
SetupBenchArgs();
Expand All @@ -59,13 +48,6 @@ int main(int argc, char** argv)
return EXIT_SUCCESS;
}

// Set the datadir after parsing the bench options
const fs::path bench_datadir{SetDataDir()};

SHA256AutoDetect();
ECC_Start();
SetupEnvironment();

int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING);
Expand All @@ -88,9 +70,5 @@ int main(int argc, char** argv)

benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);

fs::remove_all(bench_datadir);

ECC_Stop();

return EXIT_SUCCESS;
}
75 changes: 2 additions & 73 deletions src/bench/block_assemble.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,18 @@
// Copyright (c) 2011-2018 The Bitcoin Core developers
// Copyright (c) 2011-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/bench.h>
#include <chainparams.h>
#include <coins.h>
#include <consensus/merkle.h>
#include <consensus/validation.h>
#include <crypto/sha256.h>
#include <miner.h>
#include <policy/policy.h>
#include <pow.h>
#include <scheduler.h>
#include <txdb.h>
#include <test/util.h>
#include <txmempool.h>
#include <util/time.h>
#include <validation.h>
#include <validationinterface.h>

#include <boost/thread.hpp>

#include <list>
#include <vector>

static std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
{
auto block = std::make_shared<CBlock>(
BlockAssembler{Params()}
.CreateNewBlock(coinbase_scriptPubKey)
->block);

block->nTime = ::chainActive.Tip()->GetMedianTimePast() + 1;
block->hashMerkleRoot = BlockMerkleRoot(*block);

return block;
}


static CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
{
auto block = PrepareBlock(coinbase_scriptPubKey);

while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
++block->nNonce;
assert(block->nNonce);
}

bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
assert(processed);

return CTxIn{block->vtx[0]->GetHash(), 0};
}


static void AssembleBlock(benchmark::State& state)
{
const std::vector<unsigned char> op_true{OP_TRUE};
Expand All @@ -64,32 +24,6 @@ static void AssembleBlock(benchmark::State& state)

const CScript SCRIPT_PUB{CScript(OP_0) << std::vector<unsigned char>{witness_program.begin(), witness_program.end()}};

// Switch to regtest so we can mine faster
// Also segwit is active, so we can include witness transactions
SelectParams(CBaseChainParams::REGTEST);

InitScriptExecutionCache();

boost::thread_group thread_group;
CScheduler scheduler;
{
LOCK(cs_main);
::pblocktree.reset(new CBlockTreeDB(1 << 20, true));
::pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
::pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
}
{
const CChainParams& chainparams = Params();
thread_group.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler));
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
LoadGenesisBlock(chainparams);
CValidationState state;
ActivateBestChain(state, chainparams);
assert(::chainActive.Tip() != nullptr);
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), chainparams.GetConsensus())};
assert(witness_enabled);
}

// Collect some loose transactions that spend the coinbases of our mined blocks
constexpr size_t NUM_BLOCKS{200};
std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
Expand All @@ -114,11 +48,6 @@ static void AssembleBlock(benchmark::State& state)
while (state.KeepRunning()) {
PrepareBlock(SCRIPT_PUB);
}

thread_group.interrupt_all();
thread_group.join_all();
GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler();
}

BENCHMARK(AssembleBlock, 700);
37 changes: 2 additions & 35 deletions src/bench/duplicate_inputs.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2011-2018 The Bitcoin Core developers
// Copyright (c) 2011-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.

Expand All @@ -10,15 +10,11 @@
#include <miner.h>
#include <policy/policy.h>
#include <pow.h>
#include <scheduler.h>
#include <txdb.h>
#include <test/util.h>
#include <txmempool.h>
#include <util/time.h>
#include <validation.h>
#include <validationinterface.h>

#include <boost/thread.hpp>

#include <list>
#include <vector>

Expand All @@ -27,31 +23,7 @@ static void DuplicateInputs(benchmark::State& state)
{
const CScript SCRIPT_PUB{CScript(OP_TRUE)};

// Switch to regtest so we can mine faster
// Also segwit is active, so we can include witness transactions
SelectParams(CBaseChainParams::REGTEST);

InitScriptExecutionCache();

boost::thread_group thread_group;
CScheduler scheduler;
const CChainParams& chainparams = Params();
{
LOCK(cs_main);
::pblocktree.reset(new CBlockTreeDB(1 << 20, true));
::pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
::pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
}
{
thread_group.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler));
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
LoadGenesisBlock(chainparams);
CValidationState cvstate;
ActivateBestChain(cvstate, chainparams);
assert(::chainActive.Tip() != nullptr);
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), chainparams.GetConsensus())};
assert(witness_enabled);
}

CBlock block{};
CMutableTransaction coinbaseTx{};
Expand Down Expand Up @@ -92,11 +64,6 @@ static void DuplicateInputs(benchmark::State& state)
assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
}

thread_group.interrupt_all();
thread_group.join_all();
GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler();
}

BENCHMARK(DuplicateInputs, 10);
2 changes: 1 addition & 1 deletion src/qt/test/addressbooktests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <qt/test/addressbooktests.h>
#include <qt/test/util.h>
#include <test/test_bitcoin.h>
#include <test/setup_common.h>

#include <interfaces/chain.h>
#include <interfaces/node.h>
Expand Down
2 changes: 1 addition & 1 deletion src/qt/test/rpcnestedtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <rpc/register.h>
#include <rpc/server.h>
#include <qt/rpcconsole.h>
#include <test/test_bitcoin.h>
#include <test/setup_common.h>
#include <univalue.h>
#include <util/system.h>

Expand Down
2 changes: 1 addition & 1 deletion src/qt/test/wallettests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <qt/transactionview.h>
#include <qt/walletmodel.h>
#include <key_io.h>
#include <test/test_bitcoin.h>
#include <test/setup_common.h>
#include <validation.h>
#include <wallet/wallet.h>
#include <qt/overviewpage.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ unit tests as possible).

The build system is setup to compile an executable called `test_bitcoin`
that runs all of the unit tests. The main source file is called
test_bitcoin.cpp. To add a new unit test file to our test suite you need
setup_common.cpp. To add a new unit test file to our test suite you need
to add the file to `src/Makefile.test.include`. The pattern is to create
one test file for each class or source file for which you want to create
unit tests. The file naming convention is `<source_filename>_tests.cpp`
Expand Down
4 changes: 2 additions & 2 deletions src/test/addrman_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) 2012-2018 The Bitcoin Core developers
// Copyright (c) 2012-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 <addrman.h>
#include <test/test_bitcoin.h>
#include <test/setup_common.h>
#include <string>
#include <boost/test/unit_test.hpp>

Expand Down
4 changes: 2 additions & 2 deletions src/test/allocator_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) 2012-2018 The Bitcoin Core developers
// Copyright (c) 2012-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 <util/system.h>

#include <support/allocators/secure.h>
#include <test/test_bitcoin.h>
#include <test/setup_common.h>

#include <memory>

Expand Down
Loading

0 comments on commit 78295e9

Please sign in to comment.