Bitcoin Core  24.99.0
P2P Digital Currency
checkqueue.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 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 size_t BATCHES = 101;
16 static const size_t BATCH_SIZE = 30;
17 static const int PREVECTOR_SIZE = 28;
18 static const unsigned int QUEUE_BATCH_SIZE = 128;
19 
20 // This Benchmark tests the CheckQueue with a slightly realistic workload,
21 // where checks all contain a prevector that is indirect 50% of the time
22 // and there is a little bit of work done between calls to Add.
24 {
25  // We shouldn't ever be running with the checkqueue on a single core machine.
26  if (GetNumCores() <= 1) return;
27 
28  const ECCVerifyHandle verify_handle;
29  ECC_Start();
30 
31  struct PrevectorJob {
33  PrevectorJob() = default;
34  explicit PrevectorJob(FastRandomContext& insecure_rand){
35  p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2));
36  }
37  bool operator()()
38  {
39  return true;
40  }
41  void swap(PrevectorJob& x) noexcept
42  {
43  p.swap(x.p);
44  };
45  };
47  // The main thread should be counted to prevent thread oversubscription, and
48  // to decrease the variance of benchmark results.
49  queue.StartWorkerThreads(GetNumCores() - 1);
50 
51  // create all the data once, then submit copies in the benchmark.
52  FastRandomContext insecure_rand(true);
53  std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
54  for (auto& vChecks : vBatches) {
55  vChecks.reserve(BATCH_SIZE);
56  for (size_t x = 0; x < BATCH_SIZE; ++x)
57  vChecks.emplace_back(insecure_rand);
58  }
59 
60  bench.minEpochIterations(10).batch(BATCH_SIZE * BATCHES).unit("job").run([&] {
61  // Make insecure_rand here so that each iteration is identical.
62  CCheckQueueControl<PrevectorJob> control(&queue);
63  for (auto vChecks : vBatches) {
64  control.Add(vChecks);
65  }
66  // control waits for completion by RAII, but
67  // it is done explicitly here for clarity
68  control.Wait();
69  });
70  queue.StopWorkerThreads();
71  ECC_Stop();
72 }
BENCHMARK(CCheckQueueSpeedPrevectorJob)
static const size_t BATCH_SIZE
Definition: checkqueue.cpp:16
static void CCheckQueueSpeedPrevectorJob(benchmark::Bench &bench)
Definition: checkqueue.cpp:23
static const int PREVECTOR_SIZE
Definition: checkqueue.cpp:17
static const size_t BATCHES
Definition: checkqueue.cpp:15
static const unsigned int QUEUE_BATCH_SIZE
Definition: checkqueue.cpp:18
ECC_Start()
Definition: key.cpp:392
ECC_Stop()
Definition: key.cpp:409
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:215
void Add(std::vector< T > &vChecks)
Definition: checkqueue.h:241
Queue for verifications that have to be performed.
Definition: checkqueue.h:31
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:336
Fast randomness source.
Definition: random.h:143
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:213
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:37
void swap(prevector< N, T, Size, Diff > &other) noexcept
Definition: prevector.h:455
void resize(size_type new_size)
Definition: prevector.h:318
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1413