一、背景
在 Linux 中执行 JMeter 脚本时候,大家是否一直使用jmeter -n -t [jmx file] -l [results file] -e -o [Path to web report folder]
命令执行,是否想过通过 shell 脚本或者 python 快速执行.
今天简单介绍下 shell 脚本执行。前置条件需要配置 JMeter 环境变量,如果没有配置,需要在脚本中修改相应位置。
二、效果
shell 脚本:
三、环境变量配置
vi ~/.bash_profile
#jmeter:路径
JMETER_HOME=/root/tools/apache-jmeter-3.3
PATH=$PATH:$HOME/bin:$JMETER_HOME/bin:
export PATH
#执行生效:
source ~/.bash_profile
验证:
四、脚本解析
1、参考代码
#!/bin/bash
# author:7DGroup
# 2019/12/08/20/50
cmdsh="/home/gaolou/apache-jmeter-3.1/bin/JMeterPluginsCMD.sh"
case=$1
mNum=$2
durationTime=$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" ${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
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"
2、主要逻辑
脚本执行方法:
- 给予可执行权限:
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
JMX 脚本写法需要注意
这里实际上 shell 脚本是替换【ThreadGroup.num_threads">1】
3、目录说明
脚本存放目录:
shell 脚本存放位置:
执行结果:
- 表示当前执行的脚本名字
- 表示原始脚本
- csc 结果保存
执行脚本结果目录说明:
打开脚本:
vim日志:
4、下载报告
使用 tar 命令
- 解包:tar zxvf FileName.tar
- 打包:tar zcvf FileName.tar DirName
下载:
sz filename.tar
下载解压后打开:
五、总结
通过 shell 脚本执行Jmeter脚本,如果是长时间执行可以采用后台执行方法,加上 nohup 这样执行不用担心 ssh 窗口执行失败。
源码地址: