Measure the success rate of a bash command

I had some flaky tests and needed a simple way to run the tests X times and measure the success rate. Here’s the script in case someone finds it helpful:

#!/usr/bin/env bash

# Replace my_command() below with what you want to do
my_command() {
  random_number=$(( ( RANDOM % 10 )  + 1 ))
  return_value=0
  if [ $random_number \> 7 ];
  then 
    return 1;
  fi;
  return 0;
}

total_runs=1000
fails=0
succeeds=0

for (( c=0; c<=$total_runs; c++ ))
do
  my_command &amp;&amp; succeeds=$((succeeds+1)) || fails=$((fails+1))
done

success_rate=$succeeds/$total_runs
echo "Success rate: $success_rate"

Instead of my_command I instead had make test.

Leave a Reply