Skip to content

Instantly share code, notes, and snippets.

@wooglie
Created April 29, 2024 23:34
Show Gist options
  • Save wooglie/e29e881d4c14017cd987677f796b5520 to your computer and use it in GitHub Desktop.
Save wooglie/e29e881d4c14017cd987677f796b5520 to your computer and use it in GitHub Desktop.
Run a Node.js script for x amount of times, record each output to a file, and report the failure rate
#!/bin/bash
# Check if a number was provided as an argument
if [ $# -eq 0 ]
then
echo "Please provide a number of times to run the script."
exit 1
fi
# Directory where output files will be saved
output_dir="outputs"
mkdir -p "$output_dir"
# Files to store success and error counts
success_count_file="${output_dir}/success_count.txt"
error_count_file="${output_dir}/error_count.txt"
# Initialize success and error counters
success_count=0
error_count=0
start_time=$(date +%s)
# Main loop to run the Node.js script
for (( i=0; i<$1; i++ ))
do
# Filename based on current timestamp
timestamp=$(date +"%Y-%m-%dT%H:%M:%S%z")
filename="${output_dir}/${timestamp}.txt"
# Execute the Node.js script and capture its exit status
pnpm test > "$filename"
status=$?
# Increment counters based on exit status
if [ $status -eq 0 ]
then
((success_count++))
echo "Success: $filename"
else
((error_count++))
echo "Error: $filename"
fi
done
# Calculate the duration in seconds
end_time=$(date +%s)
duration=$((end_time - start_time))
echo "Total execution time: ${duration}s"
# Save the counters to files
echo "Successes: $success_count" > "$success_count_file"
echo "Errors: $error_count" > "$error_count_file"
echo "Script execution complete."
echo "Total: $1"
echo "Successes: $success_count"
echo "Errors: $error_count"
# Calculate total operations
total_count=$((success_count + error_count))
# Calculate failing rate as a percentage
if [ "$total_count" -eq 0 ]; then
failing_rate=0 # Avoid division by zero if total_count is 0
else
failing_rate=$((100 * error_count / total_count))
fi
echo "Failing rate: $failing_rate%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment