关于jmeter:性能工具之Jmeter使用shell启动

41次阅读

共计 5909 个字符,预计需要花费 15 分钟才能阅读完成。

背景

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

成果

shell 脚本

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\”>.*</#ThreadGroup.scheduler\”>true</#g” ${filenam

e}.jmx

sed -i “s#LoopController.loops\”>.*</#LoopController.loops\”>-1</#g” ${filename}.jmx

sed -i “s#LoopController.continue_forever\”>.*</#LoopController.continue_forever\”>true</#g” ${filename}.jmx

sed -i “s#ThreadGroup.duration\”>.*</#ThreadGroup.duration\”>${durationTime}</#g” ${filename}.jmx
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 "<stringProp name=\"ThreadGroup.num_threads\">" ${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\”>.*</#ThreadGroup.scheduler\”>true</#g” ${filename}.jmx

sed -i “s#LoopController.loops\”>.*</#LoopController.loops\”>-1</#g” ${filename}.jmx

sed -i “s#LoopController.continue_forever\”>.*</#LoopController.continue_forever\”>true</#g” ${filename}.jmx

sed -i “s#ThreadGroup.duration\”>.*</#ThreadGroup.duration\”>${durationTime}</#g” ${filename}.jmx

命令执行解释:

阐明:通过封装 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 "<stringProp name=\"ThreadGroup.num_threads\">" ${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</stringProp>】

脚本寄存目录

第三步
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 查看执行日志。

正文完
 0