Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1731417
  • 博文数量: 107
  • 博客积分: 1715
  • 博客等级: 上尉
  • 技术积分: 3168
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-18 18:42
个人简介

阿里巴巴DBA,原去哪儿网DBA。专注于MySQL源码研究、DBA运维、CGroup虚拟化及Linux Kernel源码研究等。 github:https://github.com/HengWang/ Email:king_wangheng@163.com 微博 :@王恒-Henry QQ :506437736

文章分类

全部博文(107)

文章存档

2014年(2)

2013年(38)

2012年(67)

分类: Mysql/postgreSQL

2012-05-11 15:03:41

    以下将最近写的脚本分享给大家,希望大家多多指正。
1、run.sh脚本

点击(此处)折叠或打开

  1. #! /bin/sh

  2. ###########################################################
  3. # Copyright (c) 2012, Henry.Wang. All rights reserved.
  4. #
  5. # This program is a script to execute the all the other
  6. # scripts to test. The purpose is to test the performance
  7. # influence of the multi-threads for mysql
  8. ###########################################################

  9. # set -x

  10. # Get the key value of input arguments format like '--args=value'.
  11. get_key_value()
  12. {
  13.     echo "$1" | sed 's/^--[a-zA-Z_-]*=//'
  14. }

  15. # Usage will be helpful when you need to input the valid arguments.
  16. usage()
  17. {
  18. cat <<EOF
  19. Usage: $0 [configure-options]
  20.   -?, --help Show this help message.
  21.   --mysqldir=<> Set the mysql directory
  22.   --sysbenchdir=<> Set the sysbench directory
  23.   --defaults-file=<> Set the configure file for mysql
  24.   --host=<> Set the host name.
  25.   --port=<> Set the port number.
  26.   --database=<> Set the database to sysbench.
  27.   --user=<> Set the user name.
  28.   --password=<> Set the password of user.
  29.   --socket=<> Set the socket file
  30.   --tablesize=<> Set the table seize.
  31.   --engine=<> Set the sysbench engine.
  32.   --min-threads=<> Set the min threads number.
  33.   --max-threads=<> Set the max threads number.
  34.   --max-requests=<> Set the max requests number.
  35.   --max-time=<> Set the max time number.
  36.   --step=<> Set the thread incremental step.
  37.   --var=<> Set the variable to test.
  38.   --value=<> Set the value of the variable.
  39.   --interval=<> Set the interval time.
  40.   --count=<> Set the count of test.
  41.   -p,--prepare,--prepare=<> Set the prepare procedure.
  42.   -c,--cleanup,--cleanup=<> Set the cleanup procedure.
  43.   -r,--run,--run=<> Set the run procedure.
  44.   -s,--server,--server=<> Set the server whether start and shutdown
  45.                                    within the test or not.
  46.   --outputdir=<> Set the output directory.

  47. Note: this script is intended for internal use by developers.

  48. EOF
  49. }

  50. # Print the default value of the arguments of the script.
  51. print_default()
  52. {
  53. cat <<EOF
  54.   The default value of the variables:
  55.   
  56.   mysqldir $MYSQLDIR
  57.   sysbenchdir $SYSBENCHDIR
  58.   defaults-file $CONFIG
  59.   host $HOST
  60.   port $PORT
  61.   database $DATABASE
  62.   user $USER
  63.   password $PASSWORD
  64.   socket $SOCKET
  65.   tablesize $TABLESIZE
  66.   engine $ENGINE
  67.   min-threads $MIN_THREADS
  68.   max-threads $MAX_THREADS
  69.   max-requests $REQUESTS
  70.   max-time $TIME
  71.   step $STEP
  72.   var $VAR
  73.   value $VALUE
  74.   interval $INTERVAL
  75.   count $COUNT
  76.   prepare TRUE
  77.   cleanup TRUE
  78.   run TRUE
  79.   server TRUE
  80.   outputdir $OUTPUTDIR

  81. EOF
  82. }

  83. # Parse the input arguments and get the value of the input argument.
  84. parse_options()
  85. {
  86.   while test $# -gt 0
  87.   do
  88.     case "$1" in
  89.     --mysqldir=*)
  90.       MYSQLDIR=`get_key_value "$1"`;;
  91.     --sysbenchdir=*)
  92.       SYSBENCHDIR=`get_key_value "$1"`;;
  93.     --defaults-file=*)
  94.       CONFIG=`get_key_value "$1"`;;
  95.     --host=*)
  96.       HOST=`get_key_value "$1"`;;
  97.     --port=*)
  98.       PORT=`get_key_value "$1"`;;
  99.     --database=*)
  100.       DATABASE=`get_key_value "$1"`;;
  101.     --user=*)
  102.       USER=`get_key_value "$1"`;;
  103.     --password=*)
  104.       PASSWORD=`get_key_value "$1"`;;
  105.     --socket=*)
  106.       SOCKET=`get_key_value "$1"`;;
  107.     --tablesize=*)
  108.       TABLESIZE=`get_key_value "$1"`;;
  109.     --engine=*)
  110.       ENGINE=`get_key_value "$1"`;;
  111.     --min-threads=*)
  112.       MIN_THREADS=`get_key_value "$1"`;;
  113.     --max-threads=*)
  114.       MAX_THREADS=`get_key_value "$1"`;;
  115.     --max-requests=*)
  116.       REQUESTS=`get_key_value "$1"`;;
  117.     --max-time=*)
  118.       TIME=`get_key_value "$1"`;;
  119.     --step=*)
  120.       STEP=`get_key_value "$1"`;;
  121.     --var=*)
  122.       VAR=`get_key_value "$1"`;;
  123.     --value=*)
  124.       VALUE=`get_key_value "$1"`;;
  125.     --interval=*)
  126.       INTERVAL=`get_key_value "$1"`;;
  127.     --count=*)
  128.       COUNT=`get_key_value "$1"`;;
  129.     -p | --prepare)
  130.       PREPARE=1;;
  131.     --prepare=*)
  132.       PREPARE=`get_key_value "$1"`;;
  133.     -r | --run)
  134.       RUN=1;;
  135.     --run=*)
  136.       RUN=`get_key_value "$1"`;;
  137.     -c | --cleanup)
  138.       CLEANUP=1;;
  139.     --cleanup=*)
  140.       CLEANUP=`get_key_value "$1"`;;
  141.     -s | --server)
  142.       SERVER=1;;
  143.     --server=*)
  144.       SERVER=`get_key_value "$1"`;;
  145.     --outputdir=*)
  146.       OUTPUTDIR=`get_key_value "$1"`+"/${DATETIME}";;
  147.     -? | --help)
  148.       usage
  149.       print_default
  150.       exit 0;;
  151.     *)
  152.       echo "Unknown option '$1'"
  153.       exit 1;;
  154.     esac
  155.     shift
  156.   done
  157. }

  158. ############################################################
  159. # Define the variables the script used for executing.
  160. MYSQLDIR=/opt/Percona-Server
  161. SYSBENCHDIR=/opt/sysbench
  162. CONFIG=/opt/Percona-Server/etc/my.cnf
  163. HOST=localhost
  164. PORT=3306
  165. DATABASE=test
  166. USER=root
  167. PASSWORD=
  168. SOCKET=/tmp/mysql.sock
  169. TABLESIZE=10000
  170. ENGINE=innodb
  171. VAR="full"
  172. VALUE="default"
  173. INTERVAL=1
  174. MIN_THREADS=100
  175. MAX_THREADS=1000
  176. STEP=100
  177. REQUESTS=10000
  178. TIME=1000
  179. COUNT=1
  180. PREPARE=1
  181. RUN=1
  182. CLEANUP=1
  183. SERVER=1
  184. DATETIME=`date +%Y%m%d%H%M%S`
  185. OUTPUTDIR=/opt/output/${DATETIME}

  186. # Call the parse_options function to parse the input arguments.
  187. parse_options "$@"

  188. # Start the mysql server for the test
  189. if [ $SERVER -eq 1 ]
  190. then
  191.   echo "----Start mysql server----"
  192.   ./test/server_op.sh --mysqldir=$MYSQLDIR --defaults-file=$CONFIG --host=$HOST --port=$PORT --user=$USER \
  193.   --password=$PASSWORD --socket=$SOCKET -s
  194.   SERVER=2
  195. fi

  196. if [ $? -ne 0 ]
  197. then
  198.   echo "Exit with error when run server_op.sh procedure!"
  199.   exit -1
  200. fi

  201. # Run the mysql_vars.sh script to collect the variables value of mysql server.
  202. echo "----Run mysql_vars.sh script----"
  203. ./test/mysql_vars.sh --mysqldir=$MYSQLDIR --host=$HOST --port=$PORT --user=$USER \
  204. --password=$PASSWORD --socket=$SOCKET --outputdir=$OUTPUTDIR

  205. if [ $? -ne 0 ]
  206. then
  207.   echo "Exit with error when run mysql_vars.sh procedure!"
  208.   exit -1
  209. fi

  210. threads=$MIN_THREADS
  211. # Test the influence of the given argument with different threads .
  212. while [ $threads -le $MAX_THREADS ]
  213. do

  214.   # If the number of min threads adds step bigger than the number of max threads,
  215.     # then set outputdir to the subdirectory of outputdir.
  216.     if [ $MIN_THREADS -lt $MAX_THREADS ]
  217.     then
  218.      threadpath=${OUTPUTDIR}/${threads}
  219.     else
  220.      threadpath=${OUTPUTDIR}
  221.     fi

  222.   iter=1
  223.   while [ $iter -le $COUNT ]
  224.   do
  225.   
  226.     # Start the mysql server if not started.
  227.     if [ $SERVER -eq 1 ]
  228.     then
  229.       echo "----Start mysql server----"
  230.       ./test/server_op.sh --mysqldir=$MYSQLDIR --defaults-file=$CONFIG --host=$HOST --port=$PORT --user=$USER \
  231.       --password=$PASSWORD --socket=$SOCKET -s
  232.       SERVER=2
  233.     fi
  234.     
  235.     # If the count variable bigger than 1, then set outputdir to
  236.     # the subdirectory of outputdir.
  237.     if [ $COUNT -eq 1 ]
  238.     then
  239.       iterpath=${threadpath}
  240.     else
  241.       iterpath=${threadpath}/${iter}
  242.     fi
  243.     
  244.     [[ -d $iterpath ]] || mkdir -p $iterpath
  245.     
  246.     # Run the sysbench_oltp.sh script to prepare the test environment
  247.     if [ $PREPARE -eq 1 ]
  248.     then
  249.       echo "----Prepare the test environment----"
  250.       ./test/sysbench_oltp.sh --sysbenchdir=$SYSBENCHDIR --host=$HOST --port=$PORT \
  251.       --database=$DATABASE --user=$USER --password=$PASSWORD --socket=$SOCKET \
  252.       --tablesize=$TABLESIZE --engine=$ENGINE --threads=$threads \
  253.       --max-requests=$REQUESTS --max-time=$TIME --var=$VAR --value=$VALUE \
  254.       --outputdir=$iterpath -p
  255.       if [ $? -ne 0 ]
  256.       then
  257.         echo "Exit with error when prepare procedure!"
  258.         exit -1
  259.       fi
  260.     fi
  261.         
  262.   # Run the iostat.sh script to collect the io status of system.
  263.     echo "----Run iostat.sh script----"
  264.     ./test/iostat.sh --interval=$INTERVAL --outputdir=$iterpath &
  265.     PIDIOSTAT=$!
  266.     
  267.     if [ $? -ne 0 ]
  268.     then
  269.       echo "Exit with error when run iostat.sh procedure!"
  270.       killall -9 $PIDIOSTAT
  271.       exit -1
  272.     fi
  273.     
  274.   # Run the vmstat.sh script to collect the vitual memory status of system.
  275.     echo "----Run vmstat.sh script----"
  276.     ./test/vmstat.sh --interval=$INTERVAL --outputdir=$iterpath &
  277.     PIDVMSTAT=$!
  278.     
  279.     if [ $? -ne 0 ]
  280.     then
  281.       echo "Exit with error when run vmstat.sh procedure!"
  282.       killall -9 $PIDVMSTAT
  283.       exit -1
  284.     fi
  285.     
  286.   # Run the global_stat.sh script to collect the global status of mysql server.
  287.     echo "----Run global_stat.sh script----"
  288.     ./test/global_stat.sh --mysqldir=$MYSQLDIR --host=$HOST --port=$PORT --user=$USER \
  289.     --password=$PASSWORD --socket=$SOCKET --interval=$INTERVAL --outputdir=$iterpath &
  290.     PIDGSTAT=$!
  291.     
  292.     if [ $? -ne 0 ]
  293.     then
  294.       echo "Exit with error when run global_stat.sh procedure!"
  295.       killall -9 $PIDGSTAT
  296.       exit -1
  297.     fi
  298.     
  299.   # Run the innodb_stat.sh script to collect the innodb engine status of mysql server.
  300.     echo "----Run innodb_stat.sh script----"
  301.     ./test/innodb_stat.sh --mysqldir=$MYSQLDIR --host=$HOST --port=$PORT --user=$USER \
  302.     --password=$PASSWORD --socket=$SOCKET --interval=$INTERVAL --outputdir=$iterpath &
  303.     PIDINNOSTAT=$!
  304.     
  305.     if [ $? -ne 0 ]
  306.     then
  307.       echo "Exit with error when run innodb_stat.sh procedure!"
  308.       killall -9 $PIDINNOSTAT
  309.       exit -1
  310.     fi
  311.     
  312.     # Run the sysbench_oltp.sh script to run the sysbench procedure.
  313.     if [ $RUN -eq 1 ]
  314.     then
  315.       echo "----Run the sysbench procedure----"
  316.        ./test/sysbench_oltp.sh --sysbenchdir=$SYSBENCHDIR --host=$HOST --port=$PORT \
  317.       --database=$DATABASE --user=$USER --password=$PASSWORD --socket=$SOCKET \
  318.       --tablesize=$TABLESIZE --engine=$ENGINE --threads=$threads \
  319.       --max-requests=$REQUESTS --max-time=$TIME --var=$VAR --value=$VALUE \
  320.       --outputdir=$iterpath -r
  321.       
  322.       if [ $? -ne 0 ]
  323.       then
  324.         echo "Exit with error when run procedure!"
  325.         exit -1
  326.       fi
  327.     fi
  328.     
  329.     
  330.   # Cleanup the collection procedures.
  331.     killall -9 ${PIDIOSTAT} ${PIDVMSTAT} ${PIDGSTAT} ${PIDINNOSTAT}
  332.     
  333.     kill -9 $(ps -ef|grep "iostat.sh"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
  334.     kill -9 $(ps -ef|grep "vmstat.sh"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
  335.     kill -9 $(ps -ef|grep "sysbench"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
  336.     kill -9 $(ps -ef|grep "global_stat.sh"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
  337.     kill -9 $(ps -ef|grep "innodb_stat.sh"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
  338.     kill -9 $(ps -ef|grep "mysqladmin"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
  339.     kill -9 $(ps -ef|grep "mysqld_safe"|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
  340.     
  341.   
  342.   # Run the sysbench_oltp.sh script to cleanup the test environment
  343.     if [ $CLEANUP -eq 1 ]
  344.     then
  345.       echo "----Cleanup the test environment----"
  346.        ./test/sysbench_oltp.sh --sysbenchdir=$SYSBENCHDIR --host=$HOST --port=$PORT \
  347.       --database=$DATABASE --user=$USER --password=$PASSWORD --socket=$SOCKET \
  348.       --tablesize=$TABLESIZE --engine=$ENGINE --threads=$threads \
  349.       --max-requests=$REQUESTS --max-time=$TIME --var=$VAR --value=$VALUE \
  350.       --outputdir=$iterpath -c
  351.       
  352.       if [ $? -ne 0 ]
  353.       then
  354.         echo "Exit with error when cleanup procedure!"
  355.         exit -1
  356.       fi
  357.     fi
  358.   
  359.   # Shutdown the mysql server for new test.
  360.     if [ $SERVER -eq 2 ]
  361.     then
  362.       echo "----Shutdown mysql server----"
  363.       ./test/server_op.sh --mysqldir=$MYSQLDIR --defaults-file=$CONFIG --host=$HOST --port=$PORT --user=$USER \
  364.       --password=$PASSWORD --socket=$SOCKET -d
  365.       SERVER=1
  366.       if [ $? -ne 0 ]
  367.       then
  368.         echo "Exit with error when cleanup procedure!"
  369.         exit -1
  370.       fi
  371.     fi
  372.   
  373.     iter=$(($iter+1))
  374.   done
  375.   
  376.   threads=$((${threads}+${STEP}))
  377. done

  378. echo "The running is successfully finished!"
  379. echo "-------------------------------------"
  380. exit 0

 
阅读(2877) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~