后端工具JMeter TCP压测 & 动态报文
Jint
Apache JMeter 可用于模拟服务器、服务器组、网络或对象上的重负载,以测试其强度或分析不同负载类型下的整体性能。
本文章基于Apache JMeter 5.1.1版本,首先介绍其基本使用,而后通过内部FileToString函数和自定义函数分别演示如何在压测时使用自定义报文。
TCP压测
1.创建线程组
选中位于左侧TestPlan,依次点击右键,添加, 线程(用户),线程组即可创建成功。我设置的线程属性如下:

2. 增加TCP取样器
选中Thread Group依次右键添加,取样器,TCP取样器。

TCP取样器有三种发送方式:
- TCPClientImpl:文本数据,默认。
- BinaryTCPClientImpl:传输16进制数据,指定EOL结束符。
- LengthPrefixedBinaryTCPClientImpl:数据包的前2个字节为数据长度。
若要修改为传输16进制数据,应修改Jmeter下bin/jmeter.properties配置文件,并重启Jmeter。

3. 开始压测
输入要发送的16进制数据,点击左侧绿色开始按钮进行压测,右侧会显示本地请求的线程数。记得清除测试记录,以免影响到后续的测试结果。

注意:16进制数据之间不能有空格或换行符。
4. 查看压测结果
选中TCP取样器,右键依次点击添加,监听器,查看结果树。
选中TCP取样器,右键依次点击添加,监听器,汇总报告。
动态报文
我们可以使用Jmeter内部函数(FileToString)或自定义函数这两种方式,实现动态报文内容的发送。
FileToString函数
此函数能够按行读取文件内容,导航栏的函数助手可以帮助我们了解函数详情。如下图所示
- 函数助手。
- 选择FileToString函数。
- 输入对应参数(有可选、必选参数)。
- 点击生成按钮,将会拷贝函数,并展示预期效果。
- 预期效果。

在拷贝到函数后,尝试发送动态报文,操作如下图:

自定义函数
现在演示的是,使用java编写自定义函数,并打成 fat jar 导入到 Jmeter,以此来实现动态报文,具体操作如下:
1. 建立一个maven项目,导入依赖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<dependencies> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_core</artifactId> <version>5.4.1</version> </dependency> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_java</artifactId> <version>5.4.1</version> </dependency> </dependencies>
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>cn.vsp.TestMain</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
|
2. 扩展 AbstractFunction 类
创建子类RandomFunction用于生成随机数,注意最后一个子包必须是functions。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.example.functions;
import org.apache.jmeter.engine.util.CompoundVariable; import org.apache.jmeter.functions.AbstractFunction; import org.apache.jmeter.functions.InvalidVariableException; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.samplers.Sampler;
import java.util.Collection; import java.util.List; import java.util.concurrent.ThreadLocalRandom;
public class RandomFunction extends AbstractFunction {
@Override public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException { return String.valueOf(ThreadLocalRandom.current().nextInt(10)); }
@Override public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {}
@Override public String getReferenceKey() { return "__test"; }
@Override public List<String> getArgumentDesc() { return null; } }
|
3. 导入Jmeter
将maven项目打成jar胖包,放置到 jmeter/lib/ext目录下。最后重启Jmeter,打开函数助手就能看到自定义函数。
