使用Loadrunner对IBM MQ进行性能测试
作者:网络转载 发布时间:[ 2015/1/30 11:56:11 ] 推荐标签:LoadRunner 性能测试工具
四、测试脚本
以下为MQ发送测试脚本,利用此脚本只用根据相应测试需求,调整注释部分:
/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description: simple test harness to PUT messages on a MQ queue
*
*/
import lrapi.lr;
import com.ibm.mq.*;
import java.util.HashMap;
import java.util.Random;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Actions
{
// 队列管理器
String queueMgrName = "QMCCPS01";
// 队列名
String putQueueName = "CNAPS_BPH";
// 通道名
String channel = "SYSTEM.DEF.SVRCONN";
// 消息
String msgBody = "";
// ip 地址
String hostname = "10.40.2.16";
// 端口号
int port = 1601;
// 字符集
int CCSID = 819;
MQQueueManager queueMgr = null;
MQQueue getQueue = null;
MQQueue putQueue = null;
MQPutMessageOptions pmo = new MQPutMessageOptions();
MQGetMessageOptions gmo = new MQGetMessageOptions();
MQMessage requestMsg = new MQMessage();
MQMessage responseMsg = new MQMessage();
// ?????????
public int init() throws Throwable{
// Open a connection to the queue manager and the put/get queues
try {
// As values set in the MQEnvironment class take effect when the
// MQQueueManager constructor is called, you must set the values
// in the MQEnvironment class before you construct an MQQueueManager
// object.
MQEnvironment.hostname=hostname;
MQEnvironment.port=port;
MQEnvironment.CCSID =CCSID;
MQEnvironment.properties.put("transport", "MQSeries");
// MQEnvironment.channel = "SYSTEM.DEF.SVRCONN";
MQEnvironment.channel = channel;
queueMgr = new MQQueueManager(queueMgrName);
// Access the put/get queues. Note the open options used.
putQueue = queueMgr.accessQueue(putQueueName, 49);
// getQueue= queueMgr.accessQueue(getQueueName,
// MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public int action() throws Throwable{
// This is an XML message that will be put on the queue. Could do some
// fancy
// things with XML classes here if necessary.
// The message string can contain {parameters} if lr.eval_string() is
// used.
// Clear the message objects on each iteration.
requestMsg.clearMessage();
responseMsg.clearMessage();
//读取报文内容,并利用replace函数参数化报文编号
String req = read("G:\大额贷记来账.xml");
String data_msg = "<data_msg>";
String msgBody = req.replace("<MsgId>2010101000000000</MsgId>", "<MsgId>20200117" + data_msg + "</MsgId>");
// Create a message object and put it on the request queue
try {
pmo.options = MQC.MQPMO_NEW_MSG_ID; // The queue manager replaces
// the contents of the MsgId
// field in MQMD with a new
// message identifier.
// should be put on this queue
requestMsg.report = MQC.MQRO_PASS_MSG_ID; // If a report or reply is
// generated as a result
// of this message, the
// MsgId of this message
// is copied to the
// MsgId of the report
// or reply message.
requestMsg.format = MQC.MQFMT_STRING; // Set message format. The
// application message data
// can be either an SBCS
// string (single-byte
// character set), or a DBCS
// string (double-byte
// character set).
// requestMsg.messageType=MQC.MQMT_REQUEST; // The message is one
// that requires a reply.
lr.start_transaction("大额贷记来账");
requestMsg.writeString(msgBody); // message payload
MQMessage inMsg = new MQMessage();
inMsg.write(msgBody.getBytes("UTF-8"));
putQueue.put(inMsg,pmo);
lr.end_transaction("大额贷记来账",lr.PASS );
} catch (Exception e) {
e.printStackTrace();
}
/***
* // Get the response message object from the response queue try {
* responseMsg.correlationId = requestMsg.messageId; // The Id to be
* matched against when getting a message from a queue
* gmo.matchOptions=MQC.MQMO_MATCH_CORREL_ID; // The message to be
* retrieved must have a correlation identifier that matches the value
* of the CorrelId field in the MsgDesc parameter of the MQGET call.
* gmo.options=MQC.MQGMO_WAIT; // The application waits until a suitable
* message arrives. gmo.waitInterval=60000; // timeout in ms
* getQueue.get(responseMsg, gmo);
*
* // Check the message content byte[] responseMsgData =
* responseMsg.readStringOfByteLength
* (responseMsg.getTotalMessageLength()).getBytes(); String msg = new
* String(responseMsgData); lr.output_message(msg); // for debugging.
* Disable this for a load test. // TODO: add your own message checking
* here using string functions. // I have found that extracting XML
* fields and comparing them (rather than // comparing the whole message
* body or substrings) is more resistant to change. // If no match is
* found, then lr.error_message() and lr.exit(). } catch(Exception e) {
* e.printStackTrace(); lr.error_message("Error receiving message.");
* lr.exit(lr.EXIT_VUSER, lr.FAIL); }
*
* lr.end_transaction("test_message", lr.AUTO);
*/
return 0;
}// end of action
// ???????
public int end() throws Throwable{
// Close all the connections
try {
putQueue.close();
// getQueue.close();
queueMgr.close();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}// end of end
public static String read(String fileName){
String req = "";
FileInputStream in = null;
try {
in = new FileInputStream(fileName);
int len = in.available();
byte[] b = new byte[len];
in.read(b);
req = new String(b);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return req;
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11