全网唯一一份jmeter5.x版本得插件,禁止转载。你能找到的全部都是4.x的插件。
抱残守缺。
写的不算很好,欢迎讨论。
参考文章地址:https://www.cnblogs.com/qgc1995/p/18267737
1.配置pom.xml
<?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>
<groupId>org.example</groupId>
<artifactId>rocketmqJmeter</artifactId>
<version>1.0-SNAPSHOT</version>
<name>rocketmqJmeter</name>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.14</version>
</dependency>
<!-- rcketmq依赖-服务端5.x-->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client-java</artifactId>
<version>5.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client-apis</artifactId>
<version>5.0.6</version>
</dependency>
<!-- rcketmq依赖-服务端4.x-->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>5.1.0</version>
</dependency>
<!-- jmeter依赖-->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>5.6.3</version>
</dependency>
<!--otel依赖-->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<version>1.42.1</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-boot-starter</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
<!-- 设置为连同依赖一起打包为一个jar包-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.name}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2、代码部分
package org.example;
import com.alibaba.fastjson.JSONObject;
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;
import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.message.Message;
import org.apache.rocketmq.client.apis.message.MessageBuilder;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.ProducerBuilder;
import org.apache.rocketmq.client.apis.producer.SendReceipt;
import org.apache.rocketmq.client.java.impl.producer.ProducerBuilderImpl;
import org.apache.rocketmq.client.java.message.MessageBuilderImpl;
import org.apache.rocketmq.remoting.protocol.heartbeat.ProducerData;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.UUID;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class JmeterMq5Producer extends AbstractJavaSamplerClient {
private String proxyAddr;
private String topic;
private String tags;
private String keys;
//private byte[] body;
private Producer producer;
private String vin;
// 给参数填充默认值
@Override
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("proxyAddr", "192.168.xx.xx:8081");
params.addArgument("topic", "testtopic");
params.addArgument("tags", "tags1");
params.addArgument("keys", "keys1");
return params;
}
// 随机字符串函数
public static String generateRandomString(int length_str) {
String CHAR_POOL = "abcdefghijklmnopqrstuvwxyz0123456789";
SecureRandom random = new SecureRandom();
StringBuilder sb = new StringBuilder(length_str);
for (int i = 0; i < length_str; i++) {
int index = random.nextInt(CHAR_POOL.length());
sb.append(CHAR_POOL.charAt(index));
}
return sb.toString();
}
// 定义producer方法
public static Producer my_producer(String proxyAddr) throws ClientException, IOException {
ClientServiceProvider provider = ClientServiceProvider.loadService();
// 定义provider
// String endpoints = "192.168.228.130:8081";
ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
.setEndpoints(proxyAddr)
.enableSsl(false)
.build();
// 定义producer
Producer producer = provider.newProducerBuilder()
.setClientConfiguration(clientConfiguration)
.build();
//producer.close();
return producer;
}
// 定义请求的前置处理
@Override
public void setupTest(JavaSamplerContext context) {
proxyAddr = context.getParameter("proxyAddr");
topic = context.getParameter("topic");
tags = context.getParameter("tags");
keys = context.getParameter("keys");
try {
producer = my_producer(proxyAddr);
} catch (ClientException | IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void teardownTest(JavaSamplerContext context) {
try {
producer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//这里是一个请求主体执行部分
@Override
public SampleResult runTest(JavaSamplerContext context) {
SampleResult sr = new SampleResult();
sr.sampleStart();
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
// 获取当前时间
LocalDateTime now = LocalDateTime.now();
// 使用定义的格式格式化当前时间
String formattedDateTime = now.format(formatter);
JSONObject json_messageBody = new JSONObject();
json_messageBody.put("data_base64", "CAESBHRlc3QYMg==");
json_messageBody.put("content-type", "application/x-protobuf");
json_messageBody.put("dataschema", "type.googleapis.com/testt");
json_messageBody.put("id", UUID.randomUUID().toString());
json_messageBody.put("ttl", 120000);
String messageBody = json_messageBody.toJSONString();
MessageBuilder messageBuilder = new MessageBuilderImpl();
Message message = messageBuilder.setTopic(topic)
.setBody(messageBody.getBytes(StandardCharsets.UTF_8))
.setTag(tags)
.setKeys(keys)
.addProperty("traceId", "00-" + generateRandomString(32) + "-" + generateRandomString(16) + "-01")
.build();
SendReceipt sendResult = producer.send(message);
// System.out.printf("%s%n", sendResult.getMessageId());
sr.setResponseData(sendResult.toString(), "utf-8");
if (sendResult.getMessageId()!= null) {
sr.setResponseData("{\"code\": \"200\", \"MsgId\" : \"" + sendResult.getMessageId() + "\"}", "utf-8");
} else {
System.err.println(sendResult);
sr.setResponseData("{\"code\" : \"500\", \"msg\": \"失败\", \"Error\": \"" + sendResult.toString() + "\"}", "utf-8");
}
} catch (Exception e) {
sr.setResponseData("{\"code\" : \"501\", \"msg\": \"其他失败\", \"Error\": " + e + "}", "utf-8");
try {
if (producer != null) {
producer.close();
}
} catch (IOException ex) {
throw new RuntimeException(ex + "jxh");
}
}
try {
sr.setDataType(SampleResult.TEXT);
sr.setSuccessful(true);
} catch (Exception e) {
sr.setSuccessful(false);
e.printStackTrace();
} finally {
sr.sampleEnd();
}
return sr;
}
}
3、maven打包
4、放到apache-jmeter-5.6.3\lib\ext目录下
5、启动jmeter.bat