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
1 | export PATH="$PATH:/opt/apache-jmeter-5.4.1/bin" |
- This way we can execute
jmetercommand 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 GroupunderTest Plan, hoverAdd, then hoverConfig Element, clickHTTP Request Default, setServer Name or IPas${__P(HOST)},Port Numberas${__P(PORT)}
- Right click
Thread Group, hoverAdd, then hoverSample, clickHTTP Request, setNameasFrontend,Pathas/
- Right click
Thread Group, hoverAdd, then hoverSample, clickHTTP Request, setNameasBackend,Pathas/api/currentUser
- Right click
Thread Group, hoverAdd, then hoverAssertion, clickResponse Assertion, setField to TestasResponse Code,Patten Matching RulesasEquals, clickAddin the bottom, input200
__Pis 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, setNameasLogin,Server Name or IPas${__P(HOST)},Port Numberas${__P(PORT)}, Method ofHTTP RequestasPOST,Pathas/api/login/account, clickAddin the bottom, addNameasusernameandpassword,Valueas${__P(USERNAME)}and${__P(PASSWORD)}respectively
- Right click
setUp Thread Group, hoverAdd, then hoverPost Processors, clickRegular Expression Extractor, setName of created variableastoken,Regular Expressionas"token":"(.+?)", Template as$1$
- Right click
setUp Thread Group, hoverAdd, then hoverSample, clickBeanShellSampler, setNameasToken,Scriptas${__setProperty(token, ${token})}
- Right click
Thread Group, hoverAdd, then hoverConfig Element, clickHTTP Cookie Manager, clickAddin the bottom, setNameastoken,Valueas${__property(token)},Domainas${__P(HOST)}:${__P(PORT)},Pathas/
"token":"(.+?)"capture value oftokenin the return result as$1$__setPropertyis function for JMeter to set global variable__propertyis 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
reportdirectory must exists, so that the report can be written, somkdirneed 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,-eand-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
1 | export LOCAL_HOST=127.0.0.1 |
Invoke Variable
1 | #!/bin/sh |
$(echo $VAR | tr 'a-z' 'A-Z')indicates uppercase for all, same as${VAR^^}forBashwhich above version 4.0- If
reportdirectory exists, we don’t need to recreate it again, so we determine with-dfirstly ${!HOST}means the value of$HOSTis 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