Skip to content

Instantly share code, notes, and snippets.

@doylemark
Created June 27, 2025 18:02
Show Gist options
  • Save doylemark/2cfcd99f3e76ce9baf9ad220acfeca96 to your computer and use it in GitHub Desktop.
Save doylemark/2cfcd99f3e76ce9baf9ad220acfeca96 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import time
import os
import json
from datetime import datetime
def get_memory_usage(pid):
try:
result = subprocess.run(['ps', '-o', 'rss=', '-p', str(pid)],
capture_output=True, text=True, check=True)
memory_kb = int(result.stdout.strip())
return memory_kb / 1024 # Convert to MB
except (subprocess.CalledProcessError, ValueError):
return 0
def run_simulation_with_monitoring(class_name, args=None):
subprocess.run(["./bootstrap.sh", "build"], check=True, capture_output=True)
cmd = [
"java", "-Xmx512m", "-cp", "target/cloudsimplus-examples-*-with-dependencies.jar",
class_name
]
if args:
cmd.extend(args)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pid = process.pid
memory_samples = []
timestamps = []
start_time = time.time()
print(f"Monitoring process {pid}...")
while process.poll() is None:
memory_mb = get_memory_usage(pid)
current_time = time.time() - start_time
memory_samples.append(memory_mb)
timestamps.append(current_time)
time.sleep(0.1) # Sample every 100ms
stdout, stderr = process.communicate()
print(f"Process completed with return code: {process.returncode}")
return timestamps, memory_samples, process.returncode
def main():
os.makedirs('memory_profiling_results', exist_ok=True)
without_cooper_timestamps, without_cooper_memory, without_cooper_rc = run_simulation_with_monitoring(
'org.cloudsimplus.examples.dynamic.MemoryProfilingTest'
)
time.sleep(2)
print("\n2. Running simulation WITH Cooper library...")
with_cooper_timestamps, with_cooper_memory, with_cooper_rc = run_simulation_with_monitoring(
'org.cloudsimplus.examples.dynamic.MemoryProfilingTest', ['cooper']
)
# Prepare data
without_cooper_data = {
'timestamps': without_cooper_timestamps,
'memory': without_cooper_memory,
'return_code': without_cooper_rc
}
with_cooper_data = {
'timestamps': with_cooper_timestamps,
'memory': with_cooper_memory,
'return_code': with_cooper_rc
}
# Save raw data as JSON
data_filename = 'memory_profiling_results/quick_memory_data.json'
with open(data_filename, 'w') as f:
json.dump({
'without_cooper': without_cooper_data,
'with_cooper': with_cooper_data,
'timestamp': datetime.now().isoformat()
}, f, indent=2)
print(f"Raw data saved to: {data_filename}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment