銜接上文,以下是test文件夾下的腳本。
7、sysbench_oltp.sh腳本
#! /bin/sh
###########################################################
# Copyright (c) 2012, Heng.Wang. All rights reserved.
#
# This program is benifit for sysbench oltp test.
###########################################################
# 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.
--sysbenchdir=<> Set the sysbench directory
--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.
--socket=<> Set the socket file
--tablesize=<> Set the table seize.
--engine=<> Set the sysbench engine.
--threads=<> Set the threads number.
--max-requests=<> Set the max request numbers.
--max-time=<> Set the max run time.
--var=<> Set the variable.
--value=<> Set the value of the variable.
-p,--prepare Set the prepare procedure.
-r,--run Set the run procedure.
-c,--cleanup Set the cleanup procedure.
--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:
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
prepare FALSE
run TRUE
cleanup FALSE
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
--sysbenchdir=*)
SYSBENCHDIR=`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"`;;
--threads=*)
THREADS=`get_key_value "$1"`;;
--max-requests=*)
REQUESTS=`get_key_value "$1"`;;
--max-time=*)
TIME=`get_key_value "$1"`;;
--var=*)
VAR=`get_key_value "$1"`;;
--value=*)
VALUE=`get_key_value "$1"`;;
-p | --prepare)
PREPARE=1;;
-r | --run)
RUN=1;;
-c | --cleanup)
CLEANUP=1;;
--outputdir=*)
OUTPUTDIR=`get_key_value "$1"`;;
-? | --help)
usage
print_default
exit 0;;
*)
echo "Unknown option '$1'"
exit 1;;
esac
shift
done
}
# Sysbench prepare procedure, that generated the test data and sysbench environment.
prepare()
{
$SYSBENCH --test=oltp --mysql-host=$HOST --mysql-port=$PORT --mysql-user=$USER \
--mysql-password=$PASSWORD --mysql-socket=$SOCKET --mysql-db=$DATABASE \
--mysql-table-engine=$ENGINE --oltp-table-size=$TABLESIZE prepare
if [ $? -ne 0 ]
then
echo "Exit with error when prepare procedure!"
exit -1
fi
}
# Sysbench run procedure, that test the mysql server with the given argments.
run()
{
$SYSBENCH --test=oltp --mysql-host=$HOST --mysql-port=$PORT --mysql-user=$USER \
--mysql-password=$PASSWORD --mysql-socket=$SOCKET --mysql-db=$DATABASE \
--mysql-table-engine=$ENGINE --oltp-table-size=$TABLESIZE --num-threads=$THREADS \
--max-requests=$REQUESTS --max-time=$TIME run
if [ $? -ne 0 ]
then
echo "Exit with error when run procedure!"
exit -1
fi
}
# Sysbench cleanup procedure, that cleanup the environment of sysbench test.
cleanup()
{
$SYSBENCH --test=oltp --mysql-host=$HOST --mysql-port=$PORT --mysql-user=$USER \
--mysql-password=$PASSWORD --mysql-socket=$SOCKET --mysql-db=$DATABASE \
--mysql-table-engine=$ENGINE cleanup
}
#############################################################
# Define the variables the script used for executing.
SYSBENCHDIR=/opt/sysbench
HOST=localhost
PORT=3306
DATABASE=test
USER=root
PASSWORD=
SOCKET=/tmp/mysql.sock
TABLESIZE=10000
ENGINE=innodb
THREADS=1
REQUEST=10000
TIME=1000
VAR="full"
VALUE="default"
PREPARE=0
CLEANUP=0
RUN=0
OUTPUTDIR=/opt/output
# Call the parse_options function to parse the input arguments.
parse_options "$@"
# Define the sysbench executable program.
SYSBENCH=$SYSBENCHDIR/bin/sysbench
# If the mysql and sysbench executable program is not exist, exit the script.
# Or, run the sysbench test.
if [ -f $SYSBENCH ]
then
# If the output directory is not exist, then make directory.
[[ -d $OUTPUTDIR ]] || mkdir -p $OUTPUTDIR
# Print the current value of the script arguments.
print_default | tee ${OUTPUTDIR}/sysbench_${VAR}_${VALUE}_thread_${THREADS}.cnf
if [ $PREPARE -ne 1 ] && [ $RUN -ne 1 ] && [ $CLEANUP -ne 1 ]
then
echo "Please be ensure you operation for sysbench test,the operations are"
echo "[--prepare| --run| --cleanup]."
exit 1
fi
if [ $PREPARE -eq 1 ]
then
# Cleanup the environment and the test data of the sysbench.
cleanup
# Prepare the environment and the test data for sysbench.
prepare
fi
if [ $RUN -eq 1 ]
then
# Running the sysbench test.
run | tee ${OUTPUTDIR}/sysbench_${VAR}_${VALUE}_thread_${THREADS}.res
fi
if [ $CLEANUP -eq 1 ]
then
# Cleanup the environment and the test data of the sysbench.
cleanup
fi
else
echo "$SYSBENCH is not exist!"
echo "Please check the sysbench home directory."
exit -1
fi
echo "The test is successfully finished!"
echo "----------------------------------"
exit 0