Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import time
- import docker
- import psutil
- def run_docker_compose():
- client = docker.from_env()
- # Define command
- build_command = "docker-compose up --build -d"
- # Record the initial CPU times
- initial_cpu_times = psutil.cpu_times()
- # Record the start time
- start_time = time.time()
- try:
- # Run the command
- process = subprocess.run(build_command, shell=True, check=True, capture_output=True)
- except subprocess.CalledProcessError as e:
- # Handle errors in the called executable
- error = str(e)
- else:
- # If the command was successful
- error = None
- # Calculate the build duration
- build_duration = time.time() - start_time
- # Fetch container stats
- containers = client.containers.list()
- # Store the previous CPU stats
- previous_cpu_stats = {}
- for container in containers:
- previous_cpu_stats[container.id] = container.stats(stream=False)["cpu_stats"]
- # Monitor until all containers are idle
- while True:
- active_containers = []
- for container in containers:
- current_stats = container.stats(stream=False)
- previous_total_usage = previous_cpu_stats[container.id]["cpu_usage"]["total_usage"]
- current_total_usage = current_stats["cpu_stats"]["cpu_usage"]["total_usage"]
- # Calculate CPU usage
- cpu_delta = current_total_usage - previous_total_usage
- system_delta = current_stats["cpu_stats"]["system_cpu_usage"] - previous_cpu_stats[container.id]["system_cpu_usage"]
- cpu_percentage = 0.0
- if system_delta > 0.0 and cpu_delta > 0.0:
- cpu_percentage = (cpu_delta / system_delta) * len(current_stats["cpu_stats"]["cpu_usage"]["percpu_usage"]) * 100.0
- # Check if CPU usage is above the threshold
- if cpu_percentage > 1.0:
- active_containers.append(container)
- # Update the stored stats for this container
- previous_cpu_stats[container.id] = current_stats["cpu_stats"]
- if not active_containers:
- break
- time.sleep(1) # wait for a second before rechecking
- # Record the final CPU times
- final_cpu_times = psutil.cpu_times()
- # Calculate the total CPU time used during the operation
- total_cpu_time = (final_cpu_times.user - initial_cpu_times.user) + (final_cpu_times.system - initial_cpu_times.system)
- # Calculate the total memory used and convert it to GB or MB
- total_memory_usage_bytes = sum(container.stats(stream=False)["memory_stats"]["usage"] for container in containers)
- total_memory_usage_gb = total_memory_usage_bytes / (1024 ** 3)
- memory_unit = "GB"
- memory_usage = total_memory_usage_gb
- if total_memory_usage_gb < 1: # If less than 1 GB, convert to MB
- memory_usage = total_memory_usage_bytes / (1024 ** 2)
- memory_unit = "MB"
- # Prepare the result
- result = {
- 'build': {
- 'time': build_duration,
- 'error': error,
- },
- 'total_cpu_time_seconds': total_cpu_time, # This is in seconds
- 'total_memory_usage': f"{memory_usage:.2f} {memory_unit}",
- # ... other stats you might want to include
- }
- return result
- # Example usage
- result = run_docker_compose()
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement