Raven Core  3.0.0
P2P Digital Currency
checkqueue.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "bench.h"
7 #include "util.h"
8 #include "validation.h"
9 #include "checkqueue.h"
10 #include "prevector.h"
11 #include <vector>
12 #include <boost/thread/thread.hpp>
13 #include "random.h"
14 
15 
16 // This Benchmark tests the CheckQueue with the lightest
17 // weight Checks, so it should make any lock contention
18 // particularly visible
19 static const int MIN_CORES = 2;
20 static const size_t BATCHES = 101;
21 static const size_t BATCH_SIZE = 30;
22 static const int PREVECTOR_SIZE = 28;
23 static const int QUEUE_BATCH_SIZE = 128;
24 static void CCheckQueueSpeed(benchmark::State& state)
25 {
26  struct FakeJobNoWork {
27  bool operator()()
28  {
29  return true;
30  }
31  void swap(FakeJobNoWork& x){};
32  };
33  CCheckQueue<FakeJobNoWork> queue {QUEUE_BATCH_SIZE};
34  boost::thread_group tg;
35  for (auto x = 0; x < std::max(MIN_CORES, GetNumCores()); ++x) {
36  tg.create_thread([&]{queue.Thread();});
37  }
38  while (state.KeepRunning()) {
39  CCheckQueueControl<FakeJobNoWork> control(&queue);
40 
41  // We call Add a number of times to simulate the behavior of adding
42  // a block of transactions at once.
43 
44  std::vector<std::vector<FakeJobNoWork>> vBatches(BATCHES);
45  for (auto& vChecks : vBatches) {
46  vChecks.resize(BATCH_SIZE);
47  }
48  for (auto& vChecks : vBatches) {
49  // We can't make vChecks in the inner loop because we want to measure
50  // the cost of getting the memory to each thread and we might get the same
51  // memory
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 }
61 
62 // This Benchmark tests the CheckQueue with a slightly realistic workload,
63 // where checks all contain a prevector that is indirect 50% of the time
64 // and there is a little bit of work done between calls to Add.
65 static void CCheckQueueSpeedPrevectorJob(benchmark::State& state)
66 {
67  struct PrevectorJob {
69  PrevectorJob(){
70  }
71  explicit PrevectorJob(FastRandomContext& insecure_rand){
72  p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2));
73  }
74  bool operator()()
75  {
76  return true;
77  }
78  void swap(PrevectorJob& x){p.swap(x.p);};
79  };
80  CCheckQueue<PrevectorJob> queue {QUEUE_BATCH_SIZE};
81  boost::thread_group tg;
82  for (auto x = 0; x < std::max(MIN_CORES, GetNumCores()); ++x) {
83  tg.create_thread([&]{queue.Thread();});
84  }
85  while (state.KeepRunning()) {
86  // Make insecure_rand here so that each iteration is identical.
87  FastRandomContext insecure_rand(true);
88  CCheckQueueControl<PrevectorJob> control(&queue);
89  std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
90  for (auto& vChecks : vBatches) {
91  vChecks.reserve(BATCH_SIZE);
92  for (size_t x = 0; x < BATCH_SIZE; ++x)
93  vChecks.emplace_back(insecure_rand);
94  control.Add(vChecks);
95  }
96  // control waits for completion by RAII, but
97  // it is done explicitly here for clarity
98  control.Wait();
99  }
100  tg.interrupt_all();
101  tg.join_all();
102 }
103 BENCHMARK(CCheckQueueSpeed);
104 BENCHMARK(CCheckQueueSpeedPrevectorJob);
void resize(size_type new_size)
Definition: prevector.h:317
uint64_t randrange(uint64_t range)
Generate a random integer in the range [0..range).
Definition: random.h:104
bool KeepRunning()
Definition: bench.cpp:44
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:18
void swap(prevector< N, T, Size, Diff > &other)
Definition: prevector.h:441
Fast randomness source.
Definition: random.h:45
Queue for verifications that have to be performed.
Definition: checkqueue.h:31
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
BENCHMARK(CCheckQueueSpeed)
int GetNumCores()
Return the number of physical cores available on the current system.
Definition: util.cpp:908