-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_monitor.py
More file actions
155 lines (130 loc) · 5.33 KB
/
performance_monitor.py
File metadata and controls
155 lines (130 loc) · 5.33 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
#!/usr/bin/env python3
"""
Performance monitoring script for the arrest data server
"""
import psutil
import time
import threading
import logging
import os
import tempfile
from datetime import datetime
# Configure logging
TEMP_DIR = tempfile.gettempdir()
PERF_LOG_FILE = os.path.join(TEMP_DIR, 'performance_monitor.log')
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename=PERF_LOG_FILE,
filemode='w'
)
logger = logging.getLogger('performance_monitor')
class PerformanceMonitor:
"""Monitor system performance and resource usage"""
def __init__(self, interval=30):
"""Initialize the performance monitor"""
self.interval = interval # seconds between measurements
self.running = False
self.monitor_thread = None
self.start_time = None
def start(self):
"""Start performance monitoring"""
if self.running:
logger.warning("Performance monitor is already running")
return
self.running = True
self.start_time = time.time()
self.monitor_thread = threading.Thread(target=self._monitor_loop)
self.monitor_thread.daemon = True
self.monitor_thread.start()
logger.info(f"Performance monitor started (interval: {self.interval}s)")
def stop(self):
"""Stop performance monitoring"""
self.running = False
if self.monitor_thread:
self.monitor_thread.join(timeout=5)
logger.info("Performance monitor stopped")
def _monitor_loop(self):
"""Main monitoring loop"""
while self.running:
try:
self._collect_metrics()
time.sleep(self.interval)
except Exception as e:
logger.error(f"Error in performance monitoring: {e}")
time.sleep(10) # Wait before retrying
def _collect_metrics(self):
"""Collect system performance metrics"""
try:
# CPU usage
cpu_percent = psutil.cpu_percent(interval=1)
# Memory usage
memory = psutil.virtual_memory()
memory_percent = memory.percent
memory_mb = memory.used / 1024 / 1024
# Disk usage
disk = psutil.disk_usage('/')
disk_percent = disk.percent
# Network I/O
network = psutil.net_io_counters()
bytes_sent = network.bytes_sent
bytes_recv = network.bytes_recv
# Process-specific metrics (if monitoring a specific process)
process_metrics = self._get_process_metrics()
# Log metrics
logger.info(
f"PERF: CPU={cpu_percent:.1f}% | "
f"MEM={memory_percent:.1f}% ({memory_mb:.0f}MB) | "
f"DISK={disk_percent:.1f}% | "
f"NET_SENT={bytes_sent/1024/1024:.1f}MB | "
f"NET_RECV={bytes_recv/1024/1024:.1f}MB"
)
if process_metrics:
logger.info(f"PROCESS: {process_metrics}")
except Exception as e:
logger.error(f"Error collecting metrics: {e}")
def _get_process_metrics(self):
"""Get metrics for specific processes (server/client)"""
try:
processes = []
for proc in psutil.process_iter(['pid', 'name', 'memory_info', 'cpu_percent']):
try:
if 'python' in proc.info['name'].lower():
# Check if it's our server or client process
cmdline = ' '.join(proc.cmdline())
if 'server' in cmdline.lower() or 'client' in cmdline.lower():
memory_mb = proc.info['memory_info'].rss / 1024 / 1024
cpu_percent = proc.info['cpu_percent']
processes.append({
'pid': proc.info['pid'],
'name': proc.info['name'],
'memory_mb': memory_mb,
'cpu_percent': cpu_percent,
'cmdline': cmdline[:100] + '...' if len(cmdline) > 100 else cmdline
})
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
if processes:
return ' | '.join([
f"{p['name']}({p['pid']}): {p['memory_mb']:.0f}MB, {p['cpu_percent']:.1f}%"
for p in processes
])
except Exception as e:
logger.error(f"Error getting process metrics: {e}")
return None
def main():
"""Main function to run the performance monitor"""
print("Starting performance monitor...")
print(f"Log file: {PERF_LOG_FILE}")
monitor = PerformanceMonitor(interval=30) # Check every 30 seconds
try:
monitor.start()
# Keep running until interrupted
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nStopping performance monitor...")
monitor.stop()
print("Performance monitor stopped")
if __name__ == "__main__":
main()