Jmeter模拟RocketMQ生产者消息发送

实现方案

jmeter 压测工具, 可以支持java请求。

java手写一个rocketmq程序,注意这里需要实现jmeter的JavaSamplerClient

java项目

建议使用maven普通项目 (springboot 测试中发现无法被jmeter识别)

 pom



    4.0.0

    org.example
    jMeterTest
    1.0-SNAPSHOT

    
        8
        8
    

    
        
            org.rocketmq.spring.boot
            rocketmq-spring-boot-starter
            1.0.0.RELEASE
        

        
            org.apache.jmeter
            ApacheJMeter_java
            3.1
        
    

RocketMqProducer

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;

public class RocketMqProducer implements JavaSamplerClient {
    String produceGroup = "ProducerGroupName_1";
    String topic = "diagnosisEqtInfoDataPushT";
    String tag = "ccc";
    String namesrvAddr = "192.168.13.13:9876";
    String message = "{\n" +
            "    \"manufacturerName\": \"ILINECN\",\n" +
            "    \"operateTag\": \"INSERT\",\n" +
            "    \"deviceId\": 11\n" +
            "}";

    @Override
    public void setupTest(JavaSamplerContext arg0) {
        namesrvAddr = arg0.getParameter("namesrvAddr");
        topic = arg0.getParameter( "topic");
        tag = arg0.getParameter( "tag");
        message = arg0.getParameter("message");
        produceGroup = arg0.getParameter("produceGroup");
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sr = new SampleResult();
        sr.setSampleLabel("RocketMQ测试");
        try {
            sr.sampleStart();
            DefaultMQProducer producer = new DefaultMQProducer(produceGroup);
            producer.setNamesrvAddr(namesrvAddr);
            producer.setInstanceName("ProducerByCC");
            producer.setVipChannelEnabled(false);

            producer.start();// once
            Message msg = new Message(topic,
                    tag,
                    "WangXiaoRui",
                    message.getBytes());
            try {
                {
                    SendResult sendResult = producer.send(msg);
                    if(sendResult ==null || sendResult.getSendStatus() != SendStatus.SEND_OK){
                        System.err.println(sendResult);
                    }
                }

            }catch (Exception e){
                e.printStackTrace();;
            }
            producer.shutdown();

            sr.setResponseData("success","utf-8");
            sr.setDataType(SampleResult.TEXT);
            sr.setSuccessful(true);
        }catch(Exception e){
            sr.setSuccessful(false);
//            sr.setResponseData(e.getMessage(),"utf-8");
            e.printStackTrace();
        }
        finally {
            sr.sampleEnd();
        }


        return sr;

    }

    @Override
    public void teardownTest(JavaSamplerContext javaSamplerContext) {

    }

    @Override
    public Arguments getDefaultParameters() {
        Arguments args = new Arguments();
        args.addArgument("namesrvAddr", "192.168.13.13:9876");

        args.addArgument("topic", "diagnosisEqtInfoDataPushT");
        args.addArgument("tag", "ccc");
        args.addArgument("message", "{\n" +
                "    \"manufacturerName\": \"ILINECN\",\n" +
                "    \"operateTag\": \"INSERT\",\n" +
                "    \"deviceId\": 11\n" +
                "}");

        args.addArgument("produceGroup", "ProducerGroupName_1");

        return args;
    }
}

jar部署jemter

1. mvn 打包成jar

2. jar放在/lib/extJmeter模拟RocketMQ生产者消息发送_第1张图片

 3.增加第三方引用包在/lib

关于这些jar哪儿来, 在开发环境本地仓库copy。 或者去外网仓库下载,主要事rocketmq需要的第三方jar

Jmeter模拟RocketMQ生产者消息发送_第2张图片

 jMeter创建计划-请求

选择java请求

Jmeter模拟RocketMQ生产者消息发送_第3张图片

 类名称中选择丢进去的jar下的类。Jmeter模拟RocketMQ生产者消息发送_第4张图片

设置参数

Jmeter模拟RocketMQ生产者消息发送_第5张图片

 参数默认值在项目代码 getDefaultParameters()。可修改后发送。s

你可能感兴趣的:(mq,html)