-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.hpp
More file actions
154 lines (121 loc) · 4.57 KB
/
Copy pathmetrics.hpp
File metadata and controls
154 lines (121 loc) · 4.57 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
#ifndef METRICS_HPP
#define METRICS_HPP
#include <string>
#include <chrono>
#include <unordered_map>
#include <vector>
#include <mutex>
#include <algorithm>
#include <numeric>
#include <thread>
#include <atomic>
#include <functional>
class MetricsCollector {
public:
static MetricsCollector& getInstance() {
static MetricsCollector instance;
return instance;
}
// Start a timer for an operation
void startTimer(const std::string& operation, const std::string& id) {
std::lock_guard<std::mutex> lock(mtx);
timers[operation + "_" + id] = std::chrono::high_resolution_clock::now();
}
// End a timer and record the duration
void endTimer(const std::string& operation, const std::string& id) {
std::lock_guard<std::mutex> lock(mtx);
auto now = std::chrono::high_resolution_clock::now();
auto it = timers.find(operation + "_" + id);
if (it == timers.end()) return;
auto start = it->second;
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - start).count();
metrics[operation].push_back(duration);
timers.erase(it);
}
// Record a metric directly
void recordMetric(const std::string& name, double value) {
std::lock_guard<std::mutex> lock(mtx);
metrics[name].push_back(value);
}
// Get summary statistics for a metric
struct MetricStats {
double min;
double max;
double avg;
double p95; // 95th percentile
double p99; // 99th percentile
size_t count;
};
MetricStats getStats(const std::string& name) {
std::lock_guard<std::mutex> lock(mtx);
if (metrics.find(name) == metrics.end() || metrics[name].empty()) {
return MetricStats{0, 0, 0, 0, 0, 0};
}
auto& values = metrics[name];
size_t count = values.size();
// Calculate basic statistics
double sum = std::accumulate(values.begin(), values.end(), 0.0);
double avg = sum / count;
// Sort for percentiles
std::vector<double> sorted = values;
std::sort(sorted.begin(), sorted.end());
double min = sorted.front();
double max = sorted.back();
// Calculate percentiles
size_t p95_idx = static_cast<size_t>(count * 0.95);
size_t p99_idx = static_cast<size_t>(count * 0.99);
double p95 = sorted[p95_idx];
double p99 = sorted[p99_idx];
return MetricStats{min, max, avg, p95, p99, count};
}
// Start periodic reporting
void startReporting(int intervalSeconds,
std::function<void(const std::string&)> reportCallback) {
if (reporterRunning) return;
reporterRunning = true;
reporterThread = std::thread([this, intervalSeconds, reportCallback]() {
while (reporterRunning) {
std::this_thread::sleep_for(std::chrono::seconds(intervalSeconds));
std::string report = generateReport();
reportCallback(report);
}
});
}
void stopReporting() {
reporterRunning = false;
if (reporterThread.joinable()) {
reporterThread.join();
}
}
std::string generateReport() {
std::lock_guard<std::mutex> lock(mtx);
std::stringstream ss;
ss << "=== Performance Metrics Report ===\n";
for (const auto& entry : metrics) {
if (entry.second.empty()) continue;
auto stats = getStats(entry.first);
ss << entry.first << " (count: " << stats.count << "):\n"
<< " Min: " << stats.min << " μs\n"
<< " Avg: " << stats.avg << " μs\n"
<< " Max: " << stats.max << " μs\n"
<< " P95: " << stats.p95 << " μs\n"
<< " P99: " << stats.p99 << " μs\n";
}
return ss.str();
}
void clearMetrics() {
std::lock_guard<std::mutex> lock(mtx);
metrics.clear();
}
private:
MetricsCollector() : reporterRunning(false) {}
~MetricsCollector() {
stopReporting();
}
std::unordered_map<std::string, std::chrono::time_point<std::chrono::high_resolution_clock>> timers;
std::unordered_map<std::string, std::vector<double>> metrics;
std::mutex mtx;
std::thread reporterThread;
std::atomic<bool> reporterRunning;
};
#endif // METRICS_HPP