2016年2月5日星期五

JMeter_015:使用Java Request 采样器测试AMQ Queue发送

环境:MAC OS X EI Capitan 10.11.3 + JMeter 2.13 + JBoss  AMQ 6.2.1

JMeter 自带的JMS Point-to-Point 只能同时测试发送和接收,客户想单独分开测试发送和接收。
我想了想,还是自己写程序,然后用Java Request 采样器。

1. AmqSenderTest.java

package com.travelsky.activemq;

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 javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 *
 * @author maping
 */
public class AmqSenderTest extends AbstractJavaSamplerClient {

    private static final String LABEL = "AmqSenderTest";
    private static final int DEFAULT_COUNT = 1;
    private static final String DEFAULT_URL = "failover:(tcp://127.0.0.1:61616)";
    private static final String DEFAULT_ADMIN_USER = "admin";
    private static final String DEFAULT_ADMIN_PASSWORD = "admin";
   
    private int count;
    private String url;
    private ConnectionFactory connectionFactory;
    private Connection connection;
    private Session session;
    private Destination destination;
    private MessageProducer producer;

    @Override
    public void setupTest(JavaSamplerContext arg0) {
        System.out.println("### setupTest");
        String countStr = arg0.getParameter("count");
        count = (countStr != null ? Integer.parseInt(countStr) : DEFAULT_COUNT);
        String urlStr = arg0.getParameter("url");
        url = (urlStr != null ? urlStr : DEFAULT_URL);

        connectionFactory = new ActiveMQConnectionFactory(
                DEFAULT_ADMIN_USER,
                DEFAULT_ADMIN_PASSWORD,
                url);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("FirstQueue");
            producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public SampleResult runTest(JavaSamplerContext arg0) {
        SampleResult sr = new SampleResult();
        sr.setSampleLabel(LABEL);
        sr.sampleStart();

        try {
            sendMessage(session, producer);
            sr.setSuccessful(true);
            System.out.println("### send over!");
        } catch (Exception e) {
            e.printStackTrace();
            sr.setSuccessful(false);
        } finally {
            sr.sampleEnd();
        }
        return sr;
    }

    public void sendMessage(Session session, MessageProducer producer)
            throws Exception {
        for (int i = 1; i <= count; i++) {
            TextMessage message = session
                    .createTextMessage("ActiveMq发送的消息" + i);
            producer.send(message);
        }
    }

    @Override
    public void teardownTest(JavaSamplerContext arg0) {
        System.out.println("### teardownTest");
        try {
            if (null != connection) {
                connection.close();
            }
        } catch (Throwable ignore) {
        }
    }

    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("count", DEFAULT_COUNT + "");
        params.addArgument("url", DEFAULT_URL);
        return params;
    }
}

2. 编译+部署
(1)CLASSPATH需要加入ApacheJMeter_core.jar和ApacheJMeter_java.jar
(2)把编译后的jar包,复制到 jmeter/lib/ext目录下

3. 创建测试计划
50个线程,每个线程发送2000条消息,一共10万条消息。


4. 运行
/Users/maping/Apache/jmeter/bin/jmeter.sh  -n -t /Users/maping/Apache/jmeter/test/AMQ-Queue-Sender.jmx  -Jloop=1 -l /Users/maping/Apache/jmeter/result/AMQ-Queue-Sender_`date +'%y%m%d%H%M%S'`.csv

没有评论: