Skip to content

Commit aad4cb5

Browse files
committed
Address ryanofsky feedback on CCheckQueue benchmarks. Eliminated magic numbers, fixed scoping of vectors (and memory movement component of benchmark).
1 parent 9f03110 commit aad4cb5

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/bench/checkqueue.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
// This Benchmark tests the CheckQueue with the lightest
1616
// weight Checks, so it should make any lock contention
1717
// particularly visible
18+
static const int MIN_CORES = 2;
19+
static const size_t BATCHES = 101;
20+
static const size_t BATCH_SIZE = 30;
21+
static const int PREVECTOR_SIZE = 28;
22+
static const int QUEUE_BATCH_SIZE = 128;
1823
static void CCheckQueueSpeed(benchmark::State& state)
1924
{
2025
struct FakeJobNoWork {
@@ -24,21 +29,25 @@ static void CCheckQueueSpeed(benchmark::State& state)
2429
}
2530
void swap(FakeJobNoWork& x){};
2631
};
27-
CCheckQueue<FakeJobNoWork> queue {128};
32+
CCheckQueue<FakeJobNoWork> queue {QUEUE_BATCH_SIZE};
2833
boost::thread_group tg;
29-
for (auto x = 0; x < std::max(2, GetNumCores()); ++x) {
34+
for (auto x = 0; x < std::max(MIN_CORES, GetNumCores()); ++x) {
3035
tg.create_thread([&]{queue.Thread();});
3136
}
3237
while (state.KeepRunning()) {
3338
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);
3839

3940
// We call Add a number of times to simulate the behavior of adding
4041
// a block of transactions at once.
41-
for (size_t j = 0; j < 101; ++j) {
42+
43+
std::vector<std::vector<FakeJobNoWork>> vBatches(BATCHES);
44+
for (auto& vChecks : vBatches) {
45+
vChecks.resize(BATCH_SIZE);
46+
}
47+
for (auto& vChecks : vBatches) {
48+
// We can't make vChecks in the inner loop because we want to measure
49+
// the cost of getting the memory to each thread and we might get the same
50+
// memory
4251
control.Add(vChecks);
4352
}
4453
// control waits for completion by RAII, but
@@ -55,31 +64,31 @@ static void CCheckQueueSpeed(benchmark::State& state)
5564
static void CCheckQueueSpeedPrevectorJob(benchmark::State& state)
5665
{
5766
struct PrevectorJob {
58-
prevector<28, uint8_t> p;
67+
prevector<PREVECTOR_SIZE, uint8_t> p;
5968
PrevectorJob(){
6069
}
6170
PrevectorJob(FastRandomContext& insecure_rand){
62-
p.resize(insecure_rand.rand32() % 56);
71+
p.resize(insecure_rand.rand32() % (PREVECTOR_SIZE*2));
6372
}
6473
bool operator()()
6574
{
6675
return true;
6776
}
6877
void swap(PrevectorJob& x){p.swap(x.p);};
6978
};
70-
CCheckQueue<PrevectorJob> queue {128};
79+
CCheckQueue<PrevectorJob> queue {QUEUE_BATCH_SIZE};
7180
boost::thread_group tg;
72-
for (auto x = 0; x < std::max(2, GetNumCores()); ++x) {
81+
for (auto x = 0; x < std::max(MIN_CORES, GetNumCores()); ++x) {
7382
tg.create_thread([&]{queue.Thread();});
7483
}
7584
while (state.KeepRunning()) {
7685
// Make insecure_rand here so that each iteration is identical.
7786
FastRandomContext insecure_rand(true);
7887
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)
88+
std::vector<std::vector<PrevectorJob>> vBatches(BATCHES);
89+
for (auto& vChecks : vBatches) {
90+
vChecks.reserve(BATCH_SIZE);
91+
for (size_t x = 0; x < BATCH_SIZE; ++x)
8392
vChecks.emplace_back(insecure_rand);
8493
control.Add(vChecks);
8594
}

0 commit comments

Comments
 (0)