目标:支持MQTT消息的压测
参考:GitHub - emqx/mqtt-jmeter: MQTT JMeter Plugin
1、工程pom依赖
<properties>
<jmeter-version>5.4.3</jmeter-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>${jmeter-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>${jmeter-version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
2、设置采样器
指定IP、host、场景
public class SamplerUI extends AbstractSamplerGui{
private CommonConnUI connUI = new CommonConnUI();
public SamplerUI() {
this.init();
}
/**设置面板样式*/
private void init() {
setLayout(new BorderLayout());
setBorder(makeBorder());
add(makeTitlePanel(), BorderLayout.NORTH);
JPanel mainPanel = new VerticalPanel();
add(mainPanel, BorderLayout.CENTER);
mainPanel.add(connUI.createConnPanel());
mainPanel.add(connUI.createUserPanel());
}
@Override
public void configure(TestElement element) {
super.configure(element);
Sampler sampler = (Sampler)element;
connUI.configure(sampler);
}
@Override
public TestElement createTestElement() {
Sampler sampler = new Sampler();
this.configureTestElement(sampler);
connUI.setupSamplerProperties(sampler);
return sampler;
}
@Override
public String getLabelResource() {
throw new RuntimeException();
}
@Override
public String getStaticLabel() {
return "新增测试";
}
@Override
public void modifyTestElement(TestElement arg0) {
Sampler sampler = (Sampler)arg0;
this.configureTestElement(sampler);
connUI.setupSamplerProperties(sampler);
}
@Override
public void clearGui() {
super.clearGui();
connUI.clearUI();
}
}
public class CommonConnUI{
public JPanel createUserPanel() {
JPanel con = new HorizontalPanel();
JPanel connPanel = new HorizontalPanel();
connPanel.setBorder
(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(), "设置"));
connPanel.add(onlineNum);
connPanel.add(upStream);
con.add(connPanel);
return con;
}
}
3、设置触发
import org.apache.jmeter.samplers.AbstractSampler;
public class Sampler extends AbstractSampler {
@Override
public SampleResult sample(Entry arg0){
init();
SampleResult result = new SampleResult();
result.setSampleLabel(getName());
JMeterVariables vars = JMeterContextService.getContext().getVariables();
res.sampleStart();
}
}
4、增加结果监听器
public class Result extends AbstractTestElement implements SampleListener {
}
监听收消息成功率
监听发消息成功率
监听发已读消息成功率
监听发消息的大小
5、Linux机器执行jmeter脚本
1)提前准备
●Jmeter压缩包
●要压测的jmx文件
2)Linux部署jmeter
●用FinalShell工具将文件传输到Linux机器上
●安装JDK(如无)【sudo apt-get update sudo apt-get install openjdk-17-jdk】
●解压Jmeter:【unzip apache-jmeter-5.5.zip】
●配置环境变量,在/etc/profile文件中增加配置:【PATH=$PATH:$JAVA_HOME/bin:/root/jmeter-test/apache-jmeter-5.5/bin】
●生效环境变量:【source /etc/profile】
●确认jmeter有效【jmeter -v】
3)执行
●执行脚本【jmeter -n -t /path.jmx -l log.jtl】
●执行结果查看:将jtl文件拉取到本地,导入jmeter中进行展示
4)其他问题
如需要查看执行结果,而jtl文件中只有表头,没有内容,可修改 /bin路径下 user.properties和jmeter.properties 配置。
user.properties修改如下
jmeter.save.saveservice.response_data=true
jmeter.save.saveservice.samplerData=true
jmeter.properties修改如下
jmeter.save.saveservice.output_format=xml
jmeter.save.saveservice.response_data=true
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.requestHeaders=true
jmeter.save.saveservice.url=true
jmeter.save.saveservice.responseHeaders=true