Skip to content

Instantly share code, notes, and snippets.

@quantumxt
Created June 18, 2024 09:53
Show Gist options
  • Select an option

  • Save quantumxt/c7cfc37a00dcfde0ae97a1f955f0b4c8 to your computer and use it in GitHub Desktop.

Select an option

Save quantumxt/c7cfc37a00dcfde0ae97a1f955f0b4c8 to your computer and use it in GitHub Desktop.
Benchmark CPU
#!/bin/bash
# Set the monitoring duration in seconds
DURATION=300
# Set the interval between samples in seconds
INTERVAL=1
# Get the current date and time
CURRENT_DATE=$(date +%Y-%m-%d)
CURRENT_TIME=$(date +%H-%M-%S)
# Construct the filename
FILENAME="CPU_BENCH_${CURRENT_DATE}_${CURRENT_TIME}.csv"
# Get the start time
START_TIME=$(date +%s)
# Calculate the end time
END_TIME=$((START_TIME + DURATION))
# Create a temporary file to store the CPU usage data
TEMP_FILE=$(mktemp)
T_MIN=$(echo "$DURATION/60" | bc)
echo "Monitoring CPU for $T_MIN min..."
# Monitor CPU usage and store data in the temporary file
while [ $(date +%s) -lt $END_TIME ]; do
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
ONE_MINUTE_LOAD=$(uptime | awk -F'load average:' '{ print $2 }' | awk '{ print $1 }' | tr -d ',')
echo "$(date +%T),$CPU_USAGE,$ONE_MINUTE_LOAD" >> $TEMP_FILE
sleep $INTERVAL
done
echo "Done! Saving data..."
# Print the header for the output
echo "Time,CPU Usage (%),1-Minute Load Average" > "${FILENAME}"
# Print the CPU usage data from the temporary file
cat $TEMP_FILE >> "${FILENAME}"
# Remove the temporary file
rm $TEMP_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment