四、测试脚本
以下为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; } } |