Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize JMeter Custom Log and Log Analysis

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to achieve JMeter custom log and log analysis, the article is very detailed, has a certain reference value, interested friends must read it!

1 JMeter log overview

JMeter, like the Java program, records event logs, and the log files are saved in the bin directory named jmeter.log. Of course, we can also view the log directly in the panel, click the yellow sign in the upper right corner to open the log panel, and click put again.

It can be seen that the log can help us to locate some problems that are not easy to detect directly.

In addition, JMeter can easily set the log output level:

2 JMeter Custom Log

All you saw before is the system log, which is the log printed by JMeter itself. What if we want to output some logs ourselves? This usually requires the help of Beanshell.

For example, the response of an interface is as follows:

Add the Beanshell assertion under the request, and after running, the corresponding content is output in the log:

Import org.apache.log4j.Logger;// gets the response data of the interface String result = prev.getResponseDataAsString (); if (result.contains ("error")) {Failure=true; log.error ("interface failure:" + result);}

Of course, the most important role of custom logs is when running scripts on the Linux server, because there is no interface, troubleshooting is more troublesome.

As a continuation of the previous article, there are some inconveniences in deploying JMeter scripts to Linux servers for stress testing:

The throughput statistics include all requests, including some secondary requests (beanshell requests), resulting in inaccurate true tps statistics.

Whether the business is successful or not, and the specific reasons for the failure are difficult to investigate.

First, the response content of the test interface is shown in the figure:

Add 2 [JSON Path Extractor] under the interface to extract code and orderId from the response content of the test interface, respectively.

Then add [BeanShell assertion] under the test interface:

Import org.apache.log4j.Logger;// get the response data of the interface String result = prev.getResponseDataAsString (); / / get code and orderIdString code = vars.get ("code") from the JSON extractor; String orderId= vars.get ("orderId"); if (code.equals ("0")) {log.info ("place order success, orderId=" + orderId);} else {Failure=true; log.error ("FailureMessage:" + result);}

Upload the script to Linux and write a startup script: start.sh

#! / bin/bashjmeter_log=/home/test/jmeter.logif [- f "$jmeter_log"]; then / / delete cp $jmeter_log / home/test/jmeter.log_back rm-rf $jmeter_logfi// after backing up the original log file to launch the JMeter script jmeter-n-t / home/test/test.jmx-l / home/test/result/test.jtl

After running the script, cat jmeter.log, the effect is as follows:

3 JMeter log analysis

Write a log analysis script logAnalysis.sh for the log:

#! / bin/bashjmeter_log=/home/test/jmeter.logthread_num= `grep 'Thread started' $jmeter_log | tail-n 1 | awk-F "-' {print$6} '`start_time= `grep' All thread groups have been started' $jmeter_log | awk-F"'{print$ 1} '`end_time= `grep' Shutdown hook ended' $jmeter_log | awk-F"'{print$ 1} '`end_time= `grep' Shutdown hook ended' $jmeter_log | awk-F " "'{print$1} '`final_success_time= `grep" place order success "$jmeter_log | tail-n 1 | awk-F"' {print$1,$2}'| awk-F" "'{print$1} '`success_running_time=$ [$(date-d" $final_success_time "+% s)-$(date-d" $start_time "+% s)] running_time=$ [$(date-d" $end_time "+% s)-$(date-d" $start_time "+% s)] cancle_times= `grep" cancle orders success "$jmeter_log | wc-l` / single success_times= `grep success $jmeter_log | wc-l` / / number of successes Failure_times= `grep FailureMessage $jmeter_log | wc-l`request _ times=$ [$success_times+$failure_times] error_rate= `echo "scale=2 $failure_times/$request_times*100 "| bc`qps = $[$request_times/$running_time] throughput=$ [$success_times/$success_running_time] echo-e' Threads:'$thread_numecho-e' requests:'$request_timesecho-e' successes:'$success_timesecho-e' failures:'$failure_timesecho-e' withdrawals:'$cancle_timesecho-e' error rate:'$error_rate'%'echo-e' start time:'$start_timeecho' -e 'end time:' $end_timeecho-e 'Last successful request time:' $final_success_timeecho-e 'request time:' $running_timeecho-e 'successful run time:' $success_running_timeecho-e 'throughput:' $throughput'/s'echo-e 'QPS:'$qps'/s'

When the JMeter script runs for a period of time, execute logAnalysis.sh. The effect is as follows:

Number of threads: 180

Number of requests: 131691

Number of successes: 131493

Number of failures: 198

Number of withdrawals: 141

Error rate: 0%

Start time: 2018-11-28 15:34:54

End time: 2018-11-28 15:37:17

Last successful request time: 2018-11-28 15:37:17

Request time: 143

Successful run time: 143

Throughput: 919prime s

QPS:920/s

As you can see, the output information is comprehensive and clear. In this way, we can get the details of the stress test in real time when we run the JMeter stress test script under linux.

The above is all the contents of the article "how to implement JMeter Custom Log and Log Analysis". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report