NAIA  1.1.1
Benchmark.h
Go to the documentation of this file.
1 #ifndef BENCHMARK_H
2 #define BENCHMARK_H
3 
4 #include <chrono>
5 #include <iostream>
6 #include <string>
7 #include <unordered_map>
8 
9 #include "fmt/chrono.h"
10 #include "spdlog/spdlog.h"
11 
12 using t_clock = std::chrono::high_resolution_clock;
13 
14 namespace NAIA {
15 class Stopwatch;
16 
17 class Benchmark {
18  friend class Stopwatch;
19 
20 private:
21  struct Measurement {
22  std::chrono::duration<double, std::milli> time;
23  unsigned int nCalls = 0;
24  };
25 
26 public:
27  inline Stopwatch MakeStopwatch(std::string name);
28 
29  void Print(std::shared_ptr<spdlog::logger> logger) {
30 
31  logger->debug("Timers results:");
32  for (const auto &measurement : m_timers) {
33  logger->debug("{0: >30}: {1}", measurement.first, measurement.second.time);
34  if (measurement.second.nCalls > 0) {
35  logger->debug("{0: >30} {1} per call", "", measurement.second.time / measurement.second.nCalls);
36  }
37  }
38  };
39 
40 private:
41  void UpdateTimer(std::string timerName, t_clock::duration time) {
42  // Usually we wouldn't do this, but this is actually what we want:
43  // operator[] inserts if the key is not in the map, then we add the duration
44  auto &measurement = m_timers[timerName];
45  measurement.time += time;
46  measurement.nCalls++;
47  };
48 
49  // std::unordered_map<std::string, Measurement> m_timers;
50  std::map<std::string, Measurement> m_timers;
51 };
52 
53 class Stopwatch {
54 public:
55  Stopwatch(std::string name, Benchmark *bench) : m_name(std::move(name)), m_bench(bench), m_start(t_clock::now()){};
56  ~Stopwatch() { m_bench->UpdateTimer(m_name, t_clock::duration(t_clock::now() - m_start)); };
57 
58 private:
59  std::string m_name;
60 
62 
63  t_clock::time_point m_start;
64 };
65 
66 Stopwatch Benchmark::MakeStopwatch(std::string name) { return Stopwatch(std::move(name), this); };
67 } // namespace NAIA
68 #endif
t_clock
std::chrono::high_resolution_clock t_clock
Definition: Benchmark.h:12
NAIA::Stopwatch::~Stopwatch
~Stopwatch()
Definition: Benchmark.h:56
NAIA::Benchmark::Stopwatch
friend class Stopwatch
Definition: Benchmark.h:18
NAIA::Benchmark::Measurement::time
std::chrono::duration< double, std::milli > time
Definition: Benchmark.h:22
NAIA::Stopwatch::m_name
std::string m_name
Definition: Benchmark.h:56
NAIA
Definition: Event.h:13
NAIA::Stopwatch::Stopwatch
Stopwatch(std::string name, Benchmark *bench)
Definition: Benchmark.h:55
NAIA::Benchmark::Print
void Print(std::shared_ptr< spdlog::logger > logger)
Definition: Benchmark.h:29
NAIA::Benchmark::Measurement::nCalls
unsigned int nCalls
Definition: Benchmark.h:23
NAIA::Stopwatch
Definition: Benchmark.h:53
NAIA::Benchmark::m_timers
std::map< std::string, Measurement > m_timers
Definition: Benchmark.h:47
NAIA::Benchmark::UpdateTimer
void UpdateTimer(std::string timerName, t_clock::duration time)
Definition: Benchmark.h:41
NAIA::Benchmark::MakeStopwatch
Stopwatch MakeStopwatch(std::string name)
Definition: Benchmark.h:66
NAIA::Stopwatch::m_bench
Benchmark * m_bench
Definition: Benchmark.h:61
NAIA::Benchmark::Measurement
Definition: Benchmark.h:21
NAIA::Stopwatch::m_start
t_clock::time_point m_start
Definition: Benchmark.h:63
NAIA::Benchmark
Definition: Benchmark.h:17