The settings of JMeter performance test for different environment are basically consistent, just with differences of address, account, an so on, Here I utilize Shell Script and JMeter variable to handle uniformly
Install JMeter
wget https://mirror.cogentco.com/pub/apache//jmeter/binaries/apache-jmeter-5.4.1.tgz
tar -zxvf apache-jmeter-5.4.1.tgz -C /opt
Add bin directory to PATH
export PATH="$PATH:/opt/apache-jmeter-5.4.1/bin"
- This way we can execute
jmeter
command from anywhere
Config Test Plan
Run JMeter GUI
jmeter
Normal Setting
- Right click
Test Plan
, hoverAdd
, then hoveThreads(User)
, clickThread Group
, set appropriate value forNumber of Threads (users)
- Right click the newly added
Thread Group
underTest Plan
, hoverAdd
, then hoverConfig Element
, clickHTTP Request Default
, setServer Name or IP
as${__P(HOST)}
,Port Number
as${__P(PORT)}
- Right click
Thread Group
, hoverAdd
, then hoverSample
, clickHTTP Request
, setName
asFrontend
,Path
as/
- Right click
Thread Group
, hoverAdd
, then hoverSample
, clickHTTP Request
, setName
asBackend
,Path
as/api/currentUser
- Right click
Thread Group
, hoverAdd
, then hoverAssertion
, clickResponse Assertion
, setField to Test
asResponse Code
,Patten Matching Rules
asEquals
, clickAdd
in the bottom, input200
-
__P
is function for JMeter to get the command line parameter
Handle Authorization
The above backend api can only be access after login, the authorized method is token in the cookies or Authorization Header, so we need to get this token firstly, and append to every request
- Right click
Test Plan
, hoverAdd
, then hoverThreads(User)
, clicksetUp Thread Group
- Right click
setUp Thread Group
, hoverAdd
, then hoverSample
, clickHTTP Request
, setName
asLogin
,Server Name or IP
as${__P(HOST)}
,Port Number
as${__P(PORT)}
, Method ofHTTP Request
asPOST
,Path
as/api/login/account
, clickAdd
in the bottom, addName
asusername
andpassword
,Value
as${__P(USERNAME)}
and${__P(PASSWORD)}
respectively - Right click
setUp Thread Group
, hoverAdd
, then hoverPost Processors
, clickRegular Expression Extractor
, setName of created variable
astoken
,Regular Expression
as"token":"(.+?)"
, Template as$1$
- Right click
setUp Thread Group
, hoverAdd
, then hoverSample
, clickBeanShellSampler
, setName
asToken
,Script
as${__setProperty(token, ${token})}
- Right click
Thread Group
, hoverAdd
, then hoverConfig Element
, clickHTTP Cookie Manager
, clickAdd
in the bottom, setName
astoken
,Value
as${__property(token)}
,Domain
as${__P(HOST)}:${__P(PORT)}
,Path
as/
-
"token":"(.+?)"
capture value oftoken
in the return result as$1$
-
__setProperty
is function for JMeter to set global variable -
__property
is function for JMeter to get global variable
Run Test Plan
Run Directly
The above plan config set information of address and account as variable, which can be transmitted in the command line, such as:
mkdir report
HEAP="-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m"
jmeter -n -t test.jmx \
-J HOST=127.0.0.1 \
-J PORT=80 \
-J USERNAME=USERNAME \
-J PASSWORD=PASSWORD \
-l log/local-20210620095700.txt \
-e -o report/local-20210620095700
-
report
directory must exists, so that the report can be written, somkdir
need to be used to create it for the first time - The above command line make use of jmeter command line options
-n
,-t
,-J
,-l
,-e
and-o
- The html formatted test report in local-20210620095700, is composed by table data and visualized chart, as the following images shown:
Handle by Shell
The above commands are verbose, and we need to set every parameter for different environment, so I utilize Shell Script to simplify
Config Variable
export LOCAL_HOST=127.0.0.1
export LOCAL_PORT=80
export LOCAL_USERNAME=USERNAME
export LOCAL_PASSWORD=PASSWORD
export TESTING_HOST=192.168.1.10
export TESTING_PORT=80
export TESTING_USERNAME=USERNAME
export TESTING_PASSWORD=PASSWORD
export PRODUCT_HOST=192.168.1.100
export PRODUCT_PORT=80
export PRODUCT_USERNAME=USERNAME
export PRODUCT_PASSWORD=PASSWORD
Invoke Variable
#!/bin/sh
time=$(date "+%Y%m%d%H%M%S")
server=${1:-"local"}
prefix=$(echo $server | tr 'a-z' 'A-Z')_
HOST=${prefix}HOST
PORT=${prefix}PORT
USERNAME=${prefix}USERNAME
PASSWORD=${prefix}PASSWORD
if ! [ -d "report" ]
then
mkdir report
fi
HEAP="-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m"
jmeter -n -t test.jmx \
-J HOST=${!HOST} \
-J PORT=${!PORT} \
-J USERNAME=${!USERNAME} \
-J PASSWORD=${!PASSWORD} \
-l log/$server-$time.txt \
-e -o report/$server-$time
-
$(echo $VAR | tr 'a-z' 'A-Z')
indicates uppercase for all, same as${VAR^^}
forBash
which above version 4.0 - If
report
directory exists, we don't need to recreate it again, so we determine with-d
firstly -
${!HOST}
means the value of$HOST
is the name the other variable, and the value of this other variable will be resolved
Run Script
Local Environment
./test.sh
Testing Environment
./test.sh testing
Product Environment
./test.sh product
- if the environment parameter is omitted,
${1:-"local"}
will set the default environment aslocal
Summarize
The above plan and script can be downloaded via this GitHub Repository, which fulfills the following:
- Config test plan dynamically for JMeter through the reception of parameter inputted by command line
- Utilize setup thread group to handle login, and set token as global variable
- Simplify command and handle uniformly for different environment with Shell Script
Top comments (0)