|
| 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.h" |
| 7 | +#include "validation.h" |
| 8 | +#include "checkqueue.h" |
| 9 | +#include "prevector.h" |
| 10 | +#include <vector> |
| 11 | +#include <boost/thread/thread.hpp> |
| 12 | +#include "random.h" |
| 13 | + |
| 14 | + |
| 15 | +// This Benchmark tests the CheckQueue with the lightest |
| 16 | +// weight Checks, so it should make any lock contention |
| 17 | +// particularly visible |
| 18 | +static void CCheckQueueSpeed(benchmark::State& state) |
| 19 | +{ |
| 20 | + struct FakeJobNoWork { |
| 21 | + bool operator()() |
| 22 | + { |
| 23 | + return true; |
| 24 | + } |
| 25 | + void swap(FakeJobNoWork& x){}; |
| 26 | + }; |
| 27 | + CCheckQueue<FakeJobNoWork> queue {128}; |
| 28 | + boost::thread_group tg; |
| 29 | + for (auto x = 0; x < std::max(2, GetNumCores()); ++x) { |
| 30 | + tg.create_thread([&]{queue.Thread();}); |
| 31 | + } |
| 32 | + while (state.KeepRunning()) { |
| 33 | + CCheckQueueControl<FakeJobNoWork> control(&queue); |
| 34 | + // We can make vChecks out of the loop because calling Add doesn't |
| 35 | + // change the size of the vector. |
| 36 | + std::vector<FakeJobNoWork> vChecks; |
| 37 | + vChecks.resize(30); |
| 38 | + |
| 39 | + // We call Add a number of times to simulate the behavior of adding |
| 40 | + // a block of transactions at once. |
| 41 | + for (size_t j = 0; j < 101; ++j) { |
| 42 | + control.Add(vChecks); |
| 43 | + } |
| 44 | + // control waits for completion by RAII, but |
| 45 | + // it is done explicitly here for clarity |
| 46 | + control.Wait(); |
| 47 | + } |
| 48 | + tg.interrupt_all(); |
| 49 | + tg.join_all(); |
| 50 | +} |
| 51 | + |
| 52 | +// This Benchmark tests the CheckQueue with a slightly realistic workload, |
| 53 | +// where checks all contain a prevector that is indirect 50% of the time |
| 54 | +// and there is a little bit of work done between calls to Add. |
| 55 | +static void CCheckQueueSpeedPrevectorJob(benchmark::State& state) |
| 56 | +{ |
| 57 | + struct PrevectorJob { |
| 58 | + prevector<28, uint8_t> p; |
| 59 | + PrevectorJob(){ |
| 60 | + } |
| 61 | + PrevectorJob(FastRandomContext& insecure_rand){ |
| 62 | + p.resize(insecure_rand.rand32() % 56); |
| 63 | + } |
| 64 | + bool operator()() |
| 65 | + { |
| 66 | + return true; |
| 67 | + } |
| 68 | + void swap(PrevectorJob& x){p.swap(x.p);}; |
| 69 | + }; |
| 70 | + CCheckQueue<PrevectorJob> queue {128}; |
| 71 | + boost::thread_group tg; |
| 72 | + for (auto x = 0; x < std::max(2, GetNumCores()); ++x) { |
| 73 | + tg.create_thread([&]{queue.Thread();}); |
| 74 | + } |
| 75 | + while (state.KeepRunning()) { |
| 76 | + // Make insecure_rand here so that each iteration is identical. |
| 77 | + FastRandomContext insecure_rand(true); |
| 78 | + CCheckQueueControl<PrevectorJob> control(&queue); |
| 79 | + for (size_t j = 0; j < 101; ++j) { |
| 80 | + std::vector<PrevectorJob> vChecks; |
| 81 | + vChecks.reserve(30); |
| 82 | + for (auto x = 0; x < 30; ++x) |
| 83 | + vChecks.emplace_back(insecure_rand); |
| 84 | + control.Add(vChecks); |
| 85 | + } |
| 86 | + // control waits for completion by RAII, but |
| 87 | + // it is done explicitly here for clarity |
| 88 | + control.Wait(); |
| 89 | + } |
| 90 | + tg.interrupt_all(); |
| 91 | + tg.join_all(); |
| 92 | +} |
| 93 | +BENCHMARK(CCheckQueueSpeed); |
| 94 | +BENCHMARK(CCheckQueueSpeedPrevectorJob); |
0 commit comments