Skip to content

Instantly share code, notes, and snippets.

@pkellner
Created October 16, 2024 12:44
Show Gist options
  • Save pkellner/2bfd19bd01c04aea95b58de28eddf43d to your computer and use it in GitHub Desktop.
Save pkellner/2bfd19bd01c04aea95b58de28eddf43d to your computer and use it in GitHub Desktop.
docker-perf.sh
#!/bin/bash
# Capture the stats for all containers
docker stats --no-stream --format "{{.Container}} {{.Name}} {{.CPUPerc}} {{.MemUsage}}" > stats.txt
# Temporary file to store container info for sorting
temp_file=$(mktemp)
# Helper function to format running time
format_time() {
seconds=$1
days=$((seconds / 86400))
hours=$(( (seconds % 86400) / 3600 ))
minutes=$(( (seconds % 3600) / 60 ))
result=""
if [ $days -gt 0 ]; then
result="$days days"
fi
if [ $hours -gt 0 ]; then
if [ -n "$result" ]; then
result="$result, $hours hours"
else
result="$hours hours"
fi
fi
result="$result, $minutes minutes"
echo $result | sed 's/^, //g' # Remove leading comma if present
}
# Loop through containers and gather additional information
while read -r line; do
container_id=$(echo $line | awk '{print $1}')
container_name=$(echo $line | awk '{print $2}')
cpu_usage=$(echo $line | awk '{print $3}')
memory_usage=$(echo $line | awk '{print $4 " " $5}')
# Get container start time (uptime)
start_time=$(docker inspect --format='{{.State.StartedAt}}' $container_id)
# Calculate time running in seconds
running_seconds=$(( $(date +%s) - $(date -d "$start_time" +%s) ))
# Format the time running into a friendly format
time_running=$(format_time $running_seconds)
# Get all the container's IP addresses, separated by semicolons
ip_address=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}};{{end}}' $container_id | sed 's/;$//') # Remove the trailing semicolon
# Store the data in a temporary file for sorting
echo "$container_id $container_name $cpu_usage $memory_usage $time_running $ip_address" >> "$temp_file"
done < stats.txt
# Sort the containers by the time they've been running (descending)
sorted_file=$(mktemp)
sort -k5r "$temp_file" > "$sorted_file"
# Print the header with aligned columns
printf "%-20s %-70s %-10s %-20s %-25s %-20s\n" "Container ID" "Name" "CPU %" "Memory Usage" "Time Running" "IP Address"
# Print the sorted results
while read -r sorted_line; do
container_id=$(echo $sorted_line | awk '{print $1}')
container_name=$(echo $sorted_line | awk '{print $2}')
cpu_usage=$(echo $sorted_line | awk '{print $3}')
memory_usage=$(echo $sorted_line | awk '{print $4 " " $5}')
time_running=$(echo $sorted_line | awk '{print $6 " " $7 " " $8}')
ip_address=$(echo $sorted_line | awk '{print $9}')
printf "%-20s %-70s %-10s %-20s %-25s %-20s\n" "$container_id" "$container_name" "$cpu_usage" "$memory_usage" "$time_running" "$ip_address"
done < "$sorted_file"
# Calculate total CPU and memory usage
total_cpu=$(awk '{gsub("%","",$3); sum+=$3} END {print sum}' stats.txt)
total_mem=$(awk '{split($4, mem, "/"); if(mem[2] ~ /GiB/) sum+=mem[1]*1024; else sum+=mem[1]} END {print sum " MiB"}' stats.txt)
# Print the totals with aligned columns
printf "\n%-50s %-10s\n" "Total CPU Usage:" "$total_cpu %"
printf "%-50s %-10s\n" "Total Memory Usage:" "$total_mem"
# Clean up temporary files
rm "$temp_file" "$sorted_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment