Bitcoin Core  0.19.99
P2P Digital Currency
checkqueue.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2019 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 <util/system.h>
7 #include <checkqueue.h>
8 #include <prevector.h>
9 #include <vector>
10 #include <boost/thread/thread.hpp>
11 #include <random.h>
12 
13 
14 static const int MIN_CORES = 2;
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  struct PrevectorJob {
27  PrevectorJob(){
28  }
29  explicit PrevectorJob(FastRandomContext& insecure_rand){
30  p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2));
31  }
32  bool operator()()
33  {
34  return true;
35  }
36  void swap(PrevectorJob& x){p.swap(x.p);};
37  };
39  boost::thread_group tg;
40  for (auto x = 0; x < std::max(MIN_CORES, GetNumCores()); ++x) {
41  tg.create_thread([&]{queue.Thread();});
42  }
43  while (state.KeepRunning()) {
44  // Make insecure_rand here so that each iteration is identical.
45  FastRandomContext insecure_rand(true);
46  CCheckQueueControl<PrevectorJob> control(&queue);
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  control.Add(vChecks);
53  }
54  // control waits for completion by RAII, but
55  // it is done explicitly here for clarity
56  control.Wait();
57  }
58  tg.interrupt_all();
59  tg.join_all();
60 }
static const int MIN_CORES
Definition: checkqueue.cpp:14
static const int PREVECTOR_SIZE
Definition: checkqueue.cpp:17
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:1123
static const size_t BATCHES
Definition: checkqueue.cpp:15
bool KeepRunning()
Definition: bench.h:72
static void CCheckQueueSpeedPrevectorJob(benchmark::State &state)
Definition: checkqueue.cpp:23
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:17
static const unsigned int QUEUE_BATCH_SIZE
Definition: checkqueue.cpp:18
void swap(prevector< N, T, Size, Diff > &other)
Definition: prevector.h:456
Fast randomness source.
Definition: random.h:106
Queue for verifications that have to be performed.
Definition: checkqueue.h:30
BENCHMARK(CCheckQueueSpeedPrevectorJob, 1400)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
static const size_t BATCH_SIZE
Definition: checkqueue.cpp:16
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:173