-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_monitor.cpp
More file actions
349 lines (294 loc) · 12.5 KB
/
performance_monitor.cpp
File metadata and controls
349 lines (294 loc) · 12.5 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "performance_monitor.h"
// ===== CONSTRUCTOR & DESTRUCTOR =====
PerformanceMonitor::PerformanceMonitor()
: startTime(0), lastUpdateTime(0), lastSpeedUpdateTime(0),
totalBytes(0), lastByteCount(0), currentSpeedKBps(0),
averageSpeedKBps(0), historyIndex(0), isActive(false),
connectionStartTime(0), firstByteTime(0), transferStartTime(0),
firstByteReceived(false) {
// Initialize speed history array
for (int i = 0; i < PERFORMANCE_HISTORY_SIZE; i++) {
speedHistory[i] = 0.0;
}
}
PerformanceMonitor::~PerformanceMonitor() {
stopMonitoring();
}
// ===== MONITORING CONTROL =====
void PerformanceMonitor::startMonitoring() {
resetMonitoring();
startTime = millis();
lastUpdateTime = startTime;
lastSpeedUpdateTime = startTime;
isActive = true;
Serial.println("=== Performance Monitoring Started ===");
}
void PerformanceMonitor::stopMonitoring() {
if (isActive) {
isActive = false;
Serial.println("=== Performance Monitoring Stopped ===");
}
}
void PerformanceMonitor::resetMonitoring() {
startTime = 0;
lastUpdateTime = 0;
lastSpeedUpdateTime = 0;
totalBytes = 0;
lastByteCount = 0;
currentSpeedKBps = 0;
averageSpeedKBps = 0;
historyIndex = 0;
// Reset enhanced timing
connectionStartTime = 0;
firstByteTime = 0;
transferStartTime = 0;
firstByteReceived = false;
detailedTiming = DetailedTiming();
for (int i = 0; i < PERFORMANCE_HISTORY_SIZE; i++) {
speedHistory[i] = 0.0;
}
}
// ===== ENHANCED TIMING METHODS =====
void PerformanceMonitor::startConnectionTimer() {
connectionStartTime = millis();
firstByteReceived = false;
}
void PerformanceMonitor::markFirstByte() {
if (!firstByteReceived) {
firstByteTime = millis();
transferStartTime = millis();
firstByteReceived = true;
detailedTiming.connectionSetupMs = firstByteTime - connectionStartTime;
detailedTiming.firstByteMs = firstByteTime - connectionStartTime;
}
}
void PerformanceMonitor::stopEnhancedMonitoring() {
unsigned long endTime = millis();
detailedTiming.totalTimeMs = endTime - connectionStartTime;
if (firstByteReceived) {
detailedTiming.transferOnlyMs = endTime - transferStartTime;
}
}
// ===== PROGRESS TRACKING =====
void PerformanceMonitor::updateProgress(size_t bytesTransferred) {
if (!isActive) return;
totalBytes = bytesTransferred;
unsigned long currentTime = millis();
// Update speed if enough time has passed
if (currentTime - lastSpeedUpdateTime >= SPEED_UPDATE_INTERVAL_MS) {
calculateCurrentSpeed(bytesTransferred);
lastSpeedUpdateTime = currentTime;
}
// Update progress display
if (currentTime - lastUpdateTime >= PROGRESS_UPDATE_INTERVAL_MS) {
printProgress();
lastUpdateTime = currentTime;
}
}
void PerformanceMonitor::updateProgress(size_t current, size_t total) {
updateProgress(current);
if (total > 0 && isActive) {
float percentage = (current * 100.0) / total;
Serial.printf("Progress: %.1f%% (%s/%s) at %.2f KB/s\n",
percentage, formatBytes(current).c_str(),
formatBytes(total).c_str(), currentSpeedKBps);
}
}
void PerformanceMonitor::printProgress() const {
if (!isActive) return;
Serial.printf("Downloaded: %s | Speed: %.2f KB/s | Time: %s\n",
formatBytes(totalBytes).c_str(),
currentSpeedKBps,
formatTime(getElapsedTime()).c_str());
}
void PerformanceMonitor::printProgress(size_t current, size_t total) const {
if (total > 0) {
float percentage = (current * 100.0) / total;
Serial.printf("Progress: %d/%d bytes (%.1f%%) at %.2f KB/s\n",
current, total, percentage, currentSpeedKBps);
} else {
Serial.printf("Downloaded: %d bytes at %.2f KB/s\n", current, currentSpeedKBps);
}
}
// ===== SPEED CALCULATION =====
void PerformanceMonitor::calculateCurrentSpeed(size_t newBytes) {
if (!isActive || startTime == 0) return;
unsigned long currentTime = millis();
unsigned long timeDiff = currentTime - lastSpeedUpdateTime;
if (timeDiff > 0) {
size_t bytesDiff = newBytes - lastByteCount;
currentSpeedKBps = calculateSpeedKBps(bytesDiff, timeDiff);
// Update speed history
updateSpeedHistory();
// Calculate average speed
unsigned long totalTime = currentTime - startTime;
if (totalTime > 0) {
averageSpeedKBps = calculateSpeedKBps(newBytes, totalTime);
}
lastByteCount = newBytes;
}
}
void PerformanceMonitor::updateSpeedHistory() {
speedHistory[historyIndex] = currentSpeedKBps;
historyIndex = (historyIndex + 1) % PERFORMANCE_HISTORY_SIZE;
}
float PerformanceMonitor::getPeakSpeed() const {
float peak = 0.0;
for (int i = 0; i < PERFORMANCE_HISTORY_SIZE; i++) {
if (speedHistory[i] > peak) {
peak = speedHistory[i];
}
}
return peak;
}
// ===== TIMING METHODS =====
unsigned long PerformanceMonitor::getElapsedTime() const {
if (!isActive || startTime == 0) return 0;
return millis() - startTime;
}
float PerformanceMonitor::getElapsedTimeSeconds() const {
return getElapsedTime() / 1000.0;
}
// ===== RESULTS AND REPORTING =====
void PerformanceMonitor::printDetailedResults(size_t totalBytesTransferred) const {
Serial.println("\n=== DETAILED PERFORMANCE RESULTS ===");
Serial.println("Total bytes: " + formatBytes(totalBytesTransferred));
Serial.println("Total time: " + formatTime(getElapsedTime()));
Serial.printf("Average speed: %.2f KB/s (%.2f MB/s)\n", averageSpeedKBps, averageSpeedKBps / 1024.0);
Serial.printf("Peak speed: %.2f KB/s (%.2f MB/s)\n", getPeakSpeed(), getPeakSpeed() / 1024.0);
Serial.printf("Current speed: %.2f KB/s\n", currentSpeedKBps);
Serial.println("Target achieved: " + String(hasAchievedTarget() ? "YES" : "NO"));
Serial.println("Performance rating: " + getPerformanceRating(averageSpeedKBps));
Serial.println("====================================");
}
void PerformanceMonitor::printEnhancedResults(size_t totalBytesTransferred) const {
Serial.println("\n=== ENHANCED PERFORMANCE ANALYSIS ===");
Serial.println("File size: " + formatBytes(totalBytesTransferred));
Serial.println("Connection setup: " + String(detailedTiming.connectionSetupMs) + " ms");
Serial.println("Transfer time: " + String(detailedTiming.transferOnlyMs) + " ms");
Serial.println("Total time: " + String(detailedTiming.totalTimeMs) + " ms");
Serial.println("");
float pureSpeed = detailedTiming.getPureTransferSpeedKBps(totalBytesTransferred);
float overallSpeed = detailedTiming.getOverallSpeedKBps(totalBytesTransferred);
float efficiency = detailedTiming.getEfficiencyPercent();
Serial.println("Pure transfer speed: " + String(pureSpeed, 2) + " KB/s");
Serial.println("Overall speed: " + String(overallSpeed, 2) + " KB/s");
Serial.println("Transfer efficiency: " + String(efficiency, 1) + "%");
// File size analysis
if (totalBytesTransferred < 10240) { // < 10KB
Serial.println("Analysis: Small file - Speed dominated by connection overhead");
Serial.println("Recommendation: Use files >100KB for accurate speed testing");
} else if (totalBytesTransferred < 102400) { // < 100KB
Serial.println("Analysis: Medium file - Some overhead impact on speed");
Serial.println("Recommendation: Use files >1MB for best speed accuracy");
} else {
Serial.println("Analysis: Large file - Speed measurement is accurate");
Serial.println("Recommendation: This speed represents true performance");
}
// Performance rating based on pure transfer speed
String pureSpeedRating = getPerformanceRating(pureSpeed);
Serial.println("Pure speed rating: " + pureSpeedRating);
Serial.println("Target achieved: " + String(pureSpeed >= TARGET_SPEED_KBPS ? "YES" : "NO"));
Serial.println("=======================================");
}
void PerformanceMonitor::printSpeedSummary() const {
Serial.println("=== SPEED SUMMARY ===");
printSpeedInAllUnits(averageSpeedKBps);
Serial.println("=====================");
}
void PerformanceMonitor::printPerformanceAnalysis() const {
Serial.println("=== PERFORMANCE ANALYSIS ===");
Serial.println(getSpeedAnalysisReport());
Serial.println("=============================");
}
bool PerformanceMonitor::hasAchievedTarget() const {
return averageSpeedKBps >= TARGET_SPEED_KBPS;
}
// ===== UTILITY METHODS =====
void PerformanceMonitor::printSpeedInAllUnits(float speedKBps) const {
float speedBps = speedKBps * 1024.0;
float speedMBps = speedKBps / 1024.0;
float speedMbps = speedKBps * 8.0 / 1024.0; // Megabits per second
Serial.printf("Speed: %.0f B/s | %.2f KB/s | %.3f MB/s | %.2f Mbps\n",
speedBps, speedKBps, speedMBps, speedMbps);
}
String PerformanceMonitor::getPerformanceRating(float speedKBps) const {
if (speedKBps >= 1000.0) return "EXCELLENT";
else if (speedKBps >= 500.0) return "VERY GOOD";
else if (speedKBps >= 400.0) return "GOOD";
else if (speedKBps >= 200.0) return "AVERAGE";
else if (speedKBps >= 100.0) return "BELOW AVERAGE";
else return "POOR";
}
String PerformanceMonitor::getSpeedAnalysisReport() const {
String report = "Average: " + formatSpeed(averageSpeedKBps);
report += " | Peak: " + formatSpeed(getPeakSpeed());
report += " | Rating: " + getPerformanceRating(averageSpeedKBps);
report += " | Target: " + String(hasAchievedTarget() ? "ACHIEVED" : "NOT ACHIEVED");
return report;
}
// ===== STATIC UTILITY METHODS =====
float PerformanceMonitor::convertBytesToKB(size_t bytes) {
return (float)bytes / 1024.0;
}
float PerformanceMonitor::convertBytesToMB(size_t bytes) {
return (float)bytes / (1024.0 * 1024.0);
}
float PerformanceMonitor::calculateSpeedKBps(size_t bytes, unsigned long timeMs) {
if (timeMs == 0) return 0.0;
return (convertBytesToKB(bytes) * 1000.0) / (float)timeMs;
}
String PerformanceMonitor::formatSpeed(float speedKBps) {
if (speedKBps >= 1024.0) {
return String(speedKBps / 1024.0, 2) + " MB/s";
} else {
return String(speedKBps, 2) + " KB/s";
}
}
String PerformanceMonitor::formatTime(unsigned long timeMs) {
if (timeMs < 1000) {
return String(timeMs) + "ms";
} else if (timeMs < 60000) {
return String(timeMs / 1000.0, 1) + "s";
} else {
int minutes = timeMs / 60000;
int seconds = (timeMs % 60000) / 1000;
return String(minutes) + "m " + String(seconds) + "s";
}
}
String PerformanceMonitor::formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + " B";
} else if (bytes < 1024 * 1024) {
return String(bytes / 1024.0, 1) + " KB";
} else if (bytes < 1024 * 1024 * 1024) {
return String(bytes / (1024.0 * 1024.0), 1) + " MB";
} else {
return String(bytes / (1024.0 * 1024.0 * 1024.0), 2) + " GB";
}
}
// ===== C-STYLE FUNCTIONS =====
PerformanceResults analyzePerformance(size_t bytes, unsigned long timeMs) {
PerformanceResults results;
results.success = true;
results.totalBytes = bytes;
results.totalTimeMs = timeMs;
results.averageSpeedKBps = PerformanceMonitor::calculateSpeedKBps(bytes, timeMs);
results.peakSpeedKBps = results.averageSpeedKBps; // Simplified for static analysis
results.targetAchieved = results.averageSpeedKBps >= TARGET_SPEED_KBPS;
PerformanceMonitor monitor;
results.performanceRating = monitor.getPerformanceRating(results.averageSpeedKBps);
results.analysisReport = "Speed: " + PerformanceMonitor::formatSpeed(results.averageSpeedKBps) +
" | Rating: " + results.performanceRating +
" | Target: " + String(results.targetAchieved ? "ACHIEVED" : "NOT ACHIEVED");
return results;
}
void printPerformanceReport(const PerformanceResults& results) {
Serial.println("\n=== PERFORMANCE REPORT ===");
Serial.println("Bytes: " + PerformanceMonitor::formatBytes(results.totalBytes));
Serial.println("Time: " + PerformanceMonitor::formatTime(results.totalTimeMs));
Serial.println("Speed: " + PerformanceMonitor::formatSpeed(results.averageSpeedKBps));
Serial.println("Rating: " + results.performanceRating);
Serial.println("Target: " + String(results.targetAchieved ? "ACHIEVED" : "NOT ACHIEVED"));
Serial.println("==========================");
}