JMeter Advanced - Detailed explanation of Java request on transaction operations (with source code)

 

Jmeter transaction operation

I wrote an article before, introducing how Jmeter calls java requests, you can refer to the article  to explain the design method of JMeter Java requests in detail

But there is a problem in this article. In the method public SampleResult runTest(JavaSamplerContext arg0), only one API of one class is called. If the requirement is to call multiple APIs of multiple classes in jmeter, how to achieve it? Students will say, simple, we create multiple classes and inherit AbstractJavaSamplerClient respectively! Of course it is possible to do this, but it will create too many classes. Jmeter provides a method to implement transaction operations, that is, create a transaction in a runTest method, and then include multiple requests in a transaction. Without further ado, go directly to the code

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public class DemoTransaction extends AbstractJavaSamplerClient {
private SampleResult sr;

public static final String PARAM_PARA1 = "para1";
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("para1", "");
return params;
}


public SampleResult runTest(JavaSamplerContext arg0) {

/**创建事务demo**/
this.sr = new SampleResult();
this.sr.setSampleLabel("demo ");
this.sr.sampleStart();

/**创建事务demo中的第一个子请求**/
SampleResult result1= new SampleResult();
result1.setSampleLabel("demo1");
result1.sampleStart();

/**写子请求的具体逻辑,这里用等待时间代替**/
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**写子请求的具体逻辑,这里用等待时间代替**/

this.sr.addSubResult(result1,false); //重点把子请求的测试结果放到事务中
result1.sampleEnd(); //子请求1结束


/**创建事务demo中的第二个子请求**/
SampleResult result2= new SampleResult();
result2.setSampleLabel("demo2");
result2.sampleStart();
/**写子请求的具体逻辑,这里用等待时间代替**/
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**写子请求的具体逻辑,这里用等待时间代替**/

result2.setSuccessful(true);
this.sr.addSubResult(result2,false); //重点把子请求的测试结果放到事务中
result2.sampleEnd(); //子请求2结束

/**判断子请求是否都成功执行,如果其中一个失败,则认定事务执行失败**/

if( result1.isSuccessful() & result2.isSuccessful())
{
this.sr.setSuccessful(true);

}else {
this.sr.setSuccessful(false);

}
/**判断子请求是否都成功执行,如果其中一个失败,则认定事务执行失败**/

this.sr.sampleEnd(); //事务结束

return this.sr;
}

public void setupTest(JavaSamplerContext context) {
super.setupTest(context);

}

public void teardownTest(JavaSamplerContext context) {
super.teardownTest(context);
}
}

Using JMeter 5.x API

What needs to be emphasized here is the method addSubResult(result2, false ) This method is only provided in jmeter 5.x (addSubResult(result2) in 4.x), the first parameter is the incoming result value, the second parameter Whether to change the name of the child thread, the maven configuration of jmeter 5.x is as follows,

<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.5</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>5.5</version>
</dependency>

Use this method to achieve - display the response time and processing capacity of sub-requests in the transaction in the aggregate report

Package the java file into the \lib\ext directory, then add java sampler, select the class DemoTransaction, and run the test. The results are as follows:

In the view result tree, you can see that the transaction demo includes two subrequests demo1 and demo2

 

In the aggregation report, there is only the overall information of the demo. We can see that demo1 and demo2 wait for 1s and 0.5s respectively in the example, and the Average in the figure below is 1500 milliseconds, which proves that the response time of the transaction is equal to the sum of the two requests

Test report optimization plan

The above aggregate report is flawed, it does not list the response time and processing power of each subrequest! At present, I have not solved this problem through coding, but we can achieve it in other ways, that is, during the test, write the test results to a file, and then open the file after the test is over!

 emphasize again! To achieve this function, you must use ApacheJMeter_java and ApacheJMeter_core of jmeter 5.x. I personally tested that this function cannot be realized in jmeter 4.x!

Guess you like

Origin blog.csdn.net/liwenxiang629/article/details/130480937