您的位置:首页 > 其它

LR中MQ协议交易脚本介绍

2016-05-02 19:52 253 查看
MQ是IBM的消息中间件。

Loadrunner是不直接支持MQ协议,但是loadrunner是java vuser,所以我们可以通过Loadrunner中调用MQ提供的jar包,模拟实现对MQ进行消息传输的功能。

1.准备环境,jdk环境,MQ的jar包。

MQ的jar,当安装MQ软件后,在安装目录下有jar lib目录,不知道要具体要用哪几个包,就全部增加。



2.开始脚本开发

详细脚本如下:

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import com.ibm.mq.*;

import lrapi.lr;

public class Actions

{

String PutQueueManagerName = "QM_MBR_5500"; // 发送队列管理器名称,MQ服务器中队列管理器

String PutQueueName = "REQ.5500.CBS.NNS"; // 发送队列名,MQ应用取数据的队列

String QueueChannel = "SVRCONN_MBR5500"; // 通道名,要用服务类型的通道

String HostName = "10.14.1.10"; // MQ服务的IP地址

int PutPort = 65400; // 发送端口,MQ服务器中监听器的端口,

int CCSID = 1208; // 客户端Unix用819,windows用1381

int OpenOptions = MQC.MQOO_INPUT_AS_Q_DEF|MQC.MQOO_OUTPUT|MQC.MQOO_INQUIRE; // 连接参数

int PutDepth = 0; // 发送队列深度

MQQueueManager PutQueueManager = null; // 创建发送队列管理器对象

MQQueue PutQueue = null; // 创建发送队列对象

MQMessage PutMessage = null; // 创建发送消息对象

MQPutMessageOptions PMO = null; // 创建发送消息选项对象

public int init() throws Throwable

{

// 发送队列的参数**********************************************

MQEnvironment.hostname = HostName; // 设置环境参数

MQEnvironment.port = PutPort;

MQEnvironment.CCSID = CCSID;

MQEnvironment.channel = QueueChannel;

PutQueueManager = new MQQueueManager(PutQueueManagerName); // 连接发送队列管理器

PutQueue =PutQueueManager .accessQueue(queueName, openOptions); ; // 建立访问发送队列

PutMessage =new MQMessage();

PutMessage.format = MQC.MQFMT_STRING; // 设置消息中应用数据的格式

PutMessage.characterSet = 1208; // 设置字符集

PutMessage.expiry = -1; // 设置消息为不过期

return 0;

}//end of init

public int action() throws Throwable

{

lr.start_transaction("test");//设置开始事务
string msgbody ="<?xml version=\"1.0\" encoding=\"UTF-8\"?><transaction>test</transaction>";//保存要发送报文

PutQueueMessage(msgbody);

//设置事务

lr.end_transaction("test",lr.PASS);

return 0;

}//end of action

public int end() throws Throwable

{

try

{

PutQueue.close(); //关闭队列

PutQueueManager.close();//关闭队列管理器

PutQueueManager.disconnect(); //断开连接

}

catch (MQException ex)

{

lr.error_message("测试,完成代码为:" + ex.completionCode + ",原因为:" + ex.reasonCode);

ex.printStackTrace();

}

catch(Exception e)

{

e.printStackTrace();

}

return 0;

}// end of end

// 发送消息

public void PutQueueMessage(String MyStr)

{

try

{

PutMessage.write(MyStr.getBytes("UTF-8")); // 设置消息字体

PutQueue.put(PutMessage, PMO);// 将消息放入队列

PutQueueManager.commit(); // 提交事务处理

PutDepth = PutQueue.getCurrentDepth(); // 获取发送队列的深度

//System.out.println("++++++发送队列当前深度为:"+ PutDepth);

//System.out.println("=======发送报文是:" + MyStr);

PutMessage.clearMessage();

PutMessage = null;

}

catch (MQException ex)

{

lr.end_transaction("test",lr.FAIL);

lr.error_message("test,完成代码为:" + ex.completionCode + ",原因为:" + ex.reasonCode);

lr.exit(lr.EXIT_ITERATION_AND_CONTINUE, lr.FAIL);

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

MQ协议通常是异步处理,所以此脚本中只发送MQ交易,不验证返回结果,所以交易响应时间无法统计。但是统计响应时间的方法一般有如下两种:从MQ日志中提取响应时间和从应用数据库中统计响应时间。

因此对应的开始时间和结束时间的统计可能有如下几种方式,需要大家在实际项目中灵活使用。

响应时间的开始时间:

1) 消息进入压力发起的MQ队列中的时间

2) 消息从进入到应用系统的MQ队列中时间

3) 应用系统从队列中取出消息开始处理的时间

响应时间的结束时间:

1) 应用系统处理完数据的时间

2) 应用系统将消息放入MQ队列的时间

对端MQ队列接收到MQ消息的时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: