ltp测试脚本ltpstress.sh分析

编程入门 行业动态 更新时间:2024-10-10 06:18:44

ltp测试<a href=https://www.elefans.com/category/jswz/34/1771291.html style=脚本ltpstress.sh分析"/>

ltp测试脚本ltpstress.sh分析

        LTP是由 Linux Test Project 所开发的一套系统测试套件。它基于系统资源的利用率统计开发了一个测试的组合,为系统提供足够的压力。通过压力测试来判断系统的稳定性和可靠性。压力测试是一种破坏性的测试,即系统在非正常的、超负荷的条件下的运行情况 。用来评估在超越最大负载的情况下系统将如何运行,是系统在正常的情况下对某种负载强度的承受能力的考验。

        服务器产品一般进行72h压力测试,桌面产品一般进行24小时压力测试,常用测试命令:

# ./ltpstress.sh -n -t 72

        其中 -n参数表示不进行网络相关测试,-t参数指定测试时长(小时为单位,最小为 1,默认24),该脚本最终调用genload程序来运行,genload根据传入的进程个数fork子进程,每个子进程的读写的内存为1GB,启动多少个进程由测试时的内存状态(含SWAP分区情况)决定。

         该脚本测试与系统下的SWAP分区息息相关:

         1)当系统下存在SWAP分区时,测试所用的内存大小为:全部物理内存 + 1/2 SWAP分区大小

        2)当系统下不存在SWAP分区时(或使用swapoff -a命令关闭swap分区时),测试所用的内存大小为:通过free -m命令获取的当前空闲内存(多次测试间或有不同)

        这个测试用例的本质是调用genload程序,以1G为单位来不断申请内存,然后执行写内存及释放的过程,ltpstrees.sh中只调用一次genload, genload根据 PROC_NUM个数来决定fork多少个genload子进程。

        genload --vm $PROC_NUM --vm-bytes 1073741824 >/dev/null 2>&1 &

       genload每次申请内存均以1G为单位进行,不足1G的会以剩余内存大小单独生成一个进程,例如:如果有31.5G可用内存,那么就创建32个genload进程。脚本执行后,可通过以下命令查看genload的状态和数量:

# ps -ef | grep genload

        以下是ltpstress.sh脚本分析:

#!/bin/sh#    Copyright (c) International Business Machines  Corp., 2003
#
#    This program is free software;  you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY;  without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#    the GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program;  if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#   FILE        : ltpstress.sh
#   DESCRIPTION : A script that will stress your system using the LTP testsuite.
#   REQUIREMENTS:
#                 1) The 'rsh' daemon must be running and NFS (versions 2 &3) must be
#                    configured into the kernel and installed for networking tests.
#		  2) The 'sar' application must be installed to use the "-S" option
#   HISTORY     :
#       02/11/2003 Robbie Williamson (robbiew@austin.ibm)
#               written
#	11/20/2008 Aime Le Rouzic (aime.lerouzic@bull)
#		adapt script to work with portmap and rpcbind
##############################################################export LTPROOT=${PWD}      #  ltp/testscripts
echo $LTPROOT | grep testscripts > /dev/null 2>&1    # 不在ltp/testscripts目录下时返回1,在时返回0
if [ $? -eq 0 ]; then    # 最后运行的命令返回的结束代码(0表示正常,其他表示异常)cd ..export LTPROOT=${PWD}    # $LTPROOT=ltp/
fi
export TMPBASE="/tmp"
export PATH=$LTPROOT/testcases/bin:$PATH
memsize=0
hours=24
PROC_NUM=0
leftover_memsize=0
duration=86400
datafile="/tmp/ltpstress.data"
iofile="/tmp/ltpstress.iodata"
logfile="/tmp/ltpstress.log"
interval=10
Sar=0
Top=0
Iostat=0
LOGGING=0
PRETTY_PRT=""
QUIET_MODE=""
NO_NETWORK=0usage()
{cat <<-END >&2usage: ${0##*/} [ -d datafile ] [ -i # (in seconds) ] [ -I iofile ] [ -l logfile ] [ -m # (in Mb) ][ -n ] [ -p ] [ -q ] [ -t duration ] [ -x TMPDIR ] [-b DEVICE] [-B LTP_DEV_FS_TYPE] [ [-S]|[-T] ]-d datafile     Data file for 'sar' or 'top' to log to. Default is "/tmp/ltpstress.data".-i # (in sec)   Interval that 'sar' or 'top' should take snapshots. Default is 10 seconds.-I iofile       Log results of 'iostat' to a file every interval. Default is "/tmp/ltpstress.iodata".-l logfile      Log results of test in a logfile. Default is "/tmp/ltpstress.log"-m # (in Mb)    Specify the _minimum_ memory load of # megabytes in background. Default is all the RAM + 1/2 swap.-n              Disable networking stress.-p              Human readable format logfiles.-q              Print less verbose output to the output files.-S              Use 'sar' to measure data.-T              Use LTP's modified 'top' tool to measure data.-t duration     Execute the testsuite for given duration in hours. Default is 24.-x TMPDIR       Directory where temporary files will be created.-b DEVICE       Some tests require an unmounted block deviceto run correctly. If DEVICE is not set, a loop device iscreated and used automatically.-B LTP_DEV_FS_TYPE The file system of DEVICE.example: ${0##*/} -d /tmp/sardata -l /tmp/ltplog.$$ -m 128 -t 24 -SEND
exit
}check_memsize()    #函数作用:检查测试所用内存大小,计算得出PROC_NUM大小
{while [ $memsize -gt 1048576 ]   #if greater than 1GB   #测试所用内存大于1GB时,$memsize单位为KB,1048576=1024*1024doPROC_NUM=$(( PROC_NUM + 1 ))    #PROC_NUM初始值为0memsize=$(( $memsize - 1048576 ))  #以1GB为单位递减,计算PROC_NUM数量doneleftover_memsize=$memsize   #leftover_memsize初始值为0,使用leftover_memsize记录小于1GB的内存部分,后续用来单独创建一个线程
}while getopts d:hi:I:l:STt:m:npqx:b:B:\? arg    #遍历参数,初始化状态
do  case $arg ind)	datafile="$OPTARG";;h)      echo "Help info:"usage;;i)	interval=$OPTARG;;I)	Iostat=1iofile=$OPTARG;;l)  logfile=$OPTARGLOGGING=1;;m)	memsize=$(($OPTARG * 1024))    # -m参数指定的测试内存大小,单位为MBcheck_memsize;;n)	NO_NETWORK=1;;p)	PRETTY_PRT=" -p ";;q)	QUIET_MODE=" -q ";;S)  if [ $Top -eq 0 ]; thenSar=1elseecho "Cannot specify -S and -T...exiting"exitfi;;T)	if [ $Sar -eq 0 ]; then$LTPROOT/testcases/bin/top -h 2>&1 | grep "\-f filename" >/dev/nullif [ $? -eq 0 ]; thenTop=1elseecho "ERROR: Please build and install the version of top in the /tools dir"exitfielseecho "Cannot specify -S and -T...exiting"exitfi;;t)  hours=$OPTARGduration=$(($hours * 60 * 60));;   #duration单位为秒x)	export TMPBASE=$(readlink -f ${OPTARG});;b)	export LTP_DEV=${OPTARG};;B)	export LTP_DEV_FS_TYPE=${OPTARG};;\?)     echo "Help info:"usage;;esac
doneexport TMP="${TMPBASE}/ltpstress-$$"    # $$ shell本身的pid    #TMP=/tmp/ltpstress-pid
export TMPDIR=${TMP}
mkdir -p ${TMP}    #创建临时目录,修改权限并进入目录# to write as user nobody into tst_tmpdir()
chmod 777 $TMP || \
{echo "unable to chmod 777 $TMP ... aborting"exit 1
}cd $TMP || \
{echo "could not cd ${TMP} ... exiting"exit 1
}if [ $NO_NETWORK -eq 0 ];then    #测试时未加-n参数,进行网络相关测试,测试时未使用# Networking setupecho `hostname` >> /root/.rhostschmod 644 /root/.rhostsnetstat -an | grep 514if [ $? -eq 1 ];thenecho "Error: 'rsh' daemon not active on this machine."exit 1fips -ef | grep portmap | grep -v grepif [ $? -eq 1 ];thenps -ef | grep rpcbind | grep -v grepif [ $? -eq 1 ];thenecho "Portmap and rpcbind not running"echo "Let's start portmap"/sbin/portmap &sleep 1ps -ef | grep portmap | grep -v grepif [ $? -eq 1 ];thenecho "Could not start portmap, Let's start rpcbind"/sbin/rpcbind &sleep 1ps -ef | grep rpcbind | grep -v grepif [ $? -eq 1 ];thenEcho "Error: Could not start rpcbind daemon."exit 1elseecho "The RPC test suite is using rpcbind"fielseecho "The RPC test suite is using portmap"fielseecho "The RPC test suite is using rpcbind"fielseecho "The RPC test suite is using portmap"fips -e | grep nfsdif [ $? -eq 1 ];then/usr/sbin/rpc.nfsdfisleep 1ps -e | grep nfsdif [ $? -eq 1 ];thenecho "Error: Could not start nfs server daemon."exit 1fips -e | grep rpc.statdif [ $? -eq 1 ];then/sbin/rpc.statdfisleep 1ps -e | grep rpc.statdif [ $? -eq 1 ];thenecho "Error: Could not start statd daemon."exit 1fips -e | grep rpc.mountdif [ $? -eq 1 ];then/usr/sbin/rpc.mountdfisleep 1ps -e | grep rpc.mountdif [ $? -eq 1 ];thenecho "Error: Could not start mountd daemon."exit 1fi# End of network setup
fi   #结束网络相关测试#If -m not set, use all the RAM + 1/2 swapspace
if [ $memsize -eq 0 ]; then     #$memsize初始值为0TOTALRAM=$(free -m | grep 内存: | awk {'print $2'})   #取出free输出的total mem数值,单位为MBTOTALSWAP=$(free -m | grep 交换: | awk {'print $2'})  #取出free输出的total Swap数值,单位为MBTESTSWAP=$(($TOTALSWAP / 2))   #测试用到的TESTSWAP空间为总SWAP大小的一半if [ $TESTSWAP -eq 0 ]; then   #没有SWAP空间或关闭了SWAP空间#if there is no swap in the system, use only the free RAMTESTMEM=$(free -m | grep Mem: | awk {'print $4'})  #没有SWAP参与的情况下,测试所用的TESTMEM大小为free输出的空闲空间大小(非total大小),单位为MBelseTESTMEM=$(($TESTSWAP + $TOTALRAM))  #有SWAP参与的情况下,测试所用的TESTMEM大小为总内存+1/2SWAP空间大小,单位为MBfi#Convert to kilobytesmemsize=$(($TESTMEM * 1024))  #将TESTMEM数值转换为KB,保存在memsize变量check_memsize   #调用check_memsize函数
fi# Set max processes to unlimited.
ulimit -u unlimited    #解除最大线程限制if [ $PROC_NUM -gt 0 ];thengenload --vm $PROC_NUM --vm-bytes 1073741824 >/dev/null 2>&1 &   #-m, --vm n  spawn n procs spinning on malloc()
fi                                                                 #--vm-bytes b  malloc chunks of b bytes (default is 256MB),以字节Byte为单位,1073741824=1024*1024*1024,Byte转为GB,每个线程申请1GB内存
if [ $leftover_memsize -gt 0 ];then    #将小于1Gb的内存部分单独创建一个线程genload --vm 1 --vm-bytes $(($leftover_memsize * 1024)) >/dev/null 2>&1 &
fiif [ $NO_NETWORK -eq 0 ];then    #加了-n参数则不执行if内语句netpipe.sh >/dev/null 2>/dev/null &
fi
${LTPROOT}/bin/rand_lines -g ${LTPROOT}/runtest/stress.part1 > ${TMP}/stress.part1
${LTPROOT}/bin/rand_lines -g ${LTPROOT}/runtest/stress.part2 > ${TMP}/stress.part2
${LTPROOT}/bin/rand_lines -g ${LTPROOT}/runtest/stress.part3 > ${TMP}/stress.part3sleep 2if [ $Sar -eq 1 ]; thensar -o $datafile $interval > /dev/null &
fiif [ $Top -eq 1 ]; thenscreen -d -m $LTPROOT/testcases/bin/top -o $datafile -d $interval &SCREEN_PID=$(ps -e | grep screen | awk {'print $1'})
fisleep 2if [ $Iostat -eq 1 ]; thenwhile [ 0 = 0 ];do iostat -dt >> $iofile; sleep $interval;done &Iostat_PID=$?
fisleep 2output1=${TMPBASE}/ltpstress.$$.output1
output2=${TMPBASE}/ltpstress.$$.output2
output3=${TMPBASE}/ltpstress.$$.output3${LTPROOT}/bin/ltp-pan -e ${PRETTY_PRT} ${QUIET_MODE} -S -t ${hours}h -a stress1 -n stress1 -l $logfile -f ${TMP}/stress.part1 -o $output1 &
${LTPROOT}/bin/ltp-pan -e ${PRETTY_PRT} ${QUIET_MODE} -S -t ${hours}h -a stress2 -n stress2 -l $logfile -f ${TMP}/stress.part2 -o $output2 &
${LTPROOT}/bin/ltp-pan -e ${PRETTY_PRT} ${QUIET_MODE} -S -t ${hours}h -a stress3 -n stress3 -l $logfile -f ${TMP}/stress.part3 -o $output3 &echo "Running LTP Stress for $hours hour(s) using $(($memsize/1024)) Mb"  #此处$memsize大小为经过check_memsize后小于1GB的部分
echo ""
echo "Test output recorded in:"
echo "        $output1"
echo "        $output2"
echo "        $output3"# Sleep a little longer than duration to let ltp-pan "try" to gracefully end itself.
sleep $(($duration + 10))if [ $Sar -eq 1 ]; thenkillall -9 sadc >/dev/null 2>&1
fi
if [ $Top -eq 1 ]; thenkill $SCREEN_PID >/dev/null 2>&1
fi
killall -9 ltp-pan >/dev/null 2>&1
killall -9 genload >/dev/null 2>&1
if [ $NO_NETWORK -eq 0 ];thenkillall -9 netpipe.sh >/dev/null 2>&1killall -9 NPtcp >/dev/null 2>&1
fi
if [ $Iostat -eq 1 ];thenkill -9 $Iostat_PID >/dev/null 2>&1
fi
rm -rf ${TMP}
echo "Testing done"
if [ $LOGGING -eq 1 ];thenif [ ! -z $PRETTY_PRT ]; thengrep FAIL $logfile > /dev/null 2>&1elsegrep 'stat=' $logfile | grep -v 'stat=0' > /dev/null 2>&1fiif [ $? -eq 1 ]; thenecho "All Tests PASSED!"elseecho "Testing yielded failures. See logfile: $logfile"if [ $NO_NETWORK -eq 1 ];thenecho "The NFS related tests should fail because network stress was disabled"fifi
fi

更多推荐

ltp测试脚本ltpstress.sh分析

本文发布于:2024-02-13 04:50:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1690751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   测试   ltp   ltpstress   sh

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!