Ember
Loading...
Searching...
No Matches
PerformanceMonitor.h
Go to the documentation of this file.
1#pragma once
2
3#include "Interfaces/ITab.h"
4#include "Types/Types.h"
5#include <chrono>
6#include <deque>
7#include <map>
8#include <memory>
9#include <string>
10#include <vector>
11#include <wx/wx.h>
12
13namespace EmberForge {
14
18struct MetricData {
19 wxString name;
20 std::deque<float> values;
21 wxColour color;
22 float min_value;
23 float max_value;
25 wxString unit;
27
28 MetricData(const wxString &n, const wxColour &c, const wxString &u = "", size_t max_s = 120);
29 void AddValue(float value);
30 float GetAverage() const;
31 void Clear();
32};
33
38 public:
41
42 // Frame timing
43 void BeginFrame();
44 void EndFrame();
45
46 // Custom metrics
47 void RecordMetric(const std::string &name, float value, const std::string &unit = "");
48 void RecordTime(const std::string &name, double milliseconds);
49
50 // System metrics
52 float GetCPUUsage() const { return m_cpuUsage; }
53 float GetMemoryUsage() const { return m_memoryUsageMB; }
54 float GetCurrentFPS() const { return m_currentFPS; }
55
56 // Metric access
57 std::shared_ptr<MetricData> GetMetric(const std::string &name) const;
58 std::vector<std::shared_ptr<MetricData>> GetAllMetrics() const;
59
60 // Control
61 void SetEnabled(bool enabled) { m_enabled = enabled; }
62 bool IsEnabled() const { return m_enabled; }
63 void Clear();
64
65 // Singleton access
67
68 private:
70 void UpdateFPS();
71 float ReadCPUUsage();
72 float ReadMemoryUsage();
73
74 // Built-in metrics
75 std::shared_ptr<MetricData> m_frameTimeMetric;
76 std::shared_ptr<MetricData> m_fpsMetric;
77 std::shared_ptr<MetricData> m_cpuMetric;
78 std::shared_ptr<MetricData> m_memoryMetric;
79
80 // Custom metrics
81 std::map<std::string, std::shared_ptr<MetricData>> m_customMetrics;
82
83 // Frame timing
84 std::chrono::high_resolution_clock::time_point m_frameStart;
85 std::chrono::high_resolution_clock::time_point m_lastFrameTime;
86 std::deque<float> m_frameTimes;
87
88 // System metrics
92
93 // State
96
97// Platform-specific data for CPU monitoring
98#ifdef __linux__
99 unsigned long long m_lastCpuTotal;
100 unsigned long long m_lastCpuIdle;
101#endif
102};
103
107class PerformancePanel : public wxPanel, public ITab {
108 public:
109 explicit PerformancePanel(wxWindow *parent, PerformanceMonitor *monitor = nullptr);
110 virtual ~PerformancePanel();
111
112 // ITab interface implementation
113 wxWindow *GetWidget() override { return this; }
114 wxString GetTitle() const override { return "Performance"; }
115 wxString GetTabType() const override { return "Performance"; }
116
117 // Monitor management
118 void SetMonitor(PerformanceMonitor *monitor);
120
121 // Display control
122 void RefreshDisplay();
123 void SetUpdateInterval(int milliseconds);
124
125 // Chart toggle control
126 void SetChartEnabled(const std::string &chartName, bool enabled);
127 bool IsChartEnabled(const std::string &chartName) const;
128 void SetAllChartsEnabled(bool enabled);
129 std::vector<std::string> GetAvailableCharts() const;
130
131 private:
132 // UI Creation
133 void CreateLayout();
134 void CreateToolbar();
136 void CreateCharts();
137
138 // Event handlers
139 void OnUpdateTimer(wxTimerEvent &event);
140 void OnPaint(wxPaintEvent &event);
141 void OnSize(wxSizeEvent &event);
142 void OnToggleMonitoring(wxCommandEvent &event);
143 void OnClearMetrics(wxCommandEvent &event);
144 void OnExportMetrics(wxCommandEvent &event);
145 void OnToggleChart(wxCommandEvent &event);
146 void OnHistoryMinus(wxCommandEvent &event);
147 void OnHistoryPlus(wxCommandEvent &event);
148
149 // Drawing methods
150 void DrawChart(wxDC &dc, const wxRect &rect, std::shared_ptr<MetricData> metric);
151 void DrawMetricInfo(wxDC &dc, const wxRect &rect, std::shared_ptr<MetricData> metric);
152 void DrawGrid(wxDC &dc, const wxRect &rect, float minVal, float maxVal);
153
154 // Helper methods
156 wxRect GetChartRect(int index, int totalCharts) const;
157 wxString FormatValue(float value, const wxString &unit) const;
158
159 // UI Components
160 wxButton *m_toggleButton;
161 wxButton *m_clearButton;
162 wxButton *m_exportButton;
163 wxStaticText *m_statusText;
166
167 // Chart toggle checkboxes
169 wxCheckBox *m_fpsCheckbox;
170 wxCheckBox *m_cpuCheckbox;
171 wxCheckBox *m_memoryCheckbox;
172
173 // History control
175 wxStaticText *m_historyText;
177
178 // State
183 std::map<std::string, bool> m_chartEnabled;
184
185 // Display settings
190
191 // Event IDs
192 enum {
193 ID_UPDATE_TIMER = wxID_HIGHEST + 100,
203 };
204
206};
207
208// Convenience macros for performance tracking
209#define PERF_BEGIN_FRAME() EmberForge::PerformanceMonitor::GetInstance().BeginFrame()
210#define PERF_END_FRAME() EmberForge::PerformanceMonitor::GetInstance().EndFrame()
211#define PERF_RECORD(name, value, unit) EmberForge::PerformanceMonitor::GetInstance().RecordMetric(name, value, unit)
212#define PERF_TIME_BEGIN(name) auto _perf_start_##name = std::chrono::high_resolution_clock::now()
213#define PERF_TIME_END(name) \
214 EmberForge::PerformanceMonitor::GetInstance().RecordTime( \
215 #name, \
216 std::chrono::duration<double, std::milli>(std::chrono::high_resolution_clock::now() - _perf_start_##name) \
217 .count())
218
219} // namespace EmberForge
System performance monitoring class.
std::shared_ptr< MetricData > m_cpuMetric
std::chrono::high_resolution_clock::time_point m_lastFrameTime
std::shared_ptr< MetricData > GetMetric(const std::string &name) const
void RecordTime(const std::string &name, double milliseconds)
std::shared_ptr< MetricData > m_fpsMetric
std::shared_ptr< MetricData > m_frameTimeMetric
std::shared_ptr< MetricData > m_memoryMetric
void RecordMetric(const std::string &name, float value, const std::string &unit="")
static PerformanceMonitor & GetInstance()
std::chrono::high_resolution_clock::time_point m_frameStart
std::vector< std::shared_ptr< MetricData > > GetAllMetrics() const
std::map< std::string, std::shared_ptr< MetricData > > m_customMetrics
void DrawChart(wxDC &dc, const wxRect &rect, std::shared_ptr< MetricData > metric)
wxString FormatValue(float value, const wxString &unit) const
void SetChartEnabled(const std::string &chartName, bool enabled)
PerformanceMonitor * GetMonitor() const
void DrawGrid(wxDC &dc, const wxRect &rect, float minVal, float maxVal)
void OnPaint(wxPaintEvent &event)
void OnToggleChart(wxCommandEvent &event)
wxString GetTabType() const override
Returns the tab type identifier.
wxString GetTitle() const override
Returns the display title of the tab.
void OnHistoryPlus(wxCommandEvent &event)
std::map< std::string, bool > m_chartEnabled
void OnHistoryMinus(wxCommandEvent &event)
wxRect GetChartRect(int index, int totalCharts) const
void DrawMetricInfo(wxDC &dc, const wxRect &rect, std::shared_ptr< MetricData > metric)
void SetMonitor(PerformanceMonitor *monitor)
void SetUpdateInterval(int milliseconds)
PerformancePanel(wxWindow *parent, PerformanceMonitor *monitor=nullptr)
void OnUpdateTimer(wxTimerEvent &event)
bool IsChartEnabled(const std::string &chartName) const
void OnClearMetrics(wxCommandEvent &event)
void OnSize(wxSizeEvent &event)
void OnExportMetrics(wxCommandEvent &event)
std::vector< std::string > GetAvailableCharts() const
void OnToggleMonitoring(wxCommandEvent &event)
wxWindow * GetWidget() override
Returns the wxWidgets window used as the tab content.
Interface for tab-based UI components in the application.
Definition ITab.h:7
MetricData(const wxString &n, const wxColour &c, const wxString &u="", size_t max_s=120)
std::deque< float > values