Skip to content

Commit 8607d6b

Browse files
committed
Support very-fast-running benchmarks
- backports bitcoin/bitcoin@7072c54 Avoid calling gettimeofday every time through the benchmarking loop, by keeping track of how long each loop takes and doubling the number of iterations done between time checks when they take less than 1/16'th of the total elapsed time.
1 parent 4aebb60 commit 8607d6b

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

src/Makefile.bench.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ bench_bench_pivx_SOURCES = \
77
bench/bench_pivx.cpp \
88
bench/bench.cpp \
99
bench/bench.h \
10-
bench/MilliSleep.cpp
10+
bench/Examples.cpp
1111

1212
bench_bench_pivx_CPPFLAGS = $(BITCOIN_INCLUDES) $(EVENT_CLFAGS) $(EVENT_PTHREADS_CFLAGS) -I$(builddir)/bench/
1313
bench_bench_pivx_LDADD = \

src/bench/Examples.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 "main.h"
7+
#include "utiltime.h"
8+
9+
// Sanity test: this should loop ten times, and
10+
// min/max/average should be close to 100ms.
11+
static void Sleep100ms(benchmark::State& state)
12+
{
13+
while (state.KeepRunning()) {
14+
MilliSleep(100);
15+
}
16+
}
17+
18+
BENCHMARK(Sleep100ms);
19+
20+
// Extremely fast-running benchmark:
21+
#include <math.h>
22+
23+
volatile double sum = 0.0; // volatile, global so not optimized away
24+
25+
static void Trig(benchmark::State& state)
26+
{
27+
double d = 0.01;
28+
while (state.KeepRunning()) {
29+
sum += sin(d);
30+
d += 0.000001;
31+
}
32+
}
33+
34+
BENCHMARK(Trig);

src/bench/MilliSleep.cpp

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/bench/bench.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,22 @@ BenchRunner::RunAll(double elapsedTimeForOne)
3636

3737
bool State::KeepRunning()
3838
{
39-
double now = gettimedouble();
39+
double now;
4040
if (count == 0) {
41-
beginTime = now;
41+
beginTime = now = gettimedouble();
4242
}
4343
else {
44-
double elapsedOne = now - lastTime;
44+
// timeCheckCount is used to avoid calling gettime most of the time,
45+
// so benchmarks that run very quickly get consistent results.
46+
if ((count+1)%timeCheckCount != 0) {
47+
++count;
48+
return true; // keep going
49+
}
50+
now = gettimedouble();
51+
double elapsedOne = (now - lastTime)/timeCheckCount;
4552
if (elapsedOne < minTime) minTime = elapsedOne;
4653
if (elapsedOne > maxTime) maxTime = elapsedOne;
54+
if (elapsedOne*timeCheckCount < maxElapsed/16) timeCheckCount *= 2;
4755
}
4856
lastTime = now;
4957
++count;
@@ -57,4 +65,4 @@ bool State::KeepRunning()
5765
std::cout << name << "," << count << "," << minTime << "," << maxTime << "," << average << "\n";
5866

5967
return false;
60-
}
68+
}

src/bench/bench.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ namespace benchmark {
3838
double beginTime;
3939
double lastTime, minTime, maxTime;
4040
int64_t count;
41+
int64_t timeCheckCount;
4142
public:
4243
State(std::string _name, double _maxElapsed) : name(_name), maxElapsed(_maxElapsed), count(0) {
4344
minTime = std::numeric_limits<double>::max();
4445
maxTime = std::numeric_limits<double>::min();
46+
timeCheckCount = 1;
4547
}
4648
bool KeepRunning();
4749
};
@@ -63,4 +65,4 @@ namespace benchmark {
6365
#define BENCHMARK(n) \
6466
benchmark::BenchRunner BOOST_PP_CAT(bench_, BOOST_PP_CAT(__LINE__, n))(BOOST_PP_STRINGIZE(n), n);
6567

66-
#endif // BITCOIN_BENCH_H
68+
#endif // BITCOIN_BENCH_H

0 commit comments

Comments
 (0)