Skip to content

Instantly share code, notes, and snippets.

@JCotton1123
Last active February 11, 2016 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JCotton1123/6b2ff725b1b8fc3a1757 to your computer and use it in GitHub Desktop.
Save JCotton1123/6b2ff725b1b8fc3a1757 to your computer and use it in GitHub Desktop.
Simple script to measure the time it takes to execute a series of MySQL queries
#!/bin/bash
user='root'
password=''
function usage() {
echo "usage: $0 <query-file> <default-database>"
exit 1
}
function main() {
query_file=$1
default_database=$2
if [ -z "$query_file" ]; then
echo "A query file is required"
exit 1
fi
echo "Index, Query Time"
counter=0
tmp_file=$(mktemp /tmp/mysqltest.XXXXXX)
cat $query_file | while read query; do
logerr -n "Executing query #${counter} ..."
query="SET SESSION query_cache_type = OFF; $query"
(time mysql --user=$user --password=$password $default_database -e "$query") 2>$tmp_file >/dev/null
[ $? -ne 0 ] && logerr "failed" && exit 1
query_time=$(cat $tmp_file | grep "real" | egrep -o "[0-9a-z\.]+$")
logerr " completed in $query_time"
echo "$counter, $query_time"
((counter++))
done
}
function logerr() {
if [ -z "$2" ]; then
>&2 echo "$1"
else
>&2 echo "$1" "$2"
fi
}
if [ -z $1 ] || [ "$1" == "-h" ]; then
usage
fi
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment