Skip to content

Instantly share code, notes, and snippets.

@noqqe
Last active December 19, 2015 03:39
Show Gist options
  • Select an option

  • Save noqqe/5891559 to your computer and use it in GitHub Desktop.

Select an option

Save noqqe/5891559 to your computer and use it in GitHub Desktop.
Simply generate some read and write SQL queries to a database cluster (mariadb in my case).
#!/bin/bash
## manual section
#
# options
# --rqmax Define the max read queries
# --rqpm Define read queries per minute
# --wqmax Define the max wirte queries
# --wqpm Define write queries per minute
#
# keep in mind: the read and write queries are additive
#
# usage
# ./sqltraffic.bash --rqmax 10000 --rqpm 60 --wqmax 600 --wqpm 10
#
## config section
# hosts to query
HOSTS=( 192.168.178.121 192.168.178.122 192.168.178.123 192.168.178.124 \
192.168.178.125 192.168.178.126 192.168.178.127 )
# mysql client opts
MYOPTS="-BN -u root -ppassword"
# define read query
RQ="SELECT id FROM X.t1 ORDER BY id DESC LIMIT 1;"
# define write query
WQ="INSERT INTO D.t1 (id, data) VALUES (' ' , 'foo' );"
# define a log file for reconstruction
LOG="output.log"
## runtime section
# parse options
until [ "$1" == "" ]; do
case $1 in
--rqmax ) shift ; Q=$1 ;;
--rqpm ) shift ; D=$1 ;;
--wqmax ) shift ; W=$1 ;;
--wqpm ) shift ; P=$1 ;;
* ) shift ; shift ;; # shift ALL the things!
esac
shift
done
N=${#HOSTS[@]}
function readqueries () {
local CR=0
local SLEEP=$(echo "60 $D" | awk '{printf "%f", $1 / $2}')
until [ $CR -ge $Q ]; do
H=${HOSTS[$((RANDOM % N))]}
mysql $MYOPTS -h $H -e "$RQ" &>/dev/null
echo "R $H $(date --rfc-3339=ns ) $CR"
sleep $SLEEP
((CR++))
done
echo "FIN at $CR"
}
function writequeries () {
local CW=0
local SLEEP=$(echo "60 $P" | awk '{printf "%f", $1 / $2}')
until [ $CW -ge $W ]; do
H=${HOSTS[$((RANDOM % N))]}
mysql $MYOPTS -h $H -e "$WQ" &>/dev/null
echo "W $H $(date --rfc-3339=ns ) $CW"
sleep $SLEEP
((CW++))
done
echo "FIN at $CW"
}
# lets have some fun
echo > $LOG
(readqueries >> $LOG ) &
(writequeries >> $LOG ) &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment