Beatmup
profiler.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "profiler.h"
20 #include "../exception.h"
21 #include <algorithm>
22 #include <iomanip>
23 
24 using namespace Beatmup;
25 
26 Profiler::Profiler() : total(0) {}
27 
29  tracks.clear();
30  total = 0;
31 }
32 
33 void Profiler::operator()(const std::string& track) {
34  running.emplace_back(track, std::chrono::system_clock::now());
35 }
36 
37 void Profiler::lap() {
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 }
55 
56 void Profiler::report(std::ostream& stream, ReportType type) const {
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 }
std::map< std::string, Track > tracks
Definition: profiler.h:41
void operator()(const std::string &track)
Definition: profiler.cpp:33
uint64_t time_t
Definition: profiler.h:33
void report(std::ostream &, ReportType type=ReportType::FULL) const
Definition: profiler.cpp:56
std::vector< std::pair< std::string, std::chrono::system_clock::time_point > > running
Definition: profiler.h:42
#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