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