Bitcoin Core  0.20.99
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 <boost/thread/thread.hpp>
14 
15 #include <vector>
16 
17 static const size_t BATCHES = 101;
18 static const size_t BATCH_SIZE = 30;
19 static const int PREVECTOR_SIZE = 28;
20 static const unsigned int QUEUE_BATCH_SIZE = 128;
21 
22 // This Benchmark tests the CheckQueue with a slightly realistic workload,
23 // where checks all contain a prevector that is indirect 50% of the time
24 // and there is a little bit of work done between calls to Add.
26 {
27  // We shouldn't ever be running with the checkqueue on a single core machine.
28  if (GetNumCores() <= 1) return;
29 
30  const ECCVerifyHandle verify_handle;
31  ECC_Start();
32 
33  struct PrevectorJob {
35  PrevectorJob(){
36  }
37  explicit PrevectorJob(FastRandomContext& insecure_rand){
38  p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2));
39  }
40  bool operator()()
41  {
42  return true;
43  }
44  void swap(PrevectorJob& x){p.swap(x.p);};
45  };
47  boost::thread_group tg;
48  // The main thread should be counted to prevent thread oversubscription, and
49  // to decrease the variance of benchmark results.
50  for (auto x = 0; x < GetNumCores() - 1; ++x) {
51  tg.create_thread([&]{queue.Thread();});
52  }
53 
54  // create all the data once, then submit copies in the benchmark.
55  FastRandomContext insecure_rand(true);
56  std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
57  for (auto& vChecks : vBatches) {
58  vChecks.reserve(BATCH_SIZE);
59  for (size_t x = 0; x < BATCH_SIZE; ++x)
60  vChecks.emplace_back(insecure_rand);
61  }
62 
63  bench.minEpochIterations(10).batch(BATCH_SIZE * BATCHES).unit("job").run([&] {
64  // Make insecure_rand here so that each iteration is identical.
65  CCheckQueueControl<PrevectorJob> control(&queue);
66  for (auto vChecks : vBatches) {
67  control.Add(vChecks);
68  }
69  // control waits for completion by RAII, but
70  // it is done explicitly here for clarity
71  control.Wait();
72  });
73  tg.interrupt_all();
74  tg.join_all();
75  ECC_Stop();
76 }
static const int PREVECTOR_SIZE
Definition: checkqueue.cpp:19
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:348
void resize(size_type new_size)
Definition: prevector.h:316
void Add(std::vector< T > &vChecks)
Definition: checkqueue.h:197
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1262
static const size_t BATCHES
Definition: checkqueue.cpp:17
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:17
ANKERL_NANOBENCH(NODISCARD) std Bench & minEpochIterations(uint64_t numIters) noexcept
Sets the minimum number of iterations each epoch should take.
static const unsigned int QUEUE_BATCH_SIZE
Definition: checkqueue.cpp:20
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:259
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
void swap(prevector< N, T, Size, Diff > &other)
Definition: prevector.h:461
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:365
Fast randomness source.
Definition: random.h:119
Queue for verifications that have to be performed.
Definition: checkqueue.h:30
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
Bench & unit(char const *unit)
Sets the operation unit.
static const size_t BATCH_SIZE
Definition: checkqueue.cpp:18
static void CCheckQueueSpeedPrevectorJob(benchmark::Bench &bench)
Definition: checkqueue.cpp:25
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:190
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
BENCHMARK(CCheckQueueSpeedPrevectorJob)