-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_go.py
More file actions
78 lines (60 loc) · 2.3 KB
/
Copy pathrun_go.py
File metadata and controls
78 lines (60 loc) · 2.3 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
import subprocess
import pandas as pd
import numpy as np
import psutil
import time
# Configuration
iterations = 100
warmup_iterations = 10
# Command to run the Go program
go_command = ["./golang-worker-pool/worker_pool_pattern_go"]
# Metric lists
go_times = []
go_cpu_usages = []
go_mem_usages = []
# Warm-up phase
for i in range(warmup_iterations):
subprocess.run(go_command, check=True)
# Actual measurement phase
for i in range(iterations):
print(f"Running iteration {i + 1}")
# Start the process
process = subprocess.Popen(go_command)
proc = psutil.Process(process.pid)
# Initialize variables for resource tracking
cpu_usage_samples = []
mem_usage_samples = []
start_time = time.time()
# Monitor CPU and memory usage while the process runs
while process.poll() is None: # While the process is running
try:
cpu_usage_samples.append(proc.cpu_percent(interval=0.1)) # CPU % over 0.1 seconds
mem_usage_samples.append(proc.memory_info().rss) # Memory in bytes
except psutil.NoSuchProcess:
break # Exit if process terminates
time.sleep(0.1) # Sampling interval
# Capture total execution time
end_time = time.time()
go_times.append(end_time - start_time)
# Calculate average CPU and memory usage for this iteration
avg_cpu_usage = np.mean(cpu_usage_samples) if cpu_usage_samples else 0
avg_mem_usage = np.mean(mem_usage_samples) if mem_usage_samples else 0
go_cpu_usages.append(avg_cpu_usage)
go_mem_usages.append(avg_mem_usage)
print(f"Iteration {i + 1} - Execution Time: {go_times[-1]} s, CPU Usage: {avg_cpu_usage:.2f}%, Memory Usage: {avg_mem_usage / 1024:.2f} KB")
# Compile results into DataFrames and save to CSV
stats = {
'Execution Time': go_times,
'CPU Usage': go_cpu_usages,
'Memory Usage': go_mem_usages # Ensure column name consistency
}
summary = {
'Metric': ['Execution Time', 'CPU Usage', 'Memory Usage'],
'Average': [np.mean(go_times), np.mean(go_cpu_usages), np.mean(go_mem_usages)],
'StdDev': [np.std(go_times), np.std(go_cpu_usages), np.std(go_mem_usages)]
}
# Save raw data and summary statistics to CSV
df = pd.DataFrame(stats)
df.to_csv('go_results.csv', index=False)
summary_df = pd.DataFrame(summary)
summary_df.to_csv('go_summary.csv', index=False)