PIVX Core  5.6.99
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.h"
6 #include "util/system.h"
7 #include "checkqueue.h"
8 #include "prevector.h"
9 #include "random.h"
10 
11 #include <vector>
12 #include <boost/thread/thread.hpp>
13 
14 
15 static const int MIN_CORES = 2;
16 static const size_t BATCHES = 101;
17 static const size_t BATCH_SIZE = 30;
18 static const int PREVECTOR_SIZE = 28;
19 static const unsigned int QUEUE_BATCH_SIZE = 128;
20 
21 // This Benchmark tests the CheckQueue with a slightly realistic workload,
22 // where checks all contain a prevector that is indirect 50% of the time
23 // and there is a little bit of work done between calls to Add.
24 static void CCheckQueueSpeedPrevectorJob(benchmark::State& state)
25 {
26  struct PrevectorJob {
28  PrevectorJob(){
29  }
30  PrevectorJob(FastRandomContext& insecure_rand){
31  p.resize(insecure_rand.rand32() % (PREVECTOR_SIZE*2));
32  }
33  bool operator()()
34  {
35  return true;
36  }
37  void swap(PrevectorJob& x){p.swap(x.p);};
38  };
39  CCheckQueue<PrevectorJob> queue {QUEUE_BATCH_SIZE};
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  while (state.KeepRunning()) {
45  // Make insecure_rand here so that each iteration is identical.
46  FastRandomContext insecure_rand(true);
47  CCheckQueueControl<PrevectorJob> control(&queue);
48  std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
49  for (auto& vChecks : vBatches) {
50  vChecks.reserve(BATCH_SIZE);
51  for (size_t x = 0; x < BATCH_SIZE; ++x)
52  vChecks.emplace_back(insecure_rand);
53  control.Add(vChecks);
54  }
55  // control waits for completion by RAII, but
56  // it is done explicitly here for clarity
57  control.Wait();
58  }
59  tg.interrupt_all();
60  tg.join_all();
61 }
62 BENCHMARK(CCheckQueueSpeedPrevectorJob, 1400);
BENCHMARK(CCheckQueueSpeedPrevectorJob, 1400)
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:172
Queue for verifications that have to be performed.
Definition: checkqueue.h:29
Fast randomness source.
Definition: random.h:107
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:191
bool KeepRunning()
Definition: bench.h:69
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
void swap(prevector< N, T, Size, Diff > &other)
Definition: prevector.h:459
void resize(size_type new_size)
Definition: prevector.h:311
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1095