之前將sysbench scripts的使用說明文檔和sysbench scripts源碼分析文檔分享。現將所有的腳本分享大家,希望大家多多指正。
1、run.sh腳本
#! /bin/sh
###########################################################
# Copyright (c) 2012, Henry.Wang. All rights reserved.
#
# This program is a script to execute the all the other
# scripts to test. The purpose is to test the performance
# influence of the multi-threads for mysql
###########################################################
# set -x
# Get the key value of input arguments format like '--args=value'.
get_key_value()
{
echo "$1" | sed 's/^--[a-zA-Z_-]*=//'
}
# Usage will be helpful when you need to input the valid arguments.
usage()
{
cat <<EOF
Usage: $0 [configure-options]
-?, --help Show this help message.
--mysqldir=<> Set the mysql directory
--sysbenchdir=<> Set the sysbench directory
--defaults-file=<> Set the configure file for mysql
--host=<> Set the host name.
--port=<> Set the port number.
--database=<> Set the database to sysbench.
--user=<> Set the user name.
--password=<> Set the password of user.
--socket=<> Set the socket file
--tablesize=<> Set the table seize.
--engine=<> Set the sysbench engine.
--min-threads=<> Set the min threads number.
--max-threads=<> Set the max threads number.
--max-requests=<> Set the max requests number.
--max-time=<> Set the max time number.
--step=<> Set the thread incremental step.
--var=<> Set the variable to test.
--value=<> Set the value of the variable.
--interval=<> Set the interval time.
--count=<> Set the count of test.
-p,--prepare,--prepare=<> Set the prepare procedure.
-c,--cleanup,--cleanup=<> Set the cleanup procedure.
-r,--run,--run=<> Set the run procedure.
-s,--server,--server=<> Set the server whether start and shutdown
within the test or not.
--outputdir=<> Set the output directory.
Note: this script is intended for internal use by developers.
EOF
}
# Print the default value of the arguments of the script.
print_default()
{
cat <<EOF
The default value of the variables:
mysqldir $MYSQLDIR
sysbenchdir $SYSBENCHDIR
defaults-file $CONFIG
host $HOST
port $PORT
database $DATABASE
user $USER
password $PASSWORD
socket $SOCKET
tablesize $TABLESIZE
engine $ENGINE
min-threads $MIN_THREADS
max-threads $MAX_THREADS
max-requests $REQUESTS
max-time $TIME
step $STEP
var $VAR
value $VALUE
interval $INTERVAL
count $COUNT
prepare TRUE
cleanup TRUE
run TRUE
server TRUE
outputdir $OUTPUTDIR
EOF
}
# Parse the input arguments and get the value of the input argument.
parse_options()
{
while test $# -gt 0
do
case "$1" in
--mysqldir=*)
MYSQLDIR=`get_key_value "$1"`;;
--sysbenchdir=*)
SYSBENCHDIR=`get_key_value "$1"`;;
--defaults-file=*)
CONFIG=`get_key_value "$1"`;;
--host=*)
HOST=`get_key_value "$1"`;;
--port=*)
PORT=`get_key_value "$1"`;;
--database=*)
DATABASE=`get_key_value "$1"`;;
--user=*)
USER=`get_key_value "$1"`;;
--password=*)
PASSWORD=`get_key_value "$1"`;;
--socket=*)
SOCKET=`get_key_value "$1"`;;
--tablesize=*)
TABLESIZE=`get_key_value "$1"`;;
--engine=*)
ENGINE=`get_key_value "$1"`;;
--min-threads=*)
MIN_THREADS=`get_key_value "$1"`;;
--max-threads=*)
MAX_THREADS=`get_key_value "$1"`;;
--max-requests=*)
REQUESTS=`get_key_value "$1"`;;
--max-time=*)
TIME=`get_key_value "$1"`;;
--step=*)
STEP=`get_key_value "$1"`;;
--var=*)
VAR=`get_key_value "$1"`;;
--value=*)
VALUE=`get_key_value "$1"`;;
--interval=*)
INTERVAL=`get_key_value "$1"`;;
--count=*)
COUNT=`get_key_value "$1"`;;
-p | --prepare)
PREPARE=1;;
--prepare=*)
PREPARE=`get_key_value "$1"`;;
-r | --run)
RUN=1;;
--run=*)
RUN=`get_key_value "$1"`;;
-c | --cleanup)
CLEANUP=1;;
--cleanup=*)
CLEANUP=`get_key_value "$1"`;;
-s | --server)
SERVER=1;;
--server=*)
SERVER=`get_key_value "$1"`;;
--outputdir=*)
OUTPUTDIR=`get_key_value "$1"`+"/${DATETIME}";;
-? | --help)
usage
print_default
exit 0;;
*)
echo "Unknown option '$1'"
exit 1;;
esac
shift
done
}
############################################################
# Define the variables the script used for executing.
MYSQLDIR=/opt/Percona-Server
SYSBENCHDIR=/opt/sysbench
CONFIG=/opt/Percona-Server/etc/my.cnf
HOST=localhost
PORT=3306
DATABASE=test
USER=root
PASSWORD=
SOCKET=/tmp/mysql.sock
TABLESIZE=10000
ENGINE=innodb
VAR="full"
VALUE="default"
INTERVAL=1
MIN_THREADS=100
MAX_THREADS=1000
STEP=100
REQUESTS=10000
TIME=1000
COUNT=1
PREPARE=1
RUN=1
CLEANUP=1
SERVER=1
DATETIME=`date +%Y%m%d%H%M%S`
OUTPUTDIR=/opt/output/${DATETIME}
# Call the parse_options function to parse the input arguments.
parse_options "$@"
# Start the mysql server for the test
if [ $SERVER -eq 1 ]
then
echo "----Start mysql server----"
./test/server_op.sh --mysqldir=$MYSQLDIR --defaults-file=$CONFIG --host=$HOST --port=$PORT --user=$USER \
--password=$PASSWORD --socket=$SOCKET -s
SERVER=2
fi
if [ $? -ne 0 ]
then
echo "Exit with error when run server_op.sh procedure!"
exit -1
fi
# Run the mysql_vars.sh script to collect the variables value of mysql server.
echo "----Run mysql_vars.sh script----"
./test/mysql_vars.sh --mysqldir=$MYSQLDIR --host=$HOST --port=$PORT --user=$USER \
--password=$PASSWORD --socket=$SOCKET --outputdir=$OUTPUTDIR
if [ $? -ne 0 ]
then
echo "Exit with error when run mysql_vars.sh procedure!"
exit -1
fi
threads=$MIN_THREADS
# Test the influence of the given argument with different threads .
while [ $threads -le $MAX_THREADS ]
do
# If the number of min threads adds step bigger than the number of max threads,
# then set outputdir to the subdirectory of outputdir.
if [ $MIN_THREADS -lt $MAX_THREADS ]
then
threadpath=${OUTPUTDIR}/${threads}
else
threadpath=${OUTPUTDIR}
fi
iter=1
while [ $iter -le $COUNT ]
do
# Start the mysql server if not started.
if [ $SERVER -eq 1 ]
then
echo "----Start mysql server----"
./test/server_op.sh --mysqldir=$MYSQLDIR --defaults-file=$CONFIG --host=$HOST --port=$PORT --user=$USER \
--password=$PASSWORD --socket=$SOCKET -s
SERVER=2
fi
# If the count variable bigger than 1, then set outputdir to
# the subdirectory of outputdir.
if [ $COUNT -eq 1 ]
then
iterpath=${threadpath}
else
iterpath=${threadpath}/${iter}
fi
[[ -d $iterpath ]] || mkdir -p $iterpath
# Run the sysbench_oltp.sh script to prepare the test environment
if [ $PREPARE -eq 1 ]
then
echo "----Prepare the test environment----"
./test/sysbench_oltp.sh --sysbenchdir=$SYSBENCHDIR --host=$HOST --port=$PORT \
--database=$DATABASE --user=$USER --password=$PASSWORD --socket=$SOCKET \
--tablesize=$TABLESIZE --engine=$ENGINE --threads=$threads \
--max-requests=$REQUESTS --max-time=$TIME --var=$VAR --value=$VALUE \
--outputdir=$iterpath -p
if [ $? -ne 0 ]
then
echo "Exit with error when prepare procedure!"
exit -1
fi
fi
# Run the iostat.sh script to collect the io status of system.
echo "----Run iostat.sh script----"
./test/iostat.sh --interval=$INTERVAL --outputdir=$iterpath &
PIDIOSTAT=$!
if [ $? -ne 0 ]
then
echo "Exit with error when run iostat.sh procedure!"
killall -9 $PIDIOSTAT
exit -1
fi
# Run the vmstat.sh script to collect the vitual memory status of system.
echo "----Run vmstat.sh script----"
./test/vmstat.sh --interval=$INTERVAL --outputdir=$iterpath &
PIDVMSTAT=$!
if [ $? -ne 0 ]
then
echo "Exit with error when run vmstat.sh procedure!"
killall -9 $PIDVMSTAT
exit -1
fi
# Run the global_stat.sh script to collect the global status of mysql server.
echo "----Run global_stat.sh script----"
./test/global_stat.sh --mysqldir=$MYSQLDIR --host=$HOST --port=$PORT --user=$USER \
--password=$PASSWORD --socket=$SOCKET --interval=$INTERVAL --outputdir=$iterpath &
PIDGSTAT=$!
if [ $? -ne 0 ]
then
echo "Exit with error when run global_stat.sh procedure!"
killall -9 $PIDGSTAT
exit -1
fi
# Run the innodb_stat.sh script to collect the innodb engine status of mysql server.
echo "----Run innodb_stat.sh script----"
./test/innodb_stat.sh --mysqldir=$MYSQLDIR --host=$HOST --port=$PORT --user=$USER \
--password=$PASSWORD --socket=$SOCKET --interval=$INTERVAL --outputdir=$iterpath &
PIDINNOSTAT=$!
if [ $? -ne 0 ]
then
echo "Exit with error when run innodb_stat.sh procedure!"
killall -9 $PIDINNOSTAT
exit -1
fi
# Run the sysbench_oltp.sh script to run the sysbench procedure.
if [ $RUN -eq 1 ]
then
echo "----Run the sysbench procedure----"
./test/sysbench_oltp.sh --sysbenchdir=$SYSBENCHDIR --host=$HOST --port=$PORT \
--database=$DATABASE --user=$USER --password=$PASSWORD --socket=$SOCKET \
--tablesize=$TABLESIZE --engine=$ENGINE --threads=$threads \
--max-requests=$REQUESTS --max-time=$TIME --var=$VAR --value=$VALUE \
--outputdir=$iterpath -r
if [ $? -ne 0 ]
then
echo "Exit with error when run procedure!"
exit -1
fi
fi
# Cleanup the collection procedures.
killall -9 ${PIDIOSTAT} ${PIDVMSTAT} ${PIDGSTAT} ${PIDINNOSTAT}
kill -9 $(ps -ef|grep "iostat.sh"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
kill -9 $(ps -ef|grep "vmstat.sh"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
kill -9 $(ps -ef|grep "sysbench"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
kill -9 $(ps -ef|grep "global_stat.sh"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
kill -9 $(ps -ef|grep "innodb_stat.sh"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
kill -9 $(ps -ef|grep "mysqladmin"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
kill -9 $(ps -ef|grep "mysqld_safe"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
# Run the sysbench_oltp.sh script to cleanup the test environment
if [ $CLEANUP -eq 1 ]
then
echo "----Cleanup the test environment----"
./test/sysbench_oltp.sh --sysbenchdir=$SYSBENCHDIR --host=$HOST --port=$PORT \
--database=$DATABASE --user=$USER --password=$PASSWORD --socket=$SOCKET \
--tablesize=$TABLESIZE --engine=$ENGINE --threads=$threads \
--max-requests=$REQUESTS --max-time=$TIME --var=$VAR --value=$VALUE \
--outputdir=$iterpath -c
if [ $? -ne 0 ]
then
echo "Exit with error when cleanup procedure!"
exit -1
fi
fi
# Shutdown the mysql server for new test.
if [ $SERVER -eq 2 ]
then
echo "----Shutdown mysql server----"
./test/server_op.sh --mysqldir=$MYSQLDIR --defaults-file=$CONFIG --host=$HOST --port=$PORT --user=$USER \
--password=$PASSWORD --socket=$SOCKET -d
SERVER=1
if [ $? -ne 0 ]
then
echo "Exit with error when cleanup procedure!"
exit -1
fi
fi
iter=$(($iter+1))
done
threads=$((${threads}+${STEP}))
done
echo "The running is successfully finished!"
echo "-------------------------------------"
exit 0