使用JMeter API调用GET / POST请求

时间:2017-03-17 11:47:29

标签: jmeter

有一个要求,我需要使用Jmeter API调用get / post / put请求。有人建议如何调用get,发布来自JSR 223 / beanshell / java sampler的请求?

尝试使用以下代码。但我不是JMeter API的专家。

import org.apache.jmeter.protocol.http.sampler.*    
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.sample("http://example.com", "GET", true, 0);

1 个答案:

答案 0 :(得分:0)

如果真的需要通过JSR223 Test Elements执行此操作,请参阅示例Groovy代码(使用Apache HttpComponents,请参阅QuickStart更多例子)

import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils

CloseableHttpClient client = HttpClientBuilder.create().build()
HttpGet get = new HttpGet("http://10.195.78.185")
HttpResponse response = client.execute(get)

HttpEntity entity = response.getEntity()
byte [] responseData = EntityUtils.toByteArray(entity)
def statusCode = response.getStatusLine().statusCode
if (statusCode >= 400) {
    SampleResult.setSuccessful(false)
}
SampleResult.setResponseCode(statusCode as String)
SampleResult.setResponseMessage(response.getStatusLine().reasonPhrase)
SampleResult.setResponseData(responseData)

但我建议不要重新发明轮子并使用HTTP Request采样器。与HTTP Header Manager结合使用,您可以将其用于任何基于HTTP的API的负载测试,JMeter将自动关注收集延迟,空闲时间,连接时间,处理响应代码等指标。请参阅{{3}文章了解更多详情。