Last class, the comic boy showed you the advanced usage of JMeter in the form of Comics: how to build InfluxDB and use the cooler Grafana.
Seeing many young friends feel that they are not satisfied with reading, under the strong urging, the new article of the young man comes out again. What kind of surprise did the boy bring us this time? Keep looking!
How to do pressure measurement for different concurrent?
If you manually pressurize step by step, you not only need to change the concurrent number of human flesh, but also need to spend a lot of time waiting for completion, which can be summarized in one word: annoying! Therefore, make a good strategy to make the program pressurize and wait automatically; The most perfect solution is to look at the pressure measurement monitoring records or sit down and receive the report after completion.
Execute JMeter without UI
jmeter -n -t <testplan filename> -l <listener filename>
Example: JMeter - N - t testplan jmx -l test. jtl
Example meaning: it means running testplan in command line mode JMX file, and the output log file is test jtl
The specific parameter information is as follows: - h help - > print out useful information and exit
-n non GUI mode - > run JMeter in non GUI mode
-t test file - > JMeter test script file to run
-l log file - > file recording results
-r remote execution - > in jmter All remote servers specified in the properties file
-H proxy host - > set the proxy host used by JMeter
-P proxy port - > set the port number of the proxy host used by JMeter
We set up and record the script, and we can choose to save it at this time. Then we get a jmx file, so we can load this file with the above command to realize UI free operation!
In the shell command, we use nohup to run jmeter:
nohup jmeter -n -t ${jmx_filename} -l ${jtl_filename} &
-
Nohup is the abbreviation of no hang up, which means don't hang up. If you are running a process and you think it will not end when you exit the account, you can use the nohup command. This command can continue to run the corresponding process after you exit the account / close the terminal. By default, all output from this job is redirected to a named nohup Out file.
-
&It refers to running in the background. Note that nohup does not run in the background& It is running in the background. Then, we can skillfully combine them with nohup Command &, so that the command can be executed in the background permanently.
Yes, the rabbit is right. Like the following picture, the parameter information we configured in the UI interface of jmeter is all in the jmx file. For example, the concurrency number is 11
Demonstrate with project practice
It's not easy to explain a piece of code. Please allow words here. The following is the shell code, which will be explained with emphasis. If you are unfamiliar with the syntax, you can search online by yourself.
github project address: https://github.com/princeqjzh/iJmeter/blob/master/automation/auto_stress_test.sh
Before running the code, you need to set the jmeter directory as the environment variable so that the shell can know where the running jmeter is.
export jmx_template="PreClassMenu_auto" export suffix=".jmx" export jmx_template_filename="${jmx_template}${suffix}" export os_type=`uname`
First, use export to define several variables, which will be used later, including jmx_ template_ The filename variable stores the temporary name of jmx. In actual work, multiple concurrent numbers should be set, such as 10, 20, 30 and 40. Each concurrent number should have a file corresponding to it, which is called temporary file. The concurrent numbers of these four jmx files are 10, 20, 30 and 40 respectively.
Let's move on:
#!/usr/bin/env bash export jmx_template="PreClassMenu_auto" export suffix=".jmx" export jmx_template_filename="${jmx_template}${suffix}" export os_type=`uname` # You need to define the location of the jmeter root directory in the system variable, as follows # export jmeter_path="/your jmeter path/" # Empty nohup out cat /dev/null > nohup.out # Force kill JMeter process killJMeter() { pid=`ps -ef|grep jmeter|grep java|awk '{print $2}'` echo "jmeter Id list :$pid" if [[ "$pid" = "" ]] then echo "no jmeter pid alive" else kill -9 $pid fi }
Encountered the cat command and killJMeter function. I'll talk about these two parts later and continue to look down.
#!/usr/bin/env bash export jmx_template="PreClassMenu_auto" export suffix=".jmx" export jmx_template_filename="${jmx_template}${suffix}" export os_type=`uname` # You need to define the location of the jmeter root directory in the system variable, as follows # export jmeter_path="/your jmeter path/" # Empty nohup out cat /dev/null > nohup.out # Force kill JMeter process killJMeter() { pid=`ps -ef|grep jmeter|grep java|awk '{print $2}'` echo "jmeter Id list :$pid" if [[ "$pid" = "" ]] then echo "no jmeter pid alive" else kill -9 $pid fi } thread_number_array=(10 20 30 40 50)
An array thread is defined here_ number_ Array, this array, let's guess what it means. Is the concurrent number. The code expects five concurrent numbers: 10, 20, 30, 40 and 50. The next step is very important, please pay attention!
#!/usr/bin/env bash export jmx_template="PreClassMenu_auto" export suffix=".jmx" export jmx_template_filename="${jmx_template}${suffix}" export os_type=`uname` # You need to define the location of the jmeter root directory in the system variable, as follows # export jmeter_path="/your jmeter path/" # Empty nohup out cat /dev/null > nohup.out # Force kill JMeter process killJMeter() { pid=`ps -ef|grep jmeter|grep java|awk '{print $2}'` echo "jmeter Id list :$pid" if [[ "$pid" = "" ]] then echo "no jmeter pid alive" else kill -9 $pid fi } thread_number_array=(10 20 30 40 50) for num in "${thread_number_array[@]}" do # Generate jmx file corresponding to pressure measurement thread export jmx_filename="${jmx_template}_${num}${suffix}" export jtl_filename="test_${num}.jtl" rm -f ${jmx_filename} ${jtl_filename} cp ${jmx_template_filename} ${jmx_filename} echo "generate jmx Pressure test script ${jmx_filename}" if [[ "${os_type}" == "Darwin" ]]; then sed -i "" "s/thread_num/${num}/g" ${jmx_filename} else sed -i "s/thread_num/${num}/g" ${jmx_filename} fi # JMeter silent pressure measurement nohup ${jmeter_path}/bin/jmeter -n -t ${jmx_filename} -l ${jtl_filename} & sleep 65 killJMeter rm -f ${jmx_filename} done echo "All automatic pressure tests are completed"
This is the whole code. You can refer to the following explanation:
-
for: we are interested in thread_ number_ The array is traversed in turn. The content between do and done is the operation to be performed each time.
-
jmx_filename and jtl_filename: These are two variables, jmx_filename is the name of the temporary JMX file, and ${} represents the variable. For example, ${jmx_template} refers to JMX_ Template is a variable whose name is similar to PreClassMenu_auto_10.jmx,PreClassMenu_auto_20.jmx,PreClassMenu_auto_30.jmx, etc.
-
fm -f: used to clear miscellaneous files. If there are current directory files and jmx_filename and JTL_ If the filename has the same name, delete it.
-
cp: Copy command, which copies the contents of the template into a temporary file. It can also be understood as using the template to create a temporary file.
-
if: it is used to judge whether it is a mac computer. Input uname on the mac computer to display Darwin. if it is not displayed, it is linux. The reason for judging is that the following sed command is slightly different between mac and linux.
-
sed: This is our play. It is used for regular replacement. We use the variable num to replace the thread in the temporary file_ Num, what is it? Please see the screenshot below:
WOW! Use thread for concurrent number_ Num is replaced. The reason for this is to make it easier for regular to find it. Imagine that if I use the number 1 or 11, it will be difficult for regular to locate it. Using regular to replace num, you can modify the concurrent number of temporary files with commands. What a great operation!
-
Nohup: this command has been described earlier, but I won't say more. It runs jmeter without UI. By default, all the output of the job is redirected to a job named nohup In the out file, the cat at the beginning of the code is to empty nohup Out file. In other words, every time you run a shell script, you empty nohup Out record file, which is equivalent to clearing the log.
-
sleep and killjmeter: the combination of these two commands means: kill the pressure measurement process after 65 seconds, because sometimes JMeter cannot exit normally! Killjmeter is a function defined at the beginning. It filters out processes and kills them through grep and awk, but I'll explain more.
Finally, execute the shell script with sh command, and you will get the following results:
Write at the end
This article shows you the advanced usage of jmeter in the form of cartoon: how to run jmeter automatically. Students, don't you know what you've learned? Please practice hard and try to learn more knowledge.
This content is from Hogwarts Testing Institute. What will be interesting and informative articles next? Please continue to follow us.
-Interactive today -****
PS: the winning list of teachers' Day is next! Thank you very much for your participation. Who won the prize? Only by clicking on it can we know. I promise it's a surprise. Hogwarts testing institute has zero tolerance for brush praise and trumpet.
- Hogwarts Testing Institute
Previous recommendation
**Big talk JMeter3 | how to use tools to get high appearance performance monitoring reports
**
Big talk JMeter2 | how to pass get parameters and how to use HTTP correctly
Talk big about jmeter and let you know the basic usage of jmeter
Transformation exploration of test engineers: how to make product quality better?
**
**
**Welcome to leave a message at the bottom of the article and share it with other test partners~
**
(if you have any questions, please chat with jasmine's assistant in private)
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Click for more information