Develop performance testing platform based on JMeter API

JMeter is a powerful performance testing tool. If you develop a performance testing platform, it is appropriate to use it as the underlying execution engine. If you want to use its API, you have to have a clear understanding of the entire execution process and common classes of JMeter.

Explanation of commonly used JMeter classes and functions:

  1. TestPlan Class: Represents a test plan, which is the top-level element of performance testing. You can use it to set global test properties such as test name, thread group, listeners, etc.

  2. ThreadGroup Class: Represents a thread group, which defines the number of concurrently executing threads, startup delay, number of loops, etc. The thread group is the basic unit of performance testing, and all threads are executed in the thread group.

  3. LoopController Class: Represents a loop controller, which defines the number of loops or conditions. Loop controllers can be added to thread groups to execute multiple loops per thread.

  4. HTTPSampler Class: Represents an HTTP request sampler, used to send HTTP requests. You can set the requested URL, method (GET, POST, etc.), request parameters, request headers, etc.

  5. StandardJMeterEngine Class: Represents the JMeter engine responsible for configuring and executing the test plan. Performance tests can be started and executed by configuring a test plan to the engine.

  6. JMeterUtils Class: Provides some utility methods and properties of JMeter. Correct operation of the JMeter API can be ensured by loading the JMeter properties file and initializing local settings.

The functions and methods of these classes are what we must be familiar with in order to be able to create and configure test plans, thread groups, loop controllers, and other elements in code, as well as perform performance tests. After mastering these classes, how to use jmeter to write performance test

Let's review what the test script looks like:

Schematic diagram of the general flow of JMeter writing performance test scripts:

创建测试计划(Test Plan) --> 添加线程组(Thread Group) --> 添加配置元件(Config Elements)--> 添加逻辑控制器(Logic Controllers)

--> 添加取样器(Samplers)和断言(Assertions)--> 添加监听器(Listeners)--> 运行测试计划

Source code implementation method:

(1) Environment initialization

需要在项目中引入 上述JMeter核心API,这样我们才能使用 JMeter 提供的各种功能和类

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.util.JMeterUtils;

(2) Environment initialization

During the execution of the JMeter API, it will first read the properties in the configuration file under the JMeter software installation directory file, so we need to read the specified directory of the JMeter main configuration file and the JMeter installation directory through the JMeter API.

The environment initialization mainly includes the following two steps:

  1. Load the JMeter main configuration file JMeter.properties through JMeterUtils.loadJMeterProperties, and then assign all properties in jmeter.properties to the JMeterUtils object to obtain the required configuration later;
  2. Set the installation directory of JMeter, and the JMeter API will load the configuration required when the script runs according to the directory we specified

1

2

JMeterUtils.loadJMeterProperties("jmeter.properties");JMeterUtils.setJMeterHome(~/jemter)

JMeterUtils.initLocale();

(3) Create a test plan

Create a test plan using  TestPlan the class and set related properties.

1

2

3

TestPlan testPlan = new TestPlan("My Test Plan");

testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());

testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());

(4) Create ThreadGroups

Create a thread group using  ThreadGroup a class, and set related properties, such as the number of threads, the number of loops, etc.

1

2

3

4

5

ThreadGroup threadGroup = new ThreadGroup();

threadGroup.setName("My Thread Group");

threadGroup.setNumThreads(10);

threadGroup.setRampUp(5);

threadGroup.setSamplerController(loopController);

(5) Create a loop controller

This step is optional. In the actual testing process, we create a loop controller to simulate the behavior of a user performing the same operation multiple times. If we do not create a loop controller, the default is to perform only one operation. The code for loop controller creation is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

LoopController loopController = new LoopController();

//设置循环次数,1 代表循环 1 次

loopController.setLoops(1);

loopController.setFirst(true);

loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());

loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());

loopController.initialize()

(6) Create Sampler

Use  HTTPSampler the class to create an HTTP request sampler and set the URL, method and other properties of the request

1

2

3

4

5

HTTPSampler httpSampler = new HTTPSampler();

httpSampler.setDomain("example.com");

httpSampler.setPort(80);

httpSampler.setPath("/api/endpoint");

httpSampler.setMethod("GET");

(7) Create a result collector

The result collector can save the relevant data of the result after each Sampler operation is completed, for example, the status returned by each interface request, and the data responded by the server.

1

2

3

ResultCollector resultCollector = new ResultCollector();

resultCollector.setName(ResultCollector.class.getName());

  

(8) Build tree and generate jmx script

The above steps are actually preparations for creating a HashTree node. Add the creation to the child HashTree node, then add the child HashTree to the testplan, and finally add the tesplan node to the constructed parent HashTree node, thus generating our script executable file jmx. Substitute HashTree subTree = new HashTree(

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

    subTree.add(httpSamplerProxy);

    subTree.add(loopController);

    subTree.add(threadGroup);

    subTree.add(resultCollector);

    HashTree tree = new HashTree();

    tree.add(testPlan,subTree);

    SaveService.saveTree(tree, new FileOutputStream("test.jmx"));

catch (IOException e) {

    e.printStackTrace();

}

通过以上代码就可以创建出 JMeter 可识别的 HashTree 结构,并且可以通过 saveTree 保存为 test.jmx 文件。如果是现成的jmx文件,可直接通过 HashTree jmxTree =

SaveService.loadTree(file); loadTree 会把 jmx 文件转成内存对象,并返回内存对象中生成的 HashTree。

(9) 测试执行

通过脚本文件的执行(测试执行),我们便可以开始对服务器发起请求,进行性能测试。测试执行主要包括 2 个步骤:

  1. 把可执行的测试文件加载到 StandardJMeterEngine;
  2. 通过 StandardJMeterEngine 的 run 方法执行,便实现了 Runable 的接口,其中 engine.run 执行的便是线程的 run 方法

1

2

3

4

5

StandardJMeterEngine engine = new StandardJMeterEngine();

engine.configure(jmxTree);

engine.run();

(10) 结果收集

  JMeter API 提供了一个结果收集器(ResultCollector),用于收集和处理性能测试结果。它是 SampleListener 接口的一个实现类,可以监听每个测试样本的事件,并对测试结果进行

处理。我们可以重写 sampleOccurred 方法来收集每次 loop 的结果。该方法的参数 SampleEvent 中包含相应的测试结果。然后把测试结果数据存到消息队列里面,比如kafka ,后端获取存

储的实时采集数据,进行相应的计算,生成的数据返回给前端进行绘制展示。

最后: 为了回馈铁杆粉丝们,我给大家整理了完整的软件测试视频学习教程,朋友们如果需要可以自行免费领取【保证100%免费】

在这里插入图片描述

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

在这里插入图片描述

在这里插入图片描述

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/132186782