LR之MQ协议性能测试脚本
之前为MQ协议的脚本,也是忙的焦头烂额,又是查阅网上资料,又是询问盆友的,可谓千辛万苦,但又因网上关于MQ的资料及性能测试的很少,有些根本没有太大的实用性,即为了之后方便查阅,也同时为了大家解决工作中遇到相同的问题,整理了一下编写MQ性能测试脚本的方法。
场景:为linux服务器上新建一个队列管理器QM1,可查看上篇MQ消息队列搭建命令及方法;再通过java脚本,调用QM1的相关参数向QM1发送消息,观察消息是否正常送达。
java脚本只为消息队列发送报文交易(本人亲测都可以的哦 ^_^)
1. 报文在脚本中拼接方法:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.ibm.mq.*;
import lrapi.lr;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Actions
{
String PutQueueManagerName = "MQ1"; // 发送队列管理
String PutQueueName = "MSG_QUE"; // 发送队列名,相当于前置机的接收队列
String QueueChannel = "MSG_CHL"; // 通道名,要用服务器通道
String IP = "188.123.123.123";//服务器主机IP
int PutPort = 7001; // 发送端口号,相当于前置机的接收端口
int CCSID = 1381; // 客户端Unix用819,windows用1381
int OpenOptions = MQC.MQOO_INPUT_AS_Q_DEF|MQC.MQOO_OUTPUT|MQC.MQOO_INQUIRE; // 连接参数
int PutDepth = 0; // 发送队列深度
String SndTime = ""; // 当前发送时间
MQQueueManager PutQueueManager = null; // 创建发送队列管理器对象
MQQueue PutQueue = null; // 创建发送队列对象
MQMessage PutMessage = new MQMessage(); // 创建发送消息对象
MQPutMessageOptions PMO = new MQPutMessageOptions(); // 创建发送消息选项队列
public int init() throws Throwable
{
// 发送队列的参数**********************************************
MQEnvironment.hostname = IP; // 设置环境参数
MQEnvironment.port = PutPort;
MQEnvironment.CCSID = CCSID;
MQEnvironment.channel = QueueChannel;
PutQueueManager = new MQQueueManager(PutQueueManagerName); // 连接发送队列管理器
PutQueue = PutQueueManager.accessQueue(PutQueueName, OpenOptions, null, null, null); // 建立访问发送队列
PutMessage.format = MQC.MQFMT_STRING; // 设置消息中应用数据的格式
PutMessage.characterSet = 1381; // 设置字符集
PutMessage.expiry = -1; // 设置消息为不过期
return 0;
}//end of init
public int action() throws Throwable
{
//java格式化时间方法
Date date = new Date();
SimpleDateFormat sim=new SimpleDateFormat("yyyyMMddHHmmss"); //例如:20200426091410
SimpleDateFormat sim1=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sim2=new SimpleDateFormat("HH:mm:ss");
String nowdate=sim.format(date);
String nowdate1=sim1.format(date)+"T"+sim2.format(date); //例如:2020-04-26T09:14:10
//System.out.println("+++++++"+ nowdate);
//System.out.println("+++++++"+ nowdate1);
lr.start_transaction("01_发送");
PutQueueMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Document xmlns=\"urn:cnaps:std:ccms:2010:tech:xsd:ccms.303.001.02\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"<FreeFrmt>" +
"<GrpHdr>" +
"<MsgId><nowdate></MsgId>" +
"<CreDtTm><nowdate1></CreDtTm>" +
"<InstgPty>" +
"<InstgDrctPty>0000</InstgDrctPty>" +
"<InstgPty>0000</InstgPty>" +
"</InstgPty>" +
"<InstdPty>" +
"<InstdDrctPty>313501080608</InstdDrctPty>" +
"<InstdPty>313501080608</InstdPty>" +
"</InstdPty>" +
"<SysCd>HVPS</SysCd>" +
"</GrpHdr>" +
"<FreeFrmtInf>" +
"<MsgCntt>test</MsgCntt>" +
"</FreeFrmtInf>" +
"</FreeFrmt>" +
"</Document>");
lr.end_transaction("01_发送",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("01_发送退出关闭队列时出现错误,完成代码为:" + ex.completionCode + ",原因为:" + ex.reasonCode);
ex.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
return 0;
}// end of end
// 发送消息+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public void PutQueueMessage(String MyStr)
{
try
{
PutMessage = new MQMessage();
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("01_发送",lr.FAIL);
lr.error_message("01_发送 发送消息时出错,完成代码为:" + ex.completionCode + ",原因为:" + ex.reasonCode + "流水号为:" + SndTime);
lr.exit(lr.EXIT_ITERATION_AND_CONTINUE, lr.FAIL);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
2. 发送的报文通过脚本读取本地文件获得:(读取文件中文不会乱码)
/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description:
*
*/
import java.io.File;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.IOException;
import com.ibm.mq.*;
import lrapi.lr;
public class Actions
{
String PutQueueManagerName = "MQ1"; // 发送队列管理器Q
String PutQueueName = "MSG_QUE"; // 发送队列名,相当于前置机的接收队列
String QueueChannel = "MSG_CHL"; // 通道名,要用服务器通道
String IP = "188.123.123.123";//服务器主机IP
int PutPort = 7001; // 发送端口号,相当于前置机的接收端口
int CCSID = 1381; // 客户端Unix用819,windows用1381
int OpenOptions = MQC.MQOO_INPUT_AS_Q_DEF|MQC.MQOO_OUTPUT|MQC.MQOO_INQUIRE; // 连接参数
int PutDepth = 0; // 发送队列深度
String SndTime = ""; // 当前发送时间
MQQueueManager PutQueueManager = null; // 创建发送队列管理器对象
MQQueue PutQueue = null; // 创建发送队列对象
MQMessage PutMessage = new MQMessage(); // 创建发送消息对象
MQPutMessageOptions PMO = new MQPutMessageOptions(); // 创建发送消息选项队列
public int init() throws Throwable
{
// 发送队列的参数**********************************************
MQEnvironment.hostname = IP; // 设置环境参数
MQEnvironment.port = PutPort;
MQEnvironment.CCSID = CCSID;
MQEnvironment.channel = QueueChannel;
PutQueueManager = new MQQueueManager(PutQueueManagerName); // 连接发送队列管理器
PutQueue = PutQueueManager.accessQueue(PutQueueName, OpenOptions, null, null, null); // 建立访问发送队列
PutMessage.format = MQC.MQFMT_STRING; // 设置消息中应用数据的格式
PutMessage.characterSet = 1381; // 设置字符集
PutMessage.expiry = -1; // 设置消息为不过期
return 0;
}//end of init
public int action() throws Throwable
{
SndTime = String.valueOf(System.currentTimeMillis()); // 获取当前发送时间,13位
String MsgBody = read("E:\\LR项目资料\\测试\\beps.<NO>.xml");
lr.start_transaction("01_发送");
PutQueueMessage(MsgBody);
lr.end_transaction("01_发送",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("01_发送退出关闭队列时出现错误,完成代码为:" + ex.completionCode + ",原因为:" + ex.reasonCode);
ex.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
return 0;
}// end of end
// 发送消息++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public void PutQueueMessage(String MyStr)
{
try
{
PutMessage = new MQMessage();
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("01_发送",lr.FAIL);
lr.error_message("01_发送 发送消息时出错,完成代码为:" + ex.completionCode + ",原因为:" + ex.reasonCode + "流水号为:" + SndTime);
lr.exit(lr.EXIT_ITERATION_AND_CONTINUE, lr.FAIL);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static String read(String fileName){
String MsgBody = "";
String encoding = "UTF-8";
FileInputStream in = null;
try {
in = new FileInputStream(fileName);
int len = in.available();
byte[] b = new byte[len];
in.read(b);
MsgBody = new String(b,encoding);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return MsgBody;
}
}
附件:

MQ jar包下载地址:
链接:https://pan.baidu.com/s/1WWJzuegvajO2aZ30erMmgg
提取码:a1am
复制这段内容后打开百度网盘手机App,操作更方便哦
作者:S-Gavin
---------------------------------------
您的支持是我前进的动力!!!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点球球“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行。若要你的心情好,请便右侧赏博主。哈哈哈(っ•̀ω•́)っ✎⁾⁾

浙公网安备 33010602011771号