jmeter使用_性能工具之Jmeter使用shell启动

背景

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

效果

a2dc2a8b5ca33591fa3a1a0de194c49e.png

shell脚本

2f925ddab9444a8d4005a28782f401ea.png

a32cacf19c0901ca70784a7db381f5ec.png

Jmeter环境变量配置参考

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

验证

9f4edb6a315d12d0b0009df7f944edb0.png

shell脚本参考代码

#!/bin/bash# author:liwen# 7DGroup# 2019/12/08/20/50cmdsh="/home/gaolou/apache-jmeter-3.1/bin/JMeterPluginsCMD.sh"case=$1mNum=$2durationTime=$3mark=$4resultDataFileName="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 1fiif [ -z ${mNum} ];then        echo -e "\033[32m请输入脚本名,线程数,执行时间,备注! 输入线程数\033[1m"        echo "ERROR"        exit 1fiif [ -z ${durationTime} ];then        echo -e "\033[32m请输入脚本名,线程数,执行时间,备注! 输入备注\033[1m"        echo "ERROR"        exit 1fioneTest(){    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$((2*mNum)) ${filename}.jmx    sed -i "s#name=\"ThreadGroup\.num_threads\">1$((1*mNum)) ${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 "" ${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}oneTestecho -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$((2*mNum)) ${filename}.jmx    sed -i "s#name=\"ThreadGroup\.num_threads\">1$((1*mNum)) ${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 "" ${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

第二步

脚本写法需要注意

18e8e29222ad030e62c79f3290732d0d.png

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

脚本替换xml说明

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

3da8678f3a1ff7a5fd32b00e94d8134e.png

脚本存放目录

5c9526a4d7572bc678cb90b58b5b65ad.png

0b4bb3291bdc5361ed8cd6807157662a.png

第三步

shell脚本存放位置

557b8d1c096cd96110d9972480e633ea.png

第四步

执行结果

8c71d961373a289ddae60e99f190463c.png

  1. 表示当前执行的脚本名字

  2. 表示原始脚本

  3. csc结果保存

执行脚本结果目录说明

445d1d4bc72ca7e38a7a32e9b4acf56f.png

说明:

f0fe536d7bbe26cfc0c7636c3ab842c4.png

打开脚本

45e63c762efe16fc8b017846f2c18186.png

vim日志

074bdea6888279999833ce177af3bb6e.png

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

下载报告

tar命令解包:tar zxvf FileName.tar打包:tar zcvf FileName.tar DirName

sz filename.tar

84e0a57b0370b452be1d32dc823d2a72.png

下载解压后打开

722898483ef365d0e44604172a0ec983.png

tps

50689d2b1a4865c4fcee789245add490.png

总结

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

送给大家李煜的词:

                  《相见欢》

无言独上西楼,月如钩,寂寞梧桐深院锁清秋。

剪不断,理还乱,是离愁,别是一般滋味在心头。 

5fc447e808ca5a6853482d38072b14e0.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值