Bitcoin ABC  0.29.2
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 <key.h>
8 #include <prevector.h>
9 #include <pubkey.h>
10 #include <random.h>
11 #include <util/system.h>
12 
13 #include <vector>
14 
15 static const int MIN_CORES = 2;
16 static const size_t BATCHES = 101;
17 static const size_t BATCH_SIZE = 30;
18 static const int PREVECTOR_SIZE = 28;
19 static const size_t QUEUE_BATCH_SIZE = 128;
20 
21 // This Benchmark tests the CheckQueue with a slightly realistic workload, where
22 // checks all contain a prevector that is indirect 50% of the time and there is
23 // a little bit of work done between calls to Add.
25  const ECCVerifyHandle verify_handle;
26  ECC_Start();
27 
28  struct PrevectorJob {
30  PrevectorJob() {}
31  explicit PrevectorJob(FastRandomContext &insecure_rand) {
32  p.resize(insecure_rand.randrange(PREVECTOR_SIZE * 2));
33  }
34  bool operator()() { return true; }
35  void swap(PrevectorJob &x) noexcept { p.swap(x.p); };
36  };
38  queue.StartWorkerThreads(std::max(MIN_CORES, GetNumCores()));
39 
40  // create all the data once, then submit copies in the benchmark.
41  FastRandomContext insecure_rand(true);
42  std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
43  for (auto &vChecks : vBatches) {
44  vChecks.reserve(BATCH_SIZE);
45  for (size_t x = 0; x < BATCH_SIZE; ++x) {
46  vChecks.emplace_back(insecure_rand);
47  }
48  }
49 
50  bench.minEpochIterations(10)
52  .unit("job")
53  .run([&] {
54  // Make insecure_rand here so that each iteration is identical.
55  CCheckQueueControl<PrevectorJob> control(&queue);
56  std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
57  for (auto &vChecks : vBatches) {
58  control.Add(vChecks);
59  }
60  // control waits for completion by RAII, but it is done explicitly
61  // here for clarity
62  control.Wait();
63  });
64  queue.StopWorkerThreads();
65  ECC_Stop();
66 }
BENCHMARK(CCheckQueueSpeedPrevectorJob)
static const int MIN_CORES
Definition: checkqueue.cpp:15
static const size_t BATCH_SIZE
Definition: checkqueue.cpp:17
static void CCheckQueueSpeedPrevectorJob(benchmark::Bench &bench)
Definition: checkqueue.cpp:24
static const int PREVECTOR_SIZE
Definition: checkqueue.cpp:18
static const size_t BATCHES
Definition: checkqueue.cpp:16
static const size_t QUEUE_BATCH_SIZE
Definition: checkqueue.cpp:19
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:203
void Add(std::vector< T > &vChecks)
Definition: checkqueue.h:229
Queue for verifications that have to be performed.
Definition: checkqueue.h:27
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:223
Fast randomness source.
Definition: random.h:156
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:231
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
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
void swap(prevector< N, T, Size, Diff > &other) noexcept
Definition: prevector.h:543
void resize(size_type new_size)
Definition: prevector.h:416
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:434
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:451
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1439