Line data Source code
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 : #include "bench.h"
5 : #include <iostream>
6 : #include <sys/time.h>
7 :
8 : using namespace benchmark;
9 :
10 0 : std::map<std::string, BenchFunction> BenchRunner::benchmarks;
11 :
12 0 : static double gettimedouble(void) {
13 : struct timeval tv;
14 0 : gettimeofday(&tv, NULL);
15 0 : return tv.tv_usec * 0.000001 + tv.tv_sec;
16 : }
17 :
18 0 : BenchRunner::BenchRunner(std::string name, BenchFunction func)
19 : {
20 0 : benchmarks.insert(std::make_pair(name, func));
21 0 : }
22 :
23 : void
24 0 : BenchRunner::RunAll(double elapsedTimeForOne)
25 : {
26 0 : std::cout << "Benchmark" << "," << "count" << "," << "min" << "," << "max" << "," << "average" << "\n";
27 :
28 0 : for (std::map<std::string,BenchFunction>::iterator it = benchmarks.begin();
29 0 : it != benchmarks.end(); ++it) {
30 :
31 0 : State state(it->first, elapsedTimeForOne);
32 0 : BenchFunction& func = it->second;
33 0 : func(state);
34 : }
35 0 : }
36 :
37 0 : bool State::KeepRunning()
38 : {
39 : double now;
40 0 : if (count == 0) {
41 0 : beginTime = now = gettimedouble();
42 : }
43 : else {
44 : // timeCheckCount is used to avoid calling gettime most of the time,
45 : // so benchmarks that run very quickly get consistent results.
46 0 : if ((count+1)%timeCheckCount != 0) {
47 0 : ++count;
48 0 : return true; // keep going
49 : }
50 0 : now = gettimedouble();
51 0 : double elapsedOne = (now - lastTime)/timeCheckCount;
52 0 : if (elapsedOne < minTime) minTime = elapsedOne;
53 0 : if (elapsedOne > maxTime) maxTime = elapsedOne;
54 0 : if (elapsedOne*timeCheckCount < maxElapsed/16) timeCheckCount *= 2;
55 : }
56 0 : lastTime = now;
57 0 : ++count;
58 :
59 0 : if (now - beginTime < maxElapsed) return true; // Keep going
60 :
61 0 : --count;
62 :
63 : // Output results
64 0 : double average = (now-beginTime)/count;
65 0 : std::cout << name << "," << count << "," << minTime << "," << maxTime << "," << average << "\n";
66 :
67 0 : return false;
68 0 : }
|