-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
45 lines (37 loc) · 1.54 KB
/
run.py
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
import time
import subprocess
import os
import psutil
import pyshark
# Define local interface
interface = 'any'
# Generate output file names with timestamp
timestamp = time.strftime("%Y%m%d-%H%M%S")
output_pcap_file = "output_server.pcap" if not os.path.exists("output.pcap") else "output_{}.pcap".format(timestamp)
output_stats_file = "performance_IoT_stats.txt" if not os.path.exists("performance_stats.txt") else "performance_stats_{}.txt".format(timestamp)
# Start pcap capture on local interface
capture = pyshark.LiveCapture(interface=interface)
capture.sniff(timeout=5)
# Run external script "load-testv3.py"
process = subprocess.Popen(["python", "load-testv3.py"])
# Monitor performance of "load-testv3.py"
cpu_usage = []
memory_usage = []
start_time = time.time()
while process.poll() is None:
# Record CPU usage
cpu_usage.append(psutil.cpu_percent())
# Record memory usage
memory_usage.append(psutil.Process(process.pid).memory_info().rss)
time.sleep(0.1)
# Calculate average performance metrics
elapsed_time = time.time() - start_time
average_cpu_usage = sum(cpu_usage) / len(cpu_usage)
average_memory_usage = sum(memory_usage) / len(memory_usage)
# Write performance metrics to output file
with open(output_stats_file, 'w') as f:
f.write("Average CPU usage: {}%\n".format(average_cpu_usage))
f.write("Average memory usage: {} bytes\n".format(average_memory_usage))
f.write("Average running time: {} seconds\n".format(elapsed_time))
# Save pcap capture to file not working at the moment
#capture.save_to_file(output_pcap_file)