background

When running the Jmeter script in Linux, do you always run the Jmeter -n -t [JMX file] -l [results file] -e -o [Path to Web Report Folder] command? If you want to use shell commands or Python commands, you need to configure Jmeter environment variables. If you do not, you need to modify the corresponding positions in the script.

 

The effect

A shell script

Jmeter environment variable configuration reference

vi ~ /. Following ` ` # jmeter: PATH ` ` JMETER_HOME = / root/tools/apache - jmeter - 3.3 ` ` PATH = $PATH: $HOME/bin: $JMETER_HOME/bin: ` ` export PATH

Source ~/.bash_profile

validation

Shell script reference code

#! /bin/bash

“`# author:liwen`

“`# 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 “[32 m – press begin execution.. Please wait – 033 [1 m \” `

“““`echo testcaseName:{case}_{mNum}_{durationTime}_{mark}`

“`if [ -z ${case} ]; then`

Echo -e “\033[32m Please enter script name, thread number, execution time, remarks! Enter script name \033[0m” ‘

“`echo “ERROR”`

“`exit 1`

“`fi`

“`if`

[ -z ${mNum

` ` `}]; then`

Echo -e “\033[32m Please enter script name, thread number, execution time, remarks! Enter thread number \033[1m” ‘

““`echo “ERROR”`

“`exit 1`

“`fi`

“`if [ -z ${durationTime} ]; then`

Echo -e “\033[32m Please enter script name, number of threads, execution time, remarks! Enter Remarks \033[1m” ‘

“`echo “ERROR”`

“`exit 1`

“`fi`

““““`oneTest()`

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

“`echo “filename:”${filename}`

“`mkdir -p
n o w P w d / {nowPwd}/
{case}`

“`cd
n o w P w d / {nowPwd}/
{case}`

“`cp
n o w P w d / t e s t P l a n / {nowPwd}/testPlan/
{case}.jmx
n o w P w d / {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
c a s e . j m x {case}.jmx
{nowPwd}/
c a s e / {case}/
{filename}/${filename}.jmx`

“`cd
n o w P w d / {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#
n o w P w d / {nowPwd}/
{case}/
f i l e n a m e / {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”>.*
{durationTime}

JMX -l filename.jmx− L {filename}.jtl -j ${filename}.log ‘

Jmeter -n -t filename.jmx− L {filename}.jmx -l filename.jmx− L {filename}.jtl -j filename.log−e−o{filename}.log -e -o Filename. The log – e – o {filename} `

“`else`

Jmeter -n -t filename.jmx−R{filename}.jmx -r filename.jmx−R{hostIps} -l filename.jtl−j{filename}.jtl-j Filename. JTL – j {filename} the log – e – o ${filename} `

JMX −R{filename}.jmx -r filename. JMX −R{hostIps} -l filename. JTL −j{filename}.jtl-j Filename. JTL – j {filename} log `

Fi CMDSH −generate− CSV {CMDSH} –generate-csv CMDSH −generate− CSV {filename}. CSV –input-jtl ${filename} –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’ p ‘p’ {filename}. CSV | grep -v TOTAL | awk – F, -v case= filename−vtnum={filename} -v tnum=filename−vtnum={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,

,11,11,10 ` ` ` say, 11, 3,3,3,5, 6,6,6,7)} ‘> > sDGroup `

““ local totalGroup=cat sDGroup|wc -l

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

“`do`

` ` ` sed -n “IP” sDGroup ∣ awk -, F – vtnum = {I} p “sDGroup | awk – F, -v tnum = IP “sDGroup ∣ awk -, F – vtnum = {mNum} {printf (” % s, % s, % s, % s, % s, % s, % s, % s, % s, % s \ n”, say, 3,3,3,4, tnum, 5,5,5,6, 7,7,7,8,9)} ‘> >.. /{resultDataFileName}`

“`    done`

` ` ` `}

OneTest echo -e “\033[32m- Pressure has ended -\033[1m”

Explanation:

The first step

Script execution method:

  • Run the chomd a+x startjmeter.sh command to grant the run permission
  • Run the following command: sh startjmeter. sh Concurrent script name Execution Time Remarks

Explanation:

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

Case =$1 # script name

MNum =$2 # number of concurrent requests

DurationTime =$3 # execution time

Mark =$4 # note ‘

ResultDataFileName =” resultdata.csv “# CSV command to save data ‘

` ` ` # hostIps = “101.201.210.163 `”

If more than one machine needs to cancel this comment, add all the pressure machine ‘

Key points to explain:

Note: Use sed -i to replace information such as the number of concurrent executions and execution time

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#
n o w P w d / {nowPwd}/
{case}/
f i l e n a m e / {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`

Description of command execution:

Jmeter-n-t… And so on to execute the script, the execution command can be modified according to their own needs

if [ -z ${hostIps} ]; then

JMX -l filename.jmx− L {filename}.jtl -j ${filename}.log ‘

Jmeter -n -t filename.jmx− L {filename}.jmx -l filename.jmx− L {filename}.jtl -j filename.log−e−o{filename}.log -e -o Filename. The log – e – o {filename} `

“`else`

Jmeter -n -t filename.jmx−R{filename}.jmx -r filename.jmx−R{hostIps} -l filename.jtl−j{filename}.jtl-j Filename. JTL – j {filename} the log – e – o ${filename} `

JMX −R{filename}.jmx -r filename. JMX −R{hostIps} -l filename. JTL −j{filename}.jtl-j Filename. JTL – j {filename} log `

“`fi`

Results saved and explained:

Jmeterpluginscmd. sh to obtain CSV data, if you want to know the details of the plug-in can be baidu query how to use.

${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’ p ‘p’ {filename}. CSV | grep -v TOTAL | awk – F, -v case= filename−vtnum={filename} -v tnum=filename−vtnum={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,

,11,11,10 ` ` ` say, 11, 3,3,3,5, 6,6,6,7)} ‘> > sDGroup `

““ local totalGroup=cat sDGroup|wc -l

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

“`do`

` ` ` sed -n “IP” sDGroup ∣ awk -, F – vtnum = {I} p “sDGroup | awk – F, -v tnum = IP “sDGroup ∣ awk -, F – vtnum = {mNum} {printf (” % s, % s, % s, % s, % s, % s, % s, % s, % s, % s \ n”, say, 3,3,3,4, tnum, 5,5,5,6, 7,7,7,8,9)} ‘> >.. /{resultDataFileName}`

“`done`

The second step

Script writing requires attention

The above script writing method needs a lot of attention, otherwise in the script execution will not reach their own effect;

The script replaces the XML specification

Threadgroup.num_threads “>1

Script directory

The third step

Shell script location

The fourth step

The execution result

  1. Represents the name of the script currently executed
  2. Represents the original script
  3. CSC results are saved

Description of script execution result directory

Description:

Open the script

Vim log

By viewing logs, you can see all execution information, which facilitates script debugging and error tracing

 

Download the report

The tar command

Tar ZXVF filename.tar

Package: tar ZCVF filename.tar DirName

sz filename.tar

 

Download unzip and open

tps

 

conclusion

The Jmeter script can be successfully executed by shell script. If it is executed for a long time, the background execution method can be adopted, and [nohup sh startjm. sh script concurrent data execution time Remarks &] can be used to execute it without worrying about the failure of Jmeter execution in SSH window. Run the tail -f nohup. Log command to view execution logs.