jmeter java request_JMeter 5.0(2):编写Java Request

1 前言

现有的sample已基本能满足日常使用,但如果有些特殊要求,比如远程连接服务器,或者操作新型NoSQL库,就得专门定制

Java Request采样器要求实现org.apache.jmeter.protocol.java.sampler.JavaSamplerClient接口,或者继承AbstractJavaSamplerClient抽象类

系统自带JavaTest、SleepTest 2个Java Request采样器

2 JavaSamplerClient

每个线程,JMeter都会创建一个JavaSamplerClient的实例

JavaSamplerClient运行过程:

先执行setupTest(),完成初始化

然后是runTest(),每次迭代都会执行

最后是teardownTest(),可能存在的资源释放

接口方法如下:

getDefaultParameters : Arguments,在GUI模式下显示参数及默认值

runTest(JavaSampleContext ctx) : SampleResult

setupTest(JavaSampleContext ctx) : void

teardownTest(JavaSampleContext ctx) : void

3 SampleResult

SampleResult携带一系列属性,可以提供给pre/post processor,或者交由Assertion校验

SampleResult(boolean nanoTime, long nanoThreadSleep) {

this.responseData = EMPTY_BA;

this.responseCode = "";

this.label = "";

this.resultFileName = "";

this.threadName = "";

this.responseMessage = "";

this.responseHeaders = "";

this.requestHeaders = "";

this.timeStamp = 0L;

this.startTime = 0L;

this.endTime = 0L;

this.idleTime = 0L;

this.pauseTime = 0L;

this.dataType = "";

this.files = new HashSet(3);

this.contentType = "";

this.elapsedTime = 0L;

this.latency = 0L;

this.connectTime = 0L;

this.testLogicalAction = TestLogicalAction.CONTINUE;

this.stopThread = false;

this.stopTest = false;

this.stopTestNow = false;

this.sampleCount = 1;

this.bytes = 0L;

this.headersSize = 0;

this.bodySize = 0L;

this.groupThreads = 0;

this.allThreads = 0;

this.elapsedTime = 0L;

this.useNanoTime = nanoTime;

this.nanoThreadSleep = nanoThreadSleep;

this.nanoTimeOffset = this.initOffset();

}

4 实例:图片下载

public class DownloadPicSample extends AbstractJavaSamplerClient {

private DataInputStream dataInputStream;

private String localFoldStr;

@Override

public Arguments getDefaultParameters() {

Arguments arguments = new Arguments();

arguments.addArgument("PicLink", "");

arguments.addArgument("Local_Directory", "");

return arguments;

}

@Override

public void setupTest(JavaSamplerContext context) {

String picLink = context.getParameter("PicLink", "").trim();

try {

URL url = new URL(picLink);

dataInputStream = new DataInputStream(url.openStream());

} catch (java.io.IOException e) {

dataInputStream = null;

}

localFoldStr = context.getParameter("Local_Directory").trim();

File localFold = new File(localFoldStr);

if (! localFold.exists() || ! localFold.isDirectory()) {

localFoldStr = null;

}

}

@Override

public SampleResult runTest(JavaSamplerContext javaSamplerContext) {

SampleResult sampleResult = new SampleResult();

if(dataInputStream == null || localFoldStr == null) {

sampleResult.setResponseData("参数配置错误", "UTF-8");

return sampleResult;

}

if(localFoldStr.endsWith(File.separator))

localFoldStr.substring(0, localFoldStr.length()-1);

String localPicFile = localFoldStr + File.separator + System.currentTimeMillis() + ".jpg";

FileOutputStream outputStream = null;

try {

outputStream = new FileOutputStream(new File(localPicFile), false);

byte[] buffer = new byte[1024];

int length;

while((length = dataInputStream.read(buffer)) > 0) {

outputStream.write(buffer, 0, length);

}

sampleResult.setResponseOK();

sampleResult.setResponseData(localPicFile, "UTF-8");

} catch (java.io.IOException e) {

sampleResult.setResponseData("IO读写失败", "UTF-8");

} finally {

try {

outputStream.close();

dataInputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return sampleResult;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值