1.1. jmeter-java插件实现接口测试
(linux /mysql/rabbit-mq)
本次需要准备环境
Eclipse+jdk8
Jmeter
Python
1.1.1. Rabbit-mq web客户端简单介绍
Rabbit-mq的安装参见上一次的演示视频
生产速度:消息的产生速度,队列消息的写入
消费速度:消息的消费速度,队列消息的传出
1.1.2. nmon监控使用
(1) 以管理员身份在各节点服务器安装nmon
yum install nmon
(2) 运行nmon监控
nmon –f –s 10 –c 1200
参数说明:
-f 输出文件
-s 间隔多少秒获取一次监控数据
-c 一共要获取多少次数据
后台运行
nohup nmon –f –s 10 –c 1200 &
(3) 监控结束后运行
sort …..nmon >> ….csv
将nmon监控文件转化成csv格式,然后通过ssh-ftp工具取到windows下,利用nmon analyser工具将csv文件转换成excel格式文件,并保存,可以打开文件查看监控数据。
监控输出文件excel文件分析说明
主要查看SYS_SUM页内容(查看cpu利用率,磁盘IOPS,以及IO-Wait%)
(cpu利用率均值在70%以下,普通磁盘iops在4000以下,IO-Wait%均值在10%以下)
1.1.3. python屏幕截图
由于rabbitmq客户端是实时更新数据,所以要保留测试过程中的数据,需要对客户端屏幕进行截图。
采用 python脚本进行自动截图
(1) 配置本地python环境
(2) 安装 python截图模块
pip install image
(3) 编写python截图脚本,保存为image.py文件
脚本示例:
import time from PIL import ImageGrab for i range(1200): im = ImageGrab.grab() addr = str(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()))+str('.jpg') im.save(addr,'jpeg') time.sleep(5)
(4) 将脚本拷贝到一个可写的目录(注意空目录最好,并且有足够空间),运行脚本
python image.py
在脚本目录将生成屏幕截图,每隔5秒一张图片
1.1.4. 编写rabbitmq生产者插件
(1) 本地环境eclipse
(2) 创建工程
创建libs文件夹并导入jmeter的核心libs和java请求lib;
导入rabbitmq-java客户端lib和json数据lib
代码示例如下:
package RabbitMQJmeter; import java.io.IOException; import java.util.concurrent.TimeoutException; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.samplers.SampleResult; import com.alibaba.fastjson.JSONObject; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class LCS2Logic extends AbstractJavaSamplerClient { // 定义rabbitmq服务器的配置信息 public static String MQ_serverip; public static String MQ_serverport; public static String MQ_serveruser; public static String MQ_serveruserpwd; public static String MQ_queue; public static String MQ_exchange; public static String MQ_rkey; // 定义消息发送次数 public static String send_count; // 定义mq的队列消息ec信息 public static String ec_sessionId; public static String ec_ecid; public static String ec_routInfo; public static String ec_priority; public static String ec_groupId; // 定义mq的队列消息ec msg信息 public static String msg_templateId; public static String msg_templateContent; public static String msg_content; public static String msg_mobiles; public static String msg_sign; public static String msg_extendCode; public static String msg_needMo; // public ConnectionFactory factory; public Connection conn; public Channel channel; // 设置可用参数及的默认值; @Override public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("MQ_serverip", "192.168.32.204"); params.addArgument("MQ_serverport", "5672"); params.addArgument("MQ_serveruser", "mas"); params.addArgument("MQ_serveruserpwd", "mas"); params.addArgument("MQ_queue", "downQueue.queue2"); params.addArgument("MQ_exchange", "downQueue.queue2"); params.addArgument("MQ_rkey", "downQueue.queue2"); params.addArgument("send_count", "1"); params.addArgument("ec_sessionId", "3"); params.addArgument("ec_ecid", "EC-1"); params.addArgument("ec_routInfo", "127.0.0.1"); params.addArgument("ec_priority", "1"); params.addArgument("ec_groupId", "3"); params.addArgument("msg_templateId", ""); params.addArgument("msg_templateContent", ""); params.addArgument("msg_content", "test message jmeter"); params.addArgument("msg_mobiles", "13525102870,13671381066,18827103637"); params.addArgument("msg_sign", "ABVFGH1"); params.addArgument("msg_extendCode", "afsd"); params.addArgument("msg_needMo", "1"); return params; } // 每个线程测试前执行一次,做一些初始化工作; @Override public void setupTest(JavaSamplerContext arg0) { MQ_serveruser = arg0.getParameter("MQ_serveruser", ""); MQ_serveruserpwd = arg0.getParameter("MQ_serveruserpwd", ""); MQ_serverip = arg0.getParameter("MQ_serverip", ""); MQ_serverport = arg0.getParameter("MQ_serverport", ""); MQ_queue = arg0.getParameter("MQ_queue", ""); MQ_exchange = arg0.getParameter("MQ_exchange", ""); MQ_rkey = arg0.getParameter("MQ_rkey", ""); int server_port = Integer.parseInt(MQ_serverport); factory = new ConnectionFactory(); factory.setUsername(MQ_serveruser); factory.setPassword(MQ_serveruserpwd); factory.setHost(MQ_serverip); factory.setPort(server_port); // Connection conn; try { conn = factory.newConnection(); channel = conn.createChannel(); channel.exchangeDeclare(MQ_exchange, "direct", true);// EXCHANGE // 定义交换机 String queueName = MQ_queue;// message-queue得到消息队列 channel.queueBind(queueName, MQ_exchange, MQ_rkey);// route-bind // 定义类似路由器的东西路由交换机VS队列 } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } // long start = System.currentTimeMillis(); // System.out.println("start:" + start); } // 开始测试,从arg0参数可以获得参数值; @Override public SampleResult runTest(JavaSamplerContext arg0) { send_count = arg0.getParameter("send_count", ""); ec_sessionId = arg0.getParameter("ec_sessionId", ""); ec_ecid = arg0.getParameter("ec_ecid", ""); ec_routInfo = arg0.getParameter("ec_routInfo", ""); ec_priority = arg0.getParameter("ec_priority", ""); ec_groupId = arg0.getParameter("ec_groupId", ""); msg_templateId = arg0.getParameter("msg_templateId", ""); msg_templateContent = arg0.getParameter("msg_templateContent", ""); msg_content = arg0.getParameter("msg_content", ""); msg_mobiles = arg0.getParameter("msg_mobiles", ""); msg_sign = arg0.getParameter("msg_sign", ""); msg_extendCode = arg0.getParameter("msg_extendCode", ""); msg_needMo = arg0.getParameter("msg_needMo", ""); //消息内容格式 int sessionId = Integer.parseInt(ec_sessionId); String ecId = ec_ecid; String routInfo = ec_routInfo; int priority = Integer.parseInt(ec_priority); int groupId = Integer.parseInt(ec_groupId); String templateId = msg_templateId; String[] templateContent = msg_templateContent.split(","); String content = msg_content; String[] mobiles = msg_mobiles.split(","); String sign = msg_sign; String extendCode = msg_extendCode; int needMo = Integer.parseInt(msg_needMo); //转换成json格式 JSONObject jsonObject = new JSONObject(); JSONObject msgObject = new JSONObject(); jsonObject.put("sessionId", sessionId); jsonObject.put("ecId", ecId); jsonObject.put("routInfo", routInfo); jsonObject.put("priority", priority); jsonObject.put("groupId", groupId); // msgObject.put("templateId", templateId); msgObject.put("templateContent", templateContent); msgObject.put("content", content); msgObject.put("mobiles", mobiles); msgObject.put("sign", sign); msgObject.put("extendCode", extendCode); msgObject.put("needMo", needMo); jsonObject.put("msg", msgObject); // String messageBody = jsonObject.toString(); byte[] messageBodyBytes = messageBody.getBytes(); // 长连接服务器发送过来的消息头,12个字节, byte[] mqHeadBytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; byte[] messageBytes = new byte[mqHeadBytes.length + messageBodyBytes.length]; System.arraycopy(mqHeadBytes, 0, messageBytes, 0, mqHeadBytes.length); System.arraycopy(messageBodyBytes, 0, messageBytes, mqHeadBytes.length, messageBodyBytes.length); // 打印输出调试 // System.out.println("生产者:" + messageBody + " in thread:" + // Thread.currentThread().getName()); int count = Integer.parseInt(send_count); /// SampleResult sr = new SampleResult(); sr.setSampleLabel("JavaTest_RabbitMQ_MSG"); try { // jmeter开始统计响应时间标记 sr.sampleStart(); // publish / sub 生产者的作用就是将消息推送到消息队列里面去 实现类似于publish的功能 for (int i = 0; i < count; i++) { channel.basicPublish(MQ_exchange, MQ_rkey, null, messageBytes); } // sr.isSuccessful(); sr.setSuccessful(true); } catch (IOException e) { sr.setSuccessful(false); e.printStackTrace(); } finally { sr.sampleEnd();// jmeter 结束统计响应时间标记 } return sr; } // 测试结束时调用; @Override public void teardownTest(JavaSamplerContext arg0) { try { channel.close(); conn.close(); // long end = System.currentTimeMillis(); // System.out.println("end:" + end); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } } }