Skip to content

Instantly share code, notes, and snippets.

@wesleyegberto
Last active January 25, 2021 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wesleyegberto/c8d139b3457ed048791df178353b1fa8 to your computer and use it in GitHub Desktop.
Save wesleyegberto/c8d139b3457ed048791df178353b1fa8 to your computer and use it in GitHub Desktop.
Script to start a server and run a monitored stress test using Apache Bench
# Script to run stress test agains a URL using Apache Bench
# Usage: sh monitored_ab_stress.sh "$CMD" <URL-stress> <output-results-filename>
# where CMD="java -jar my-app.jar"
# script origin at https://github.com/jkremser/micronaut-app-k8s/blob/master/plot-test.sh
#!/bin/bash
echo "=== Monitored Apache Bench Test - Started"
echo $@
URL_TEST=$2
OUTPUT_FOLDER=$(pwd)/output
mkdir -p $OUTPUT_FOLDER
echo "" > $OUTPUT_FOLDER/test-details.txt
time_start=$(date +%s)
$1 &
MY_PID=$!
sleep 0.02
psrecord $MY_PID --plot "$OUTPUT_FOLDER/$3.png" --log "$OUTPUT_FOLDER/$3.log" --interval 0.2 & PSRECORD_PID=$!
# sleep until ready
while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' $2)" != "200" ]]; do sleep 1; done
time_end=$(date +%s)
time_spent=$((($time_end - $time_start)))
echo "=== Server ready after $time_spent s"
echo "Time to start the server: $time_spent s" >> $OUTPUT_FOLDER/test-details.txt
cpu_time_total="$(ps -p $MY_PID -o 'time=' | awk -F'[:.]+' '{t=$3*10+1000*($2+60*$1); print t}')"
echo "=== Total CPU time used: $cpu_time_total ms"
echo "Total CPU time used at startup: $cpu_time_total s" >> $OUTPUT_FOLDER/test-details.txt
sleep 1
echo "=== Starting load test"
time_start=$(date +%s)
ab -c 5 -n 5000 $URL_TEST > $OUTPUT_FOLDER/ab_output.txt
time_end=$(date +%s)
time_spent=$((($time_end - $time_start)))
echo "=== Load test spent $time_spent s"
echo "Time to finish the test: $time_spent s" >> $OUTPUT_FOLDER/test-details.txt
time_start=$(date +%s)
kill $MY_PID
time_end=$(date +%s)
time_spent=$((($time_end - $time_start)))
echo "=== Server stopped after $time_spent s"
echo "Time to stop the server: $time_spent s" >> $OUTPUT_FOLDER/test-details.txt
echo "=== Monitored Apache Bench Test - Ended"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment