-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance.py
More file actions
593 lines (479 loc) · 20.5 KB
/
performance.py
File metadata and controls
593 lines (479 loc) · 20.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
"""
Performance monitoring module for tracking system performance and optimization.
Provides real-time performance metrics, profiling, and resource usage monitoring.
"""
import time
import psutil
import threading
from typing import Dict, List, Optional, Callable
from dataclasses import dataclass, field
from collections import deque
import json
import numpy as np
@dataclass
class PerformanceMetrics:
"""Data class for performance metrics"""
fps: float = 0.0
avg_processing_time: float = 0.0
cpu_usage: float = 0.0
memory_usage: float = 0.0
gpu_usage: float = 0.0
detection_accuracy: float = 0.0
frame_drops: int = 0
timestamp: float = field(default_factory=time.time)
@dataclass
class SystemSpecs:
"""System specifications for performance analysis"""
cpu_count: int
cpu_freq: float
total_memory: float
gpu_available: bool
gpu_memory: float
opencv_version: str
mediapipe_version: str
class PerformanceProfiler:
"""
Performance profiler for measuring execution times of different components.
"""
def __init__(self):
"""Initialize performance profiler"""
self.profiles = {}
self.active_profiles = {}
def start_profile(self, name: str):
"""Start profiling a component"""
self.active_profiles[name] = time.perf_counter()
def end_profile(self, name: str) -> float:
"""
End profiling and return execution time.
Args:
name: Profile name
Returns:
Execution time in seconds
"""
if name not in self.active_profiles:
return 0.0
start_time = self.active_profiles.pop(name)
execution_time = time.perf_counter() - start_time
# Store in profile history
if name not in self.profiles:
self.profiles[name] = deque(maxlen=1000) # Keep last 1000 measurements
self.profiles[name].append(execution_time)
return execution_time
def get_profile_stats(self, name: str) -> Dict[str, float]:
"""Get statistical analysis of profile data"""
if name not in self.profiles or not self.profiles[name]:
return {}
times = list(self.profiles[name])
return {
'mean': np.mean(times),
'median': np.median(times),
'std': np.std(times),
'min': np.min(times),
'max': np.max(times),
'p95': np.percentile(times, 95),
'p99': np.percentile(times, 99),
'count': len(times)
}
def get_all_profiles(self) -> Dict[str, Dict[str, float]]:
"""Get stats for all profiles"""
return {name: self.get_profile_stats(name) for name in self.profiles.keys()}
class ResourceMonitor:
"""
Monitor system resources including CPU, memory, and GPU usage.
"""
def __init__(self, update_interval: float = 1.0):
"""
Initialize resource monitor.
Args:
update_interval: Update interval in seconds
"""
self.update_interval = update_interval
self.running = False
self.monitor_thread = None
# Resource usage history
self.cpu_history = deque(maxlen=60) # Last 60 measurements
self.memory_history = deque(maxlen=60)
self.gpu_history = deque(maxlen=60)
# Current values
self.current_cpu = 0.0
self.current_memory = 0.0
self.current_gpu = 0.0
# Callbacks for alerts
self.alert_callbacks = []
# Alert thresholds
self.cpu_threshold = 80.0 # %
self.memory_threshold = 80.0 # %
self.gpu_threshold = 80.0 # %
def start_monitoring(self):
"""Start resource monitoring in background thread"""
if self.running:
return
self.running = True
self.monitor_thread = threading.Thread(target=self._monitor_loop, daemon=True)
self.monitor_thread.start()
def stop_monitoring(self):
"""Stop resource monitoring"""
self.running = False
if self.monitor_thread:
self.monitor_thread.join()
def _monitor_loop(self):
"""Main monitoring loop"""
while self.running:
try:
# Get CPU usage
cpu_percent = psutil.cpu_percent(interval=None)
self.current_cpu = cpu_percent
self.cpu_history.append(cpu_percent)
# Get memory usage
memory = psutil.virtual_memory()
memory_percent = memory.percent
self.current_memory = memory_percent
self.memory_history.append(memory_percent)
# Get GPU usage (if available)
gpu_percent = self._get_gpu_usage()
self.current_gpu = gpu_percent
self.gpu_history.append(gpu_percent)
# Check for alerts
self._check_alerts()
time.sleep(self.update_interval)
except Exception as e:
print(f"Error in resource monitoring: {e}")
time.sleep(self.update_interval)
def _get_gpu_usage(self) -> float:
"""Get GPU usage percentage"""
try:
import pynvml
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0) # First GPU
utilization = pynvml.nvmlDeviceGetUtilizationRates(handle)
return float(utilization.gpu)
except:
return 0.0 # GPU monitoring not available
def _check_alerts(self):
"""Check if resource usage exceeds thresholds"""
alerts = []
if self.current_cpu > self.cpu_threshold:
alerts.append(f"High CPU usage: {self.current_cpu:.1f}%")
if self.current_memory > self.memory_threshold:
alerts.append(f"High memory usage: {self.current_memory:.1f}%")
if self.current_gpu > self.gpu_threshold:
alerts.append(f"High GPU usage: {self.current_gpu:.1f}%")
# Trigger callbacks for alerts
for alert in alerts:
for callback in self.alert_callbacks:
try:
callback(alert)
except:
pass # Don't let callback errors stop monitoring
def add_alert_callback(self, callback: Callable[[str], None]):
"""Add callback for resource alerts"""
self.alert_callbacks.append(callback)
def get_current_usage(self) -> Dict[str, float]:
"""Get current resource usage"""
return {
'cpu': self.current_cpu,
'memory': self.current_memory,
'gpu': self.current_gpu
}
def get_usage_history(self) -> Dict[str, List[float]]:
"""Get resource usage history"""
return {
'cpu': list(self.cpu_history),
'memory': list(self.memory_history),
'gpu': list(self.gpu_history)
}
def get_usage_stats(self) -> Dict[str, Dict[str, float]]:
"""Get statistical analysis of resource usage"""
stats = {}
for name, history in [('cpu', self.cpu_history),
('memory', self.memory_history),
('gpu', self.gpu_history)]:
if history:
values = list(history)
stats[name] = {
'current': values[-1],
'mean': np.mean(values),
'max': np.max(values),
'min': np.min(values),
'std': np.std(values)
}
else:
stats[name] = {'current': 0.0, 'mean': 0.0, 'max': 0.0, 'min': 0.0, 'std': 0.0}
return stats
class PerformanceOptimizer:
"""
Automatic performance optimizer that adjusts settings based on system performance.
"""
def __init__(self, resource_monitor: ResourceMonitor):
"""
Initialize performance optimizer.
Args:
resource_monitor: Resource monitor instance
"""
self.resource_monitor = resource_monitor
self.optimization_callbacks = []
# Optimization parameters
self.target_fps = 30
self.min_fps = 15
self.cpu_limit = 70.0
self.memory_limit = 70.0
# Current optimization level (0 = no optimization, 3 = maximum)
self.optimization_level = 0
# Optimization history
self.optimization_history = []
def add_optimization_callback(self, callback: Callable[[Dict[str, any]], None]):
"""Add callback for optimization suggestions"""
self.optimization_callbacks.append(callback)
def analyze_performance(self, current_fps: float, processing_time: float) -> Dict[str, any]:
"""
Analyze current performance and suggest optimizations.
Args:
current_fps: Current frames per second
processing_time: Current processing time per frame
Returns:
Optimization suggestions
"""
suggestions = {
'level': self.optimization_level,
'actions': [],
'reasoning': [],
'estimated_improvement': 0.0
}
# Get current resource usage
usage = self.resource_monitor.get_current_usage()
# Analyze FPS performance
if current_fps < self.min_fps:
suggestions['actions'].append('reduce_resolution')
suggestions['reasoning'].append(f'FPS too low: {current_fps:.1f} < {self.min_fps}')
suggestions['estimated_improvement'] += 5.0
# Analyze CPU usage
if usage['cpu'] > self.cpu_limit:
suggestions['actions'].append('increase_frame_skip')
suggestions['reasoning'].append(f'High CPU usage: {usage["cpu"]:.1f}%')
suggestions['estimated_improvement'] += 3.0
# Analyze memory usage
if usage['memory'] > self.memory_limit:
suggestions['actions'].append('reduce_buffer_size')
suggestions['reasoning'].append(f'High memory usage: {usage["memory"]:.1f}%')
suggestions['estimated_improvement'] += 2.0
# Analyze processing time
target_processing_time = 1.0 / self.target_fps
if processing_time > target_processing_time * 1.5:
suggestions['actions'].append('optimize_algorithms')
suggestions['reasoning'].append(f'Slow processing: {processing_time*1000:.1f}ms')
suggestions['estimated_improvement'] += 4.0
# Determine optimization level
if suggestions['actions']:
self.optimization_level = min(3, self.optimization_level + 1)
else:
self.optimization_level = max(0, self.optimization_level - 1)
suggestions['level'] = self.optimization_level
# Store optimization history
self.optimization_history.append({
'timestamp': time.time(),
'fps': current_fps,
'cpu_usage': usage['cpu'],
'memory_usage': usage['memory'],
'optimization_level': self.optimization_level,
'suggestions': suggestions['actions']
})
# Trigger callbacks
for callback in self.optimization_callbacks:
try:
callback(suggestions)
except:
pass
return suggestions
def get_optimization_config(self) -> Dict[str, any]:
"""Get current optimization configuration"""
base_config = {
'frame_skip': 1,
'resolution_scale': 1.0,
'buffer_size': 10,
'use_gpu': True,
'quality_preset': 'high'
}
# Apply optimizations based on level
if self.optimization_level >= 1:
base_config['frame_skip'] = 2
base_config['quality_preset'] = 'medium'
if self.optimization_level >= 2:
base_config['resolution_scale'] = 0.75
base_config['buffer_size'] = 5
if self.optimization_level >= 3:
base_config['frame_skip'] = 3
base_config['resolution_scale'] = 0.5
base_config['buffer_size'] = 3
base_config['quality_preset'] = 'low'
return base_config
class PerformanceMonitor:
"""
Main performance monitoring system coordinating all performance components.
"""
def __init__(self):
"""Initialize performance monitor"""
# Initialize components
self.profiler = PerformanceProfiler()
self.resource_monitor = ResourceMonitor()
self.optimizer = PerformanceOptimizer(self.resource_monitor)
# Performance metrics history
self.metrics_history = deque(maxlen=300) # 5 minutes at 1 FPS
# Frame timing
self.frame_times = deque(maxlen=100)
self.last_frame_time = time.time()
# Detection accuracy tracking
self.accuracy_samples = deque(maxlen=100)
# System specifications
self.system_specs = self._get_system_specs()
# Start monitoring
self.resource_monitor.start_monitoring()
def _get_system_specs(self) -> SystemSpecs:
"""Get system specifications"""
try:
import cv2
opencv_version = cv2.__version__
except:
opencv_version = "unknown"
try:
import mediapipe
mediapipe_version = mediapipe.__version__
except:
mediapipe_version = "unknown"
# Check GPU availability
gpu_available = False
gpu_memory = 0.0
try:
import pynvml
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
memory_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
gpu_available = True
gpu_memory = memory_info.total / (1024**3) # Convert to GB
except:
pass
return SystemSpecs(
cpu_count=psutil.cpu_count(),
cpu_freq=psutil.cpu_freq().max if psutil.cpu_freq() else 0.0,
total_memory=psutil.virtual_memory().total / (1024**3), # GB
gpu_available=gpu_available,
gpu_memory=gpu_memory,
opencv_version=opencv_version,
mediapipe_version=mediapipe_version
)
def update_frame_time(self, processing_time: float):
"""Update frame processing time"""
current_time = time.time()
frame_interval = current_time - self.last_frame_time
self.frame_times.append(frame_interval)
self.last_frame_time = current_time
# Store processing time in profiler
self.profiler.profiles.setdefault('frame_processing', deque(maxlen=1000))
self.profiler.profiles['frame_processing'].append(processing_time)
def update_accuracy(self, accuracy: float):
"""Update detection accuracy"""
self.accuracy_samples.append(accuracy)
def get_current_fps(self) -> float:
"""Calculate current FPS"""
if len(self.frame_times) < 2:
return 0.0
recent_times = list(self.frame_times)[-10:] # Last 10 frames
if not recent_times:
return 0.0
avg_interval = np.mean(recent_times)
return 1.0 / avg_interval if avg_interval > 0 else 0.0
def get_stats(self) -> PerformanceMetrics:
"""Get current performance statistics"""
# Calculate FPS
current_fps = self.get_current_fps()
# Get average processing time
processing_times = self.profiler.profiles.get('frame_processing', [])
avg_processing_time = np.mean(processing_times) if processing_times else 0.0
# Get resource usage
usage = self.resource_monitor.get_current_usage()
# Get detection accuracy
accuracy = np.mean(self.accuracy_samples) if self.accuracy_samples else 0.0
# Count frame drops (FPS significantly below target)
target_fps = 30
frame_drops = max(0, int((target_fps - current_fps) * len(self.frame_times) / 100))
metrics = PerformanceMetrics(
fps=current_fps,
avg_processing_time=avg_processing_time,
cpu_usage=usage['cpu'],
memory_usage=usage['memory'],
gpu_usage=usage['gpu'],
detection_accuracy=accuracy,
frame_drops=frame_drops
)
# Store in history
self.metrics_history.append(metrics)
return metrics
def get_performance_report(self) -> Dict[str, any]:
"""Generate comprehensive performance report"""
current_stats = self.get_stats()
report = {
'timestamp': time.time(),
'current_metrics': {
'fps': current_stats.fps,
'processing_time_ms': current_stats.avg_processing_time * 1000,
'cpu_usage': current_stats.cpu_usage,
'memory_usage': current_stats.memory_usage,
'gpu_usage': current_stats.gpu_usage,
'accuracy': current_stats.detection_accuracy
},
'system_specs': {
'cpu_count': self.system_specs.cpu_count,
'cpu_freq_ghz': self.system_specs.cpu_freq / 1000,
'total_memory_gb': self.system_specs.total_memory,
'gpu_available': self.system_specs.gpu_available,
'gpu_memory_gb': self.system_specs.gpu_memory,
'opencv_version': self.system_specs.opencv_version,
'mediapipe_version': self.system_specs.mediapipe_version
},
'performance_history': self._get_history_stats(),
'profiling_data': self.profiler.get_all_profiles(),
'resource_stats': self.resource_monitor.get_usage_stats(),
'optimization_suggestions': self.optimizer.analyze_performance(
current_stats.fps, current_stats.avg_processing_time
)
}
return report
def _get_history_stats(self) -> Dict[str, any]:
"""Get statistical analysis of performance history"""
if not self.metrics_history:
return {}
fps_values = [m.fps for m in self.metrics_history]
cpu_values = [m.cpu_usage for m in self.metrics_history]
memory_values = [m.memory_usage for m in self.metrics_history]
return {
'fps': {
'mean': np.mean(fps_values),
'min': np.min(fps_values),
'max': np.max(fps_values),
'std': np.std(fps_values)
},
'cpu': {
'mean': np.mean(cpu_values),
'min': np.min(cpu_values),
'max': np.max(cpu_values),
'std': np.std(cpu_values)
},
'memory': {
'mean': np.mean(memory_values),
'min': np.min(memory_values),
'max': np.max(memory_values),
'std': np.std(memory_values)
}
}
def save_performance_log(self, filepath: str) -> bool:
"""Save performance data to file"""
try:
report = self.get_performance_report()
with open(filepath, 'w') as f:
json.dump(report, f, indent=2, default=str)
return True
except Exception as e:
print(f"Error saving performance log: {e}")
return False
def cleanup(self):
"""Cleanup performance monitor"""
self.resource_monitor.stop_monitoring()