lesson5:利用jmeter来压测消息队列(activemq)

时间:2021-10-12 00:16:05

本文讲述了利用jmeter来压测消息队列,其中消息队列采用apache的activemq,jmeter本身是支持符合jms标准消息队列的压测,由于jmeter的官方sampler配置比较复杂,本文直接使用sdk的方式来压测,与生产实际使用更加接近,各位如对官方的sampler感兴趣,可以自行去配置完成。

准备工作:下载activemq 地址:http://activemq.apache.org 本文中的activemq采用的是5.9.0版本。

jMetterLessons工程源码地址:https://github.com/mantuliu/jMetterLessons

1.将下载到的activemq的压缩包解压,目录中不要有中文名称,然后在 bin 目录执行activemq.bat命令,命令行输出类似于下面的界面,证明activemq启动成功:

lesson5:利用jmeter来压测消息队列(activemq)

2.访问站点:http://localhost:8161/admin 用户名:admin 密码:admin 登录成功后,activemq管理界面如下所示,目前还没有相应的队列:

lesson5:利用jmeter来压测消息队列(activemq)

3.在工程jMeterLessions下的pom文件中增加下面的依赖:

    <dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.5</version>
</dependency>

4.使用mvn eclipse:eclipse重新生成eclipse项目,命令在执行过程中自动下载相关的包

5.调用activemq的发送者类如下:

package com.mantu.jmeter;

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.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import javax.jms.*; /**
* blog http://www.cnblogs.com/mantu/
*
* @author mantu
*
*/
public class Lesson5 extends AbstractJavaSamplerClient{ public static void main(String [] args){ } @Override
public SampleResult runTest(JavaSamplerContext arg0) {
// TODO Auto-generated method stub
SampleResult sr = new SampleResult();
sr.setSampleLabel("activemq测试");
try{
sr.sampleStart();
String user = env("ACTIVEMQ_USER", "admin");
String password = env("ACTIVEMQ_PASSWORD", "password");
String host = env("ACTIVEMQ_HOST", "localhost");
int port = Integer.parseInt(env("ACTIVEMQ_PORT", "61616"));
String destination = "event";
int messages = 10;
int size = 256;
String DATA = "abcdefghijklmnopqrstuvwxyz";
String body = "";
for( int i=0; i < size; i ++) {
body += DATA.charAt(i%DATA.length());
} ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + host + ":" + port); Connection connection = factory.createConnection(user, password);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination dest = new ActiveMQQueue(destination);
MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); for( int i=1; i <= messages; i ++) {
TextMessage msg = session.createTextMessage(body);
msg.setIntProperty("id", i);
producer.send(msg);
if( (i % 1000) == 0) {
System.out.println(String.format("Sent %d messages", i));
}
}
producer.send(session.createTextMessage("SHUTDOWN"));
connection.close();
sr.setResponseData("success");
sr.setDataType(SampleResult.TEXT);
sr.setSuccessful(true);
}
catch(Exception ex){
sr.setSuccessful(false);
ex.printStackTrace();
}
finally{
sr.sampleEnd();
}
return sr;
} private static String env(String key, String defaultValue) {
String rc = System.getenv(key);
if( rc== null )
return defaultValue;
return rc;
} private static String arg(String []args, int index, String defaultValue) {
if( index < args.length )
return args[index];
else
return defaultValue;
}
}

6.导出jMetterLessons工程为jar文件,输出到jmeter的lib/ext目录,并将activemq-all-5.9.0.jar(此文件可以在activemq中找到)拷贝到jmeter的lib/ext目录
7.配置java请求,并将类配置为Lesson5

lesson5:利用jmeter来压测消息队列(activemq)

启动压力测试后,可以在站点http://localhost:8161/admin发现队列已经被创建,并且队列中产生了相应的消息,本文展示了activemq队列的生产者的压力测试方法,对于消费者,原理也是大同小异的,大家可以自行模拟