Bitcoin ABC 0.33.10
P2P Digital Currency
checkqueue.cpp
Go to the documentation of this file.
1// Copyright (c) 2015 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <bench/bench.h>
6#include <checkqueue.h>
7#include <common/system.h>
8#include <key.h>
9#include <prevector.h>
10#include <pubkey.h>
11#include <random.h>
12#include <script/script.h>
13
14#include <vector>
15
16static const size_t BATCHES = 101;
17static const size_t BATCH_SIZE = 30;
18static const size_t QUEUE_BATCH_SIZE = 128;
19// This Benchmark tests the CheckQueue with a slightly realistic workload, where
20// checks all contain a prevector that is indirect 50% of the time and there is
21// a little bit of work done between calls to Add.
23 // We shouldn't ever be running with the checkqueue on a single core
24 // machine.
25 if (GetNumCores() <= 1) {
26 return;
27 }
28
29 ECC_Start();
30
31 struct PrevectorJob {
33 explicit PrevectorJob(FastRandomContext &insecure_rand) {
34 p.resize(insecure_rand.randrange(CScriptBase::STATIC_SIZE * 2));
35 }
36 std::optional<int> operator()() { return std::nullopt; }
37 };
38
39 // The main thread should be counted to prevent thread oversubscription, and
40 // to decrease the variance of benchmark results.
41 int worker_threads_num{GetNumCores() - 1};
42 CCheckQueue<PrevectorJob> queue{QUEUE_BATCH_SIZE, worker_threads_num};
43
44 // create all the data once, then submit copies in the benchmark.
45 FastRandomContext insecure_rand(true);
46 std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
47 for (auto &vChecks : vBatches) {
48 vChecks.reserve(BATCH_SIZE);
49 for (size_t x = 0; x < BATCH_SIZE; ++x) {
50 vChecks.emplace_back(insecure_rand);
51 }
52 }
53
54 bench.minEpochIterations(10)
56 .unit("job")
57 .run([&] {
58 // Make insecure_rand here so that each iteration is identical.
60 std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
61 for (auto &vChecks : vBatches) {
62 control.Add(std::move(vChecks));
63 }
64 // control waits for completion by RAII, but it is done explicitly
65 // here for clarity
66 control.Complete();
67 });
68 ECC_Stop();
69}
BENCHMARK(CCheckQueueSpeedPrevectorJob)
static const size_t BATCH_SIZE
Definition: checkqueue.cpp:17
static void CCheckQueueSpeedPrevectorJob(benchmark::Bench &bench)
Definition: checkqueue.cpp:22
static const size_t BATCHES
Definition: checkqueue.cpp:16
static const size_t QUEUE_BATCH_SIZE
Definition: checkqueue.cpp:18
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:208
std::optional< R > Complete()
Definition: checkqueue.h:225
void Add(std::vector< T > &&vChecks)
Definition: checkqueue.h:234
The verifications are represented by a type T, which must provide an operator(), returning an std::op...
Definition: checkqueue.h:32
Fast randomness source.
Definition: random.h:411
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Definition: random.h:266
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
Bench & unit(char const *unit)
Sets the operation unit.
ANKERL_NANOBENCH(NODISCARD) std Bench & minEpochIterations(uint64_t numIters) noexcept
Sets the minimum number of iterations each epoch should take.
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
static constexpr unsigned int STATIC_SIZE
Definition: prevector.h:42
void resize(size_type new_size)
Definition: prevector.h:428
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:433
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:450
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:114