-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperformancemonitor.cpp
139 lines (126 loc) · 4.28 KB
/
performancemonitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "performancemonitor.h"
#include <algorithm>
#include <numeric>
#include <cassert>
#include <climits>
#include <limits>
using namespace std;
using namespace std::chrono;
PerformanceMonitor::PerformanceMonitor()
{
m_countUsedTimes = 10;
m_currentCommonTime.first = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
m_currentCommonTime.second = m_currentCommonTime.first;
}
size_t PerformanceMonitor::countUsedTimes() const
{
return m_countUsedTimes;
}
void PerformanceMonitor::setCountUsedTimes(const size_t & countUsedTimes)
{
m_countUsedTimes = countUsedTimes;
}
size_t PerformanceMonitor::countTimers() const
{
return m_timers.size();
}
PerformanceMonitor::Timer PerformanceMonitor::timer(size_t index) const
{
return m_timers[index];
}
milliseconds PerformanceMonitor::commonTime() const
{
if (m_commonTimes.empty())
return m_currentCommonTime.second - m_currentCommonTime.first;
return accumulate(m_commonTimes.begin(), m_commonTimes.end(), milliseconds(0)) /
m_commonTimes.size();
}
void PerformanceMonitor::startTimer(const string & name)
{
auto it = m_running_timers.find(name);
if (it == m_running_timers.end())
{
m_running_timers.insert(pair<string, TimerInfo>(
name,
{ vector<milliseconds>(),
duration_cast<milliseconds>(system_clock::now().time_since_epoch()),
m_currentIndex }));
}
else
{
it->second.last_start = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
it->second.currentIndex = m_currentIndex;
}
++m_currentIndex;
}
void PerformanceMonitor::endTimer(const string & name)
{
auto it = m_running_timers.find(name);
if (it != m_running_timers.end())
{
if (it->second.durations.size() > m_countUsedTimes)
{
it->second.durations.erase(it->second.durations.begin(),
it->second.durations.begin() +
((it->second.durations.size() + 1) - m_countUsedTimes));
}
it->second.durations.push_back(duration_cast<milliseconds>(system_clock::now().time_since_epoch()) -
it->second.last_start);
}
else
{
assert(false);
}
}
void PerformanceMonitor::start()
{
m_currentIndex = 0;
m_timers.resize(0);
for (auto it = m_running_timers.begin(); it != m_running_timers.end(); ++it)
{
it->second.currentIndex = numeric_limits<std::size_t>::max();
}
m_currentCommonTime.first = duration_cast<milliseconds>(
system_clock::now().time_since_epoch());
}
void PerformanceMonitor::end()
{
m_currentCommonTime.second = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
if (m_commonTimes.size() >= m_countUsedTimes)
{
m_commonTimes.erase(m_commonTimes.begin(),
m_commonTimes.begin() + ((m_commonTimes.size() + 1) - m_countUsedTimes));
}
m_commonTimes.push_back(m_currentCommonTime.second - m_currentCommonTime.first);
vector<Timer> timers;
vector<pair<size_t, size_t>> indices;
for (auto it = m_running_timers.begin(); it != m_running_timers.end(); )
{
if (it->second.currentIndex == numeric_limits<size_t>::max())
{
it = m_running_timers.erase(it);
}
else
{
indices.push_back({ timers.size(), it->second.currentIndex });
timers.push_back({ it->first, static_cast<size_t>(it->second.getAvgDuration().count()) });
++it;
}
}
sort(indices.begin(), indices.end(), [] (const pair<size_t, size_t> & a,
const pair<size_t, size_t> & b) {
return (a.second < b.second);
});
m_timers.resize(timers.size());
for (size_t i = 0; i < timers.size(); ++i)
{
m_timers[i] = timers[indices[i].first];
}
}
string PerformanceMonitor::report() const
{
string str = "Common time: " + to_string(commonTime().count()) + "\n";
for (const Timer & timer : m_timers)
str += " " + timer.name + " : " + to_string(timer.duration) + "\n";
return str;
}