Symphony Of Empires
profiler.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // profiler.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <unordered_map>
28 #include <string>
29 #include <ctime>
30 #include <chrono>
31 #include <vector>
32 #include <queue>
33 #include <atomic>
34 #include <mutex>
35 #include <list>
36 
37 namespace Eng3D {
38  class BenchmarkTask {
39  void clear_old();
40  public:
41  BenchmarkTask(const std::string& task, uint32_t argb)
42  : name{ task },
43  color{ argb }
44  {
45  running = false;
46  }
47  BenchmarkTask& operator=(const BenchmarkTask& lhs) = delete;
48  void start();
49  void stop();
50  float get_average_time_ms();
51  float get_largest_time_ms();
52 
53  const std::string name;
54  const uint32_t color;
55  private:
56  std::list<float> times;
57  std::list<std::chrono::system_clock::time_point> start_times;
58  std::chrono::system_clock::time_point start_time;
59  bool running;
60  };
61 
62  struct Profiler {
64  render_started = false;
65  }
66  ~Profiler() = default;
67  void start(const std::string& name);
68  void stop(const std::string& name);
69  void tick_done();
70  void render_done();
71  float get_fps();
72  const std::vector<BenchmarkTask*> get_tasks();
73  private:
74  std::mutex lock;
75  size_t tick;
76  float fps;
77  std::chrono::system_clock::time_point fps_clock;
78  float fps_timer = 0;
79  size_t frames = 0;
80  std::atomic<bool> render_started;
81  std::unordered_map<std::string, BenchmarkTask> tasks;
82  };
83 }
BenchmarkTask & operator=(const BenchmarkTask &lhs)=delete
float get_largest_time_ms()
Definition: profiler.cpp:60
float get_average_time_ms()
Definition: profiler.cpp:52
BenchmarkTask(const std::string &task, uint32_t argb)
Definition: profiler.hpp:41
const uint32_t color
Definition: profiler.hpp:54
const std::string name
Definition: profiler.hpp:53
void tick_done()
Definition: profiler.cpp:104
void stop(const std::string &name)
Definition: profiler.cpp:95
~Profiler()=default
float get_fps()
Definition: profiler.cpp:108
void render_done()
Definition: profiler.cpp:112
void start(const std::string &name)
Definition: profiler.cpp:84
const std::vector< BenchmarkTask * > get_tasks()
Definition: profiler.cpp:128