Last active
July 30, 2022 22:36
-
-
Save MateusBMP/6f2bd039856eb9a613af91a75034290c to your computer and use it in GitHub Desktop.
Calculate average mysql command duration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # usage: progress_bar <current> <max> | |
| # see: https://gist.github.com/darrenclark/1392848 | |
| progress_bar () { | |
| output="\r" | |
| output="$output [" | |
| total=$1 | |
| count=0 | |
| while [ $count -lt $total ]; do | |
| output="$output#" | |
| let count=$count+1 | |
| done | |
| let total=$2-$total | |
| count=0 | |
| while [ $count -lt $total ]; do | |
| output="$output " | |
| let count=$count+1 | |
| done | |
| output="$output] $i%" | |
| echo -ne "$output" | |
| } | |
| do_select () { | |
| local start_time=$(date +%s.%6N) | |
| # preferably use passwordless authentication | |
| mysql db << EOF > /dev/null 2>&1 | |
| SELECT * from db # change to any mysql script | |
| EOF | |
| local end_time=$(date +%s.%6N) | |
| local elapsed=$(echo "scale=6; $end_time - $start_time" | bc) | |
| printf "%.6f" $elapsed | |
| } | |
| echo "Running selects..." | |
| allResults=() | |
| # Change 100 to any test number | |
| for i in {1..100}; do | |
| progress_bar $i 100 | |
| duration=$(do_select) | |
| allResults+=( $duration ) | |
| done | |
| echo ""; echo "Calculating average..." | |
| printf "Avg: " | |
| echo "${allResults[@]}" | awk '{sum += $1; count++} END {print sum / count}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment