使用loadrunner Java协议脚本测试socket接口遇到的效率问题
在eclips里面调用代码向socket 接口发送xml发送及接收仅需要不到2s.但是将代码写到loadrunner中调用至少需要14秒。
在运行时明显的卡在了
Socket mSocket = new Socket(cIP, cPort);
这是什么问题啊。有办法解决么?
我的loadrunner中的代码如下:
/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description:
*
*/
import lrapi.lr;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import org.apache.log4j.Logger;
import com.sinosoft.midplat.common.IOTrans;
public class Actions
{ private String cIP;
private int cPort;
public int action() throws Throwable {
System.out.println("程序开始...");
//本地
String mIP = "10.50.102.47";
int mPort = 7032;
/**
* 1021-新单投保
* 1022-承保收费
* 1013-保单重打
* 1002-当日撤单
*/
String mFuncFlag = null;
//返盘
mFuncFlag = "1021";
String fileName = "<xml_name>";
String mInFilePath = "C:\\create_xml/"+fileName+".xml";
String mOutFilePath = "C:\\out_xml\\"+fileName+"out.xml";
Actions mTestUI = new Actions();
mTestUI.setParmar("10.50.102.47",7032);
InputStream mIs = new FileInputStream(mInFilePath);
byte[] mOutBytes = mTestUI.sendRequest(mFuncFlag, mIs);
OutputStream mFos = new FileOutputStream(mOutFilePath);
mFos.write(mOutBytes);
System.out.println("成功结束!");
return 0;
}
public void setParmar(String pIP, int pPort) {
this.cIP = pIP;
this.cPort = pPort;
}
public byte[] sendRequest(String pFuncFlag, InputStream pInputStream) throws Exception {
Socket mSocket = new Socket(cIP, cPort);
long mOldTimeMillis = System.currentTimeMillis();
long mCurTimeMillis = mOldTimeMillis;
byte[] mInBodyBytes = IOTrans.toBytes(pInputStream);
byte[] mInTotalBytes = new byte;
//报文体长度
String mInBodyLengthStr = String.valueOf(mInBodyBytes.length);
int length = mInBodyLengthStr.length();
for(int i=0;i<8-length;i++){
mInBodyLengthStr="0"+mInBodyLengthStr;
}
byte[] mInLengthBytes = mInBodyLengthStr.getBytes();
Throwable System.arraycopy(InsuBytes, 0, mInTotalBytes, 0, InsuBytes.length);
byte[] mFuncFlagBytes = pFuncFlag.getBytes();
System.arraycopy(mFuncFlagBytes, 0, mInTotalBytes, 4, mFuncFlagBytes.length);
System.arraycopy(mInLengthBytes, 0, mInTotalBytes, 8, mInLengthBytes.length);
System.arraycopy(mInBodyBytes, 0, mInTotalBytes, 16, mInBodyBytes.length);
mSocket.getOutputStream().write(mInTotalBytes);
mSocket.shutdownOutput();
mCurTimeMillis = System.currentTimeMillis();
System.out.println();
InputStream mSocketIs = mSocket.getInputStream();
byte[] mOutHeadBytes = new byte;
for (int tReadSize = 0; tReadSize < mOutHeadBytes.length;) {
int tRead = mSocketIs.read(mOutHeadBytes, tReadSize, mOutHeadBytes.length-tReadSize);
if (-1 == tRead) {
throw new EOFException("获取报文头出错!实际读入:" + new String(mOutHeadBytes));
}
tReadSize += tRead;
}
int mOutBodyLengthInt = Integer.parseInt(new String(mOutHeadBytes, 8, 8).trim());
byte[] mOutBodyBytes = new byte;
for (int tReadSize = 0; tReadSize < mOutBodyBytes.length;) {
int tRead = mSocketIs.read(mOutBodyBytes, tReadSize, mOutBodyBytes.length-tReadSize);
if (-1 == tRead) {
throw new EOFException("获取报文体出错!实际读入长度为:" + tReadSize);
}
tReadSize += tRead;
}
mSocket.close();
mCurTimeMillis = System.currentTimeMillis();
return mOutBodyBytes;
}
public int init() throws Throwable {
return 0;
}//end of init
public int end() throws Throwable {
return 0;
}//end of end
}
页:
[1]