Advertisement
Guest User

Docker Python Basic Benchmark Script

a guest
Oct 24th, 2023
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | Source Code | 0 0
  1. import subprocess
  2. import time
  3. import docker
  4. import psutil
  5.  
  6. def run_docker_compose():
  7.     client = docker.from_env()
  8.  
  9.     # Define command
  10.     build_command = "docker-compose up --build -d"
  11.  
  12.     # Record the initial CPU times
  13.     initial_cpu_times = psutil.cpu_times()
  14.  
  15.     # Record the start time
  16.     start_time = time.time()
  17.  
  18.     try:
  19.         # Run the command
  20.         process = subprocess.run(build_command, shell=True, check=True, capture_output=True)
  21.     except subprocess.CalledProcessError as e:
  22.         # Handle errors in the called executable
  23.         error = str(e)
  24.     else:
  25.         # If the command was successful
  26.         error = None
  27.  
  28.     # Calculate the build duration
  29.     build_duration = time.time() - start_time
  30.  
  31.     # Fetch container stats
  32.     containers = client.containers.list()
  33.    
  34.     # Store the previous CPU stats
  35.     previous_cpu_stats = {}
  36.     for container in containers:
  37.         previous_cpu_stats[container.id] = container.stats(stream=False)["cpu_stats"]
  38.  
  39.     # Monitor until all containers are idle
  40.     while True:
  41.         active_containers = []
  42.         for container in containers:
  43.             current_stats = container.stats(stream=False)
  44.             previous_total_usage = previous_cpu_stats[container.id]["cpu_usage"]["total_usage"]
  45.             current_total_usage = current_stats["cpu_stats"]["cpu_usage"]["total_usage"]
  46.  
  47.             # Calculate CPU usage
  48.             cpu_delta = current_total_usage - previous_total_usage
  49.             system_delta = current_stats["cpu_stats"]["system_cpu_usage"] - previous_cpu_stats[container.id]["system_cpu_usage"]
  50.  
  51.             cpu_percentage = 0.0
  52.             if system_delta > 0.0 and cpu_delta > 0.0:
  53.                 cpu_percentage = (cpu_delta / system_delta) * len(current_stats["cpu_stats"]["cpu_usage"]["percpu_usage"]) * 100.0
  54.  
  55.             # Check if CPU usage is above the threshold
  56.             if cpu_percentage > 1.0:
  57.                 active_containers.append(container)
  58.  
  59.             # Update the stored stats for this container
  60.             previous_cpu_stats[container.id] = current_stats["cpu_stats"]
  61.  
  62.         if not active_containers:
  63.             break
  64.         time.sleep(1)  # wait for a second before rechecking
  65.  
  66.     # Record the final CPU times
  67.     final_cpu_times = psutil.cpu_times()
  68.  
  69.     # Calculate the total CPU time used during the operation
  70.     total_cpu_time = (final_cpu_times.user - initial_cpu_times.user) + (final_cpu_times.system - initial_cpu_times.system)
  71.  
  72.     # Calculate the total memory used and convert it to GB or MB
  73.     total_memory_usage_bytes = sum(container.stats(stream=False)["memory_stats"]["usage"] for container in containers)
  74.     total_memory_usage_gb = total_memory_usage_bytes / (1024 ** 3)
  75.     memory_unit = "GB"
  76.     memory_usage = total_memory_usage_gb
  77.     if total_memory_usage_gb < 1:  # If less than 1 GB, convert to MB
  78.         memory_usage = total_memory_usage_bytes / (1024 ** 2)
  79.         memory_unit = "MB"
  80.  
  81.     # Prepare the result
  82.     result = {
  83.         'build': {
  84.             'time': build_duration,
  85.             'error': error,
  86.         },
  87.         'total_cpu_time_seconds': total_cpu_time,  # This is in seconds
  88.         'total_memory_usage': f"{memory_usage:.2f} {memory_unit}",
  89.         # ... other stats you might want to include
  90.     }
  91.  
  92.     return result
  93.  
  94. # Example usage
  95. result = run_docker_compose()
  96. print(result)
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement