26 #include <glm/glm.hpp>
27 #include "eng3d/profiler.hpp"
28 #include "eng3d/log.hpp"
30 using namespace Eng3D;
33 static const float MS_TO_KEEP_DATA = 10 * 1e3;
36 start_time = std::chrono::system_clock::now();
42 Eng3D::Log::error(
"benchmark",
"Tried to stop task '" + name +
"', but it hasn't been started yet");
46 auto now = std::chrono::system_clock::now();
47 float time = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count();
48 times.push_back(time);
49 start_times.push_back(start_time);
55 for(
auto const& time : times)
57 return total_time / (MS_TO_KEEP_DATA / 1e3);
62 if(times.empty())
return 0.f;
63 return *std::max_element(times.cbegin(), times.cend());
66 void Eng3D::BenchmarkTask::clear_old() {
68 auto now = std::chrono::system_clock::now();
69 auto times_it = times.begin();
70 auto start_times_it = start_times.begin();
71 while(start_times_it != start_times.end()) {
72 auto prev_start_time = *start_times_it;
73 float time = std::chrono::duration_cast<std::chrono::milliseconds>(now - prev_start_time).count();
74 if(time > MS_TO_KEEP_DATA) {
75 start_times_it = start_times.erase(start_times_it);
76 times_it = times.erase(times_it);
85 auto it = tasks.find(name);
86 if(it == tasks.end()) {
89 tasks.insert({ name, task });
96 auto it = tasks.find(name);
97 if(it == tasks.end()) {
98 Eng3D::Log::error(
"profiler",
"Tried to stop task '" + name +
"', but it hasn't been started yet");
113 auto now = std::chrono::system_clock::now();
114 if(!render_started) {
116 render_started =
true;
118 fps_timer += std::chrono::duration_cast<std::chrono::milliseconds>(now - fps_clock).count();
121 if(fps_timer > 1000.0) {
122 fps = (float)frames * 0.5 + fps * 0.5;
129 std::vector<Eng3D::BenchmarkTask*> list(tasks.size());
130 std::transform(tasks.begin(), tasks.end(), list.begin(), [](
auto& e) {
float get_largest_time_ms()
float get_average_time_ms()
void error(const std::string_view category, const std::string_view msg)
void stop(const std::string &name)
void start(const std::string &name)
const std::vector< BenchmarkTask * > get_tasks()