本文講述了利用jmeter來壓測消息隊列,其中消息隊列採用apache的activemq,jmeter自己是支持符合jms標準消息隊列的壓測,因爲jmeter的官方sampler配置比較複雜,本文直接使用sdk的方式來壓測,與生產實際使用更加接近,各位如對官方的sampler感興趣,能夠自行去配置完成。java
準備工做:下載activemq 地址:http://activemq.apache.org 本文中的activemq採用的是5.9.0版本。git
jMetterLessons工程源碼地址:https://github.com/mantuliu/jMetterLessonsgithub
1.將下載到的activemq的壓縮包解壓,目錄中不要有中文名稱,而後在 bin 目錄執行activemq.bat命令,命令行輸出相似於下面的界面,證實activemq啓動成功:apache
2.訪問站點:http://localhost:8161/admin 用戶名:admin 密碼:admin 登陸成功後,activemq管理界面以下所示,目前尚未相應的隊列:session
3.在工程jMeterLessions下的pom文件中增長下面的依賴:eclipse
<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項目,命令在執行過程當中自動下載相關的包tcp
5.調用activemq的發送者類以下:ide
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測試
啓動壓力測試後,能夠在站點http://localhost:8161/admin發現隊列已經被建立,而且隊列中產生了相應的消息,本文展現了activemq隊列的生產者的壓力測試方法,對於消費者,原理也是大同小異的,你們能夠自行模擬spa