Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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/bench.h>
6 #include <bench/perf.h>
7 
8 #include <assert.h>
9 #include <iostream>
10 #include <iomanip>
11 
13  static std::map<std::string, benchmark::BenchFunction> benchmarks_map;
14  return benchmarks_map;
15 }
16 
18 {
19  benchmarks().insert(std::make_pair(name, func));
20 }
21 
22 void
24 {
25  perf_init();
26  if (std::ratio_less_equal<benchmark::clock::period, std::micro>::value) {
27  std::cerr << "WARNING: Clock precision is worse than microsecond - benchmarks may be less accurate!\n";
28  }
29  std::cout << "#Benchmark" << "," << "count" << "," << "min(ns)" << "," << "max(ns)" << "," << "average(ns)" << ","
30  << "min_cycles" << "," << "max_cycles" << "," << "average_cycles" << "\n";
31 
32  for (const auto &p: benchmarks()) {
33  State state(p.first, elapsedTimeForOne);
34  p.second(state);
35  }
36  perf_fini();
37 }
38 
40 {
41  if (count & countMask) {
42  ++count;
43  return true;
44  }
45  time_point now;
46 
47  uint64_t nowCycles;
48  if (count == 0) {
49  lastTime = beginTime = now = clock::now();
50  lastCycles = beginCycles = nowCycles = perf_cpucycles();
51  }
52  else {
53  now = clock::now();
54  auto elapsed = now - lastTime;
55  auto elapsedOne = elapsed / (countMask + 1);
56  if (elapsedOne < minTime) minTime = elapsedOne;
57  if (elapsedOne > maxTime) maxTime = elapsedOne;
58 
59  // We only use relative values, so don't have to handle 64-bit wrap-around specially
60  nowCycles = perf_cpucycles();
61  uint64_t elapsedOneCycles = (nowCycles - lastCycles) / (countMask + 1);
62  if (elapsedOneCycles < minCycles) minCycles = elapsedOneCycles;
63  if (elapsedOneCycles > maxCycles) maxCycles = elapsedOneCycles;
64 
65  if (elapsed*128 < maxElapsed) {
66  // If the execution was much too fast (1/128th of maxElapsed), increase the count mask by 8x and restart timing.
67  // The restart avoids including the overhead of this code in the measurement.
68  countMask = ((countMask<<3)|7) & ((1LL<<60)-1);
69  count = 0;
70  minTime = duration::max();
71  maxTime = duration::zero();
72  minCycles = std::numeric_limits<uint64_t>::max();
73  maxCycles = std::numeric_limits<uint64_t>::min();
74  return true;
75  }
76  if (elapsed*16 < maxElapsed) {
77  uint64_t newCountMask = ((countMask<<1)|1) & ((1LL<<60)-1);
78  if ((count & newCountMask)==0) {
79  countMask = newCountMask;
80  }
81  }
82  }
83  lastTime = now;
84  lastCycles = nowCycles;
85  ++count;
86 
87  if (now - beginTime < maxElapsed) return true; // Keep going
88 
89  --count;
90 
91  assert(count != 0 && "count == 0 => (now == 0 && beginTime == 0) => return above");
92 
93  // Output results
94  // Duration casts are only necessary here because hardware with sub-nanosecond clocks
95  // will lose precision.
96  int64_t min_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(minTime).count();
97  int64_t max_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(maxTime).count();
98  int64_t avg_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>((now-beginTime)/count).count();
99  int64_t averageCycles = (nowCycles-beginCycles)/count;
100  std::cout << std::fixed << std::setprecision(15) << name << "," << count << "," << min_elapsed << "," << max_elapsed << "," << avg_elapsed << ","
101  << minCycles << "," << maxCycles << "," << averageCycles << "\n";
102  std::cout.copyfmt(std::ios(nullptr));
103 
104  return false;
105 }
BenchRunner(std::string name, BenchFunction func)
Definition: bench.cpp:17
bool KeepRunning()
Definition: bench.cpp:39
void perf_fini(void)
Definition: perf.cpp:50
void perf_init(void)
Definition: perf.cpp:49
const char * name
Definition: rest.cpp:36
clock::duration duration
Definition: bench.h:48
uint64_t perf_cpucycles(void)
Functions for measurement of CPU cycles.
Definition: perf.cpp:51
std::map< std::string, BenchFunction > BenchmarkMap
Definition: bench.h:81
std::function< void(State &)> BenchFunction
Definition: bench.h:77
clock::time_point time_point
Definition: bench.h:47
static BenchmarkMap & benchmarks()
Definition: bench.cpp:12
static int count
Definition: tests.c:45
static void RunAll(duration elapsedTimeForOne=std::chrono::seconds(1))
Definition: bench.cpp:23
Released under the MIT license