Beatmup
Beatmup::Profiler Class Reference

Collects running time statistics of multiple tracks. More...

#include <profiler.h>

Classes

class  Track
 

Public Types

enum class  ReportType { BRIEF , FULL }
 

Public Member Functions

 Profiler ()
 
void reset ()
 
void operator() (const std::string &track)
 
void lap ()
 
void report (std::ostream &, ReportType type=ReportType::FULL) const
 
time_t getTotal () const
 

Private Types

typedef uint64_t time_t
 

Private Attributes

std::map< std::string, Tracktracks
 
std::vector< std::pair< std::string, std::chrono::system_clock::time_point > > running
 
time_t total
 

Detailed Description

Collects running time statistics of multiple tracks.

Definition at line 31 of file profiler.h.

Member Typedef Documentation

◆ time_t

typedef uint64_t Beatmup::Profiler::time_t
private

Definition at line 33 of file profiler.h.

Member Enumeration Documentation

◆ ReportType

Enumerator
BRIEF 
FULL 

Definition at line 46 of file profiler.h.

46  {
47  BRIEF,
48  FULL
49  };

Constructor & Destructor Documentation

◆ Profiler()

Profiler::Profiler ( )

Definition at line 26 of file profiler.cpp.

26 : total(0) {}

Member Function Documentation

◆ reset()

void Beatmup::Profiler::reset ( )

Definition at line 28 of file profiler.cpp.

28  {
29  tracks.clear();
30  total = 0;
31 }
std::map< std::string, Track > tracks
Definition: profiler.h:41

◆ operator()()

void Profiler::operator() ( const std::string &  track)

Definition at line 33 of file profiler.cpp.

33  {
34  running.emplace_back(track, std::chrono::system_clock::now());
35 }
std::vector< std::pair< std::string, std::chrono::system_clock::time_point > > running
Definition: profiler.h:42

◆ lap()

void Profiler::lap ( )

Definition at line 37 of file profiler.cpp.

37  {
38  BEATMUP_ASSERT_DEBUG(!running.empty());
39  std::chrono::system_clock::time_point endTime(std::chrono::system_clock::now());
40  Track& track = tracks[running.back().first];
41  time_t sample = (time_t)std::chrono::duration<float, std::micro>(endTime - running.back().second).count();
42  running.pop_back();
43  if (track.n == 0) {
44  track.min = track.max = track.sum = sample;
45  track.n = 1;
46  }
47  else {
48  track.n++;
49  track.sum += sample;
50  track.min = std::min(track.min, sample);
51  track.max = std::max(track.max, sample);
52  }
53  total += sample;
54 }
uint64_t time_t
Definition: profiler.h:33
#define BEATMUP_ASSERT_DEBUG(C)
Definition: exception.h:163
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
JNIEnv jlong jint jint count

◆ report()

void Profiler::report ( std::ostream &  stream,
ReportType  type = ReportType::FULL 
) const

Definition at line 56 of file profiler.cpp.

56  {
57  if (tracks.empty())
58  return;
59 
60  typedef std::pair<std::string, time_t> Entry;
61  std::vector<Entry> idx;
62  idx.reserve(tracks.size());
63  size_t maxlen = 0;
64  for (const auto& _ : tracks) {
65  idx.emplace_back(_.first, _.second.n == 0 ? 0 : (_.second.sum / _.second.n));
66  maxlen = std::max(_.first.size(), maxlen);
67  }
68  std::sort(idx.begin(), idx.end(), [&](Entry& _1, Entry& _2) { return _1.second < _2.second; });
69  stream << "=== " << total << " us" << std::endl;
70  if (type == ReportType::FULL) {
71  stream
72  << std::setw(maxlen) << "<id>"
73  << "\t" << "<avg>"
74  << "\t" << "<min>"
75  << "\t" << "<max>"
76  << "\t" << "<n>"
77  << std::endl;
78  for (auto& _ : idx) {
79  const Track& track = tracks.find(_.first)->second;
80  stream
81  << std::setw(maxlen) << _.first
82  << "\t" << (track.n == 0 ? 0 : (track.sum / track.n))
83  << "\t" << track.min
84  << "\t" << track.max
85  << "\t" << track.n
86  << std::endl;
87  }
88  }
89  else
90  for (auto& _ : idx) {
91  const Track& track = tracks.find(_.first)->second;
92  stream
93  << std::setw(maxlen) << _.first
94  << "\t" << (track.n == 0 ? 0 : (track.sum / track.n))
95  << std::endl;
96  }
97 }

◆ getTotal()

time_t Beatmup::Profiler::getTotal ( ) const
inline

Definition at line 57 of file profiler.h.

57 { return total; }

Member Data Documentation

◆ tracks

std::map<std::string, Track> Beatmup::Profiler::tracks
private

Definition at line 41 of file profiler.h.

◆ running

std::vector<std::pair<std::string, std::chrono::system_clock::time_point> > Beatmup::Profiler::running
private

Definition at line 42 of file profiler.h.

◆ total

time_t Beatmup::Profiler::total
private

Definition at line 43 of file profiler.h.


The documentation for this class was generated from the following files: