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