This repository was archived by the owner on Sep 29, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathprofiler.cpp
More file actions
182 lines (153 loc) · 5.22 KB
/
Copy pathprofiler.cpp
File metadata and controls
182 lines (153 loc) · 5.22 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "profiler.h"
#include <fstream>
#include <limits>
#include <SFML/System.hpp>
#include "ezolib.h"
class Profile
{
public:
std::string calculateResults(unsigned int depth);
std::string dumpSamples();
Profile(const char* name, Profile* parent = nullptr) : name(name), parent(parent)
{
clock.restart();
}
~Profile()
{
for(unsigned int i = 0; i < childs.size(); i++)
delete childs[i];
}
std::string name;
sf::Clock clock;
std::vector<sf::Time> samples;
std::vector<Profile*> childs;
Profile* const parent;
};
std::string Profile::calculateResults(unsigned int depth)
{
//calculate indents
std::string indent;
for(unsigned int i = 0; i < depth; i++)
indent += '\t';
//calculate min, max, average and total
sf::Time avg, min, max, total;
avg = sf::milliseconds(0);
min = sf::milliseconds(__INT32_MAX__);
max = sf::milliseconds(-(__INT32_MAX__-1));
total = sf::milliseconds(0);
for(unsigned int i = 0; i < samples.size(); i++)
{
total += samples[i];
if(samples[i] < min)
min = samples[i];
if(samples[i] > max)
max = samples[i];
}
avg = total / (sf::Int64)samples.size();
//calculate participation in parent time
sf::Time parentTotal;
for(sf::Time sample : parent->samples)
parentTotal += sample;
double parentParticipation = (1.0 / (parentTotal.asSeconds() / total.asSeconds())) * 100;
//calculate min/max % of average
double minPartOfAverage = (1.0 / (avg.asSeconds() / min.asSeconds())) * 100;
double maxPartOfAverage = (1.0 / (avg.asSeconds() / max.asSeconds())) * 100;
//format stats
std::string result = ezo::string::format("%sName: %s\n"
"%sSamples: %d\n"
"%sParticipation in parent time: %f%%\n"
"%sAverage: %fs (%lldus)\n"
"%sMin: %fs (%lldus) - %f%% of average\n"
"%sMax: %fs (%lldus) - %f%% of average\n"
"%sTotal: %fs (%lldus)\n\n",
indent.c_str(), name.c_str(),
indent.c_str(), samples.size(),
indent.c_str(), parentParticipation,
indent.c_str(), avg.asSeconds(), avg.asMicroseconds(),
indent.c_str(), min.asSeconds(), min.asMicroseconds(), minPartOfAverage,
indent.c_str(), max.asSeconds(), max.asMicroseconds(), maxPartOfAverage,
indent.c_str(), total.asSeconds(), total.asMicroseconds()).c();
//repeat in childrens
depth++;
for(unsigned int i = 0; i < childs.size(); i++)
result += childs[i]->calculateResults(depth);
return result;
}
std::string Profile::dumpSamples()
{
std::string result;
for(unsigned int i = 0; i < samples.size(); i++)
result += ezo::string::format("%d. %fs (%ll us)\n", i+1, samples[i].asSeconds(), samples[i].asMicroseconds());
return result;
}
Profiler::Profiler(const char* filename) :
main(new Profile("main", nullptr)),
current(main)
{
strcpy(this->filename, filename);
}
void Profiler::start(const char* name)
{
//search for existing profile
for(unsigned int i = 0; i < current->childs.size(); i++)
if(current->childs[i]->name == name)
{
current = current->childs[i];
current->clock.restart();
return;
}
//not found; it's new
current->childs.push_back(new Profile(name, current));
current = current->childs.back();
}
void Profiler::stop()
{
sf::Time currTime = current->clock.getElapsedTime();
current->samples.push_back(currTime);
current = current->parent;
}
void Profiler::saveResults()
{
std::string result;
//calculate main time
sf::Time maintime = main->clock.restart();
main->samples.push_back(maintime);
result += ezo::string::format("Main: %fs (%lldus)\n",
maintime.asSeconds(),
maintime.asMicroseconds());
//calculate childrens stats
for(Profile* child : main->childs)
result += child->calculateResults(1);
//save to file
std::ofstream resFile;
resFile.open(filename);
if(!resFile.is_open())
return;
resFile << result;
resFile.close();
}
void Profiler::saveSamples(Profile *profile, bool childsToo)
{
//generate filename
std::string filename = ezo::string::format("%s_dmp", profile->name.c_str()).c();
for(Profile* ancestor = profile->parent; ancestor != nullptr; ancestor = ancestor->parent)
filename += ezo::string::format(".%s", ancestor->name.c_str()).c();
filename += ".txt";
//save samples to file
std::ofstream file;
file.open(filename);
if(!file.is_open())
return;
file << profile->dumpSamples();
file.close();
//repeat in childs if required
if(childsToo)
for(auto child : profile->childs)
saveSamples(child, true);
}
Profiler::~Profiler()
{
saveResults();
//saveSamples(main, true);
delete main;
}