个人学习总结
本帖最后由 lewtone 于 2016-8-12 16:21 编辑public class Ocean2 extends AbstractJavaSamplerClient
{
private String AppClass;
private String CommandID;
private String resultData;
// 这个方法是用来自定义java方法入参的。
// params.addArgument("num1","");表示入参名字叫num1,默认值为空。
// 设置可用参数及的默认值;
public Arguments getDefaultParameters()
{
Arguments params = new Arguments();
params.addArgument("AppClass", "");
params.addArgument("CommandID", "");
params.addArgument("RequestString", "");
return params;
}
// 每个线程测试前执行一次,做一些初始化工作;
public void setupTest(JavaSamplerContext arg0)
{
}
// 测试结束时调用;
public void teardownTest(JavaSamplerContext arg0)
{
// System.out.println(end);
// System.out.println("The cost is"+(end-start)/1000);
}
// 开始测试,
public SampleResult runTest(JavaSamplerContext arg0)
{
AppClass = arg0.getParameter("AppClass");
CommandID = arg0.getParameter("CommandID");
SampleResult sr = new SampleResult();
sr.setSampleLabel("Expo请求");
try
{
ByteWriter buffer = new ByteWriter(1024);
// buffer.writeShortString(arg0.getParameter("RequestString"));
buffer.writeShortString("name=getlist&mediatype=01&pageno=1&pagesize=10¶m_v2=(section:LM01 OR section:LM02) AND (windcode:600000.SHOR windcode: 600001.SH)");
MessageV2 messageV2 = new MessageV2();
messageV2.setCommand(1411, 6001);
// messageV2.setCommand(Integer.parseInt(AppClass),Integer.parseInt(CommandID) );
messageV2.setMsgBody(buffer.toByteArray());
sr.sampleStart();// jmeter 开始统计响应时间标记
MessageV2Return result = ExpoHandle.sendMessage(messageV2, 6000);
if (result != null && result.getV2Msg() != null && result.getV2Msg().getMsgBody() != null)
{
byte[] data = result.getV2Msg().getMsgBody();
//byte[] resultData = new byte;
//System.arraycopy(data, 4, resultData, 0, resultData.length);
//System.out.println(new String(resultData) + "\n");
resultData=new String(data,"UTF-8");
}
if (resultData != null && resultData.length() > 0)
{
sr.setResponseData("成功结果是:" + resultData, null);
sr.setDataType(SampleResult.TEXT);
} else
{
sr.setResponseData("失败结果是:" + resultData, null);
sr.setDataType(SampleResult.TEXT);
}
// System.out.println(resultData);
sr.setSuccessful(true);
} catch (Throwable e)
{
sr.setSuccessful(false);
e.printStackTrace();
} finally
{
sr.sampleEnd();// jmeter 结束统计响应时间标记
// System.exit(0);
}
return sr;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
}
}
支持分享~ test
页:
[1]