Skip to content

Commit a2521e7

Browse files
committed
added memory monitor
1 parent 76d4b89 commit a2521e7

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

test/partitioner/memory_monitor.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import subprocess
2+
import psutil
3+
import time
4+
import matplotlib.pyplot as plt
5+
6+
def monitor(command):
7+
# Start the C++ process
8+
proc = subprocess.Popen(command, shell=True)
9+
mem_usage = []
10+
mem_usage2 = []
11+
timestamps = []
12+
start_time = time.time()
13+
14+
while proc.poll() is None: # While process is running
15+
try:
16+
# Get RSS memory in MB
17+
mem_info = psutil.Process(proc.pid).memory_full_info()
18+
print(mem_info)
19+
mem = mem_info.uss / (1024 * 1024)
20+
mem_usage.append(mem)
21+
22+
rss = mem_info.rss / (1024 * 1024)
23+
mem_usage2.append(rss)
24+
# rss = mem_info.rss / (1024 * 1024) # Physical RAM
25+
# vms = mem_info.vms / (1024 * 1024) # Virtual Memory
26+
timestamps.append(time.time() - start_time)
27+
except psutil.NoSuchProcess:
28+
break
29+
time.sleep(0.05) # Sample every 50ms
30+
31+
return timestamps, mem_usage, mem_usage2
32+
33+
# Run and Plot
34+
inputs = [
35+
"/sb-graph/test/partitioner/data/air_conditioners_cont_4_1000.json",
36+
"/sb-graph/test/partitioner/data/air_conditioners_cont_4_10000.json",
37+
"/sb-graph/test/partitioner/data/air_conditioners_cont_4_100000.json"
38+
]
39+
40+
colors = {
41+
inputs[0]: 'red',
42+
inputs[1]: 'blue',
43+
inputs[2]: 'green'
44+
}
45+
46+
colors2 = {
47+
inputs[0]: 'brown',
48+
inputs[1]: 'skyblue',
49+
inputs[2]: 'black'
50+
}
51+
52+
53+
# Generate the plot
54+
plt.figure(figsize=(10, 6))
55+
plt.title("Memory Consumption Timeline")
56+
plt.xlabel("Time (seconds)")
57+
plt.ylabel("Memory Usage (MB)")
58+
plt.grid(True)
59+
60+
for f in inputs:
61+
for _ in range(1):
62+
times, mem, mem2 = monitor(f"/sb-graph/install/bin/sbg-partitioner -f {f} -p 4")
63+
64+
plt.plot(times, mem, color=colors[f])
65+
plt.plot(times, mem2, color=colors2[f])
66+
plt.legend()
67+
68+
# Save the plot instead of showing it
69+
output_file = "memory_benchmark.png"
70+
plt.savefig(output_file)
71+
print(f"Benchmark finished. Results saved to {output_file}")

0 commit comments

Comments
 (0)