性能工具之Jmeter使用shell启动

背景

在linux中执行Jmeter脚本时候,大家是否一直使用【jmeter -n -t [jmx file] -l [results file] -e -o [Path to web report folder]】命令执行,是否想过通过shell命令执行或者python执行,今天简单介绍下shell命令执行,前置条件需要配置Jmeter环境变量,如果没有配置,需要在脚本中修改相应位置。

效果
性能工具之Jmeter使用shell启动_第1张图片
shell脚本
性能工具之Jmeter使用shell启动_第2张图片
Jmeter环境变量配置参考

vi ~/.bash_profile#jmeter:路径JMETER_HOME=/root/tools/apache-jmeter-3.3PATH=$PATH:$HOME/bin:$JMETER_HOME/bin:export PATH

执行生效:source ~/.bash_profile

验证

shell脚本参考代码

!/bin/bash

author:liwen

7DGroup

2019/12/08/20/50

cmdsh="/home/gaolou/apache-jmeter-3.1/bin/JMeterPluginsCMD.

sh"case=$1

mNum=$

2durationTime=$3

mark=$4

resultDataFileName="resultData.csv"

hostIps="101.201.210.163"

nowPwd=pwd
echo -e "\033[32m-压力机开始执行。。请等待-\033[1m"

echo testcaseName:${case}_${mNum}_${durationTime}_${mark}

if [ -z ${case} ];then

echo -e "\033[32m请输入脚本名,线程数,执行时间,备注! 输入脚本名\033[0m"

echo "ERROR"

exit 1

fi

if

[ -z ${mNum

} ];then

echo -e "\033[32m请输入脚本名,线程数,执行时间,备注! 输入线程数\033[1m"

echo "ERROR"

exit 1

fi

if [ -z ${durationTime} ];then

echo -e "\033[32m请输入脚本名,线程数,执行时间,备注! 输入备注\033[1m"

echo "ERROR"

exit 1

fi

oneTest()

{ filename=${case}_${mNum}_${durationTime}_${mark}

echo "filename:"${filename}

mkdir -p ${nowPwd}/${case}

cd ${nowPwd}/${case}

cp ${nowPwd}/testPlan/${case}.jmx ${nowPwd}/${case}/${case}.jmx

if [ ! -f ${resultDataFileName} ];then

echo -e "sceneName,sceneThreadNum,duration(s),interfaceName,interfaceNum,totalCount,tps,errorPersent,avgTims(ms),persentTime(ms)" > ${resultDataFileName}

fi

rm -rf ${filename}

mkdir ${filename}

cp ${case}.jmx ${nowPwd}/${case}/${filename}/${filename}.jmx

cd ${nowPwd}/${case}/${filename}/

sed -i "s#name=\"ThreadGroup.num_threads\">2<#name=\"ThreadGroup.num_threads\">$((2*mNum))<#g" ${filename}.jmx

sed -i "s#name=\"ThreadGroup.num_threads\">1<#name=\"ThreadGroup.num_threads\">$((1*mNum))<#g" ${filename}.jmx

sed -i "s#ERRORXML#${nowPwd}/${case}/${filename}/${filename}_ERROR.xml#g" ${filename}.jmx

sed -i "s#ThreadGroup.scheduler\">.*true

e}.jmx

sed -i "s#LoopController.loops\">.*-1

sed -i "s#LoopController.continue_forever\">.*true

sed -i "s#ThreadGroup.duration\">.*${durationTime}if [ -z ${hostIps} ];then

jmeter -n -t ${filename}.jmx -l ${filename}.jtl -j ${filename}.log

jmeter -n -t ${filename}.jmx -l ${filename}.jtl -j ${filename}.log -e -o ${filename}

else

jmeter -n -t ${filename}.jmx -R ${hostIps} -l ${filename}.jtl -j ${filename}.log -e -o ${filename}

jmeter -n -t ${filename}.jmx -R ${hostIps} -l ${filename}.jtl -j ${filename}.log

fi
${cmdsh} --generate-csv ${filename}.csv --input-jtl ${filename}.jtl --plugin-type AggregateReport

local sumThread=grep "" ${filename}.jmx |awk -F\> '{print $2}'|awk -F\< 'BEGIN{sum=0}{sum=sum+$1}END{print sum}'

if [ -z ${hostIps} ];then

hostNum=1

else

hostNum=echo ${hostIps}|awk -F, '{print NF}'

let sumThread=sumThread*hostNum

let mNum=mNum*hostNum

fi

sed -n '2,$p' ${filename}.csv|grep -v TOTAL| awk -F, -v case=${filename} -v tnum=${sumThread} -v duration=${durationTime}

'{printf("%s,%s,%s,%s,%s,%s,%s,%s,90%time:%s;95%time:%s;99%time:%s\n",case,tnum,duration,

$1,$2,$11,$10,$3,$5,$6,$7)}' >> sDGroup

local totalGroup=cat sDGroup|wc -l

for((i=1;i<=${totalGroup};i++))

do

sed -n "${i}p" sDGroup|awk -F, -v tnum=${mNum} '{printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",$1,$2,$3,$4,tnum,$5,$6,$7,$8,$9)}' >>../${resultDataFileName}

    done

}

oneTest
echo -e "\033[32m-压力已经结束-\033[1m"

解释:
第一步

脚本执行方法:

先给予可执行权限:chomd a+x startJmeter.sh

执行方法:sh startJmeter.sh 脚本名字 并发数  执行时间  备注

解释:

cmdsh="/home/gaolou/apache-jmeter-3.1/bin/JMeterPluginsCMD.sh"

case=$1 #脚本名字

mNum=$2 #并发数

durationTime=$3 #执行时间

mark=$4 #备注

resultDataFileName="resultData.csv"  #csv命令保存数据

hostIps="101.201.210.163" 

  #如果是多台机器需要取消该注释,把压力机器全部加上

关键地方解释:

说明:通过sed -i 替换执行并发数、执行时间等信息

sed -i "s#name=\"ThreadGroup.num_threads\">2<#name=\"ThreadGroup.num_threads\">$((2*mNum))<#g" ${filename}.jmx

sed -i "s#name=\"ThreadGroup.num_threads\">1<#name=\"ThreadGroup.num_threads\">$((1*mNum))<#g" ${filename}.jmx

sed -i "s#ERRORXML#${nowPwd}/${case}/${filename}/${filename}_ERROR.xml#g" ${filename}.jmx

sed -i "s#ThreadGroup.scheduler\">.*true

sed -i "s#LoopController.loops\">.*-1

sed -i "s#LoopController.continue_forever\">.*true

sed -i "s#ThreadGroup.duration\">.*${durationTime}

命令执行解释:

说明:通过封装Jmeter -n -t 。。。等信息去执行脚本,该执行命令可以根据自己需要修改

if [ -z ${hostIps} ];then

jmeter -n -t ${filename}.jmx -l ${filename}.jtl -j ${filename}.log

jmeter -n -t ${filename}.jmx -l ${filename}.jtl -j ${filename}.log -e -o ${filename}

else

jmeter -n -t ${filename}.jmx -R ${hostIps} -l ${filename}.jtl -j ${filename}.log -e -o ${filename}

jmeter -n -t ${filename}.jmx -R ${hostIps} -l ${filename}.jtl -j ${filename}.log

fi

结果保存解释:

说明:通过插件 JMeterPluginsCMD.sh执行获取csv数据,如果想知道该插件详细信息可以百度查询怎么使用。

${cmdsh} --generate-csv ${filename}.csv --input-jtl ${filename}.jtl --plugin-type AggregateReport

local sumThread=grep "" ${filename}.jmx |awk -F\> '{print $2}'|awk -F\< 'BEGIN{sum=0}{sum=sum+$1}END{print sum}'

if [ -z ${hostIps} ];then

hostNum=1

else

hostNum=echo ${hostIps}|awk -F, '{print NF}'

let sumThread=sumThread*hostNum

let mNum=mNum*hostNum

fi

sed -n '2,$p' ${filename}.csv|grep -v TOTAL| awk -F, -v case=${filename} -v tnum=${sumThread} -v duration=${durationTime}

'{printf("%s,%s,%s,%s,%s,%s,%s,%s,90%time:%s;95%time:%s;99%time:%s\n",case,tnum,duration,

$1,$2,$11,$10,$3,$5,$6,$7)}' >> sDGroup

local totalGroup=cat sDGroup|wc -l

for((i=1;i<=${totalGroup};i++))

do

sed -n "${i}p" sDGroup|awk -F, -v tnum=${mNum} '{printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",$1,$2,$3,$4,tnum,$5,$6,$7,$8,$9)}' >>../${resultDataFileName}

done

第二步
脚本写法需要注意

上面脚本写法需要多多注意,否则在通过脚本执行会达不到自己效果;

脚本替换xml说明

实际上shell脚本是替换【ThreadGroup.num_threads">1】

脚本存放目录

第三步
shell脚本存放位置

第四步
执行结果

表示当前执行的脚本名字

表示原始脚本

csc结果保存

执行脚本结果目录说明

说明:

打开脚本

vim日志

通过查看日志可以看出执行全部信息,这样方便脚本调试脚本与错误跟踪

下载报告

tar命令

解包:tar zxvf FileName.tar

打包:tar zcvf FileName.tar DirName

sz filename.tar

下载解压后打开

tps

总结

     通过shell脚本顺利执行Jmeter脚本,如果是长时间执行可以采用后台执行方法,加上【nohup sh startJm.sh 脚本 并发数据 执行时间 备注 &】这样执行不用担心ssh窗口执行Jmeter失败,通过tail -f nohup.log查看执行日志。

你可能感兴趣的:(jmeter)