2016-11-11 54 views
0

我有一个测试计划,包含普通HTTP采样器和JSR223采样器的组合。 JSR223我用于通过protobuf协议和HTTP采样器为简单的GET/POST请求执行请求。从HTTPSampler获取HTTPClient以在Beanshell中使用它

在通过SSL协议的测试中,我们已经发现了对JSR223采样提供SSL握手的Nginx的大,由于金额巨大的负荷。问题是,我创建了每个请求一个新的了HTTPClient:

CloseableHttpClient client = vars.getObject("client"); 

CloseableHttpClient client = HttpClients.createDefault(); 

我创建仅此客户端上的初始阶段,它的回用在每一个JSR223样的一个实例固定它所以,现在我们遇到了每个线程使用两个HTTPClient(一个由HTTPSampler使用,一个由JSR223使用)的情况。问题是有没有办法从HTTPSampler中获取HTTPClient以在JSR223中进一步使用它以避免双重握手等。

貌似HTTPSamplers是在测试期间在彼此之间传送savedClient。

回答

1

不幸的是,这样做的没有“好”的方式,所以你的方式似乎是正确的。

理论上你可以使用Java Reflection API获得对同一个HTTPClient实例的访问,但是请记住,任何时候你在某处以某种方式使用反射来解决JMeter限制时,猫会死亡。

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.util.EntityUtils; 
import org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl; 

import org.apache.jmeter.samplers.SampleResult; 

import java.lang.reflect.Field; 
import java.lang.reflect.Method; 


Field samplerImpl = sampler.getClass().getDeclaredField("impl"); 
samplerImpl.setAccessible(true); 
HTTPHC4Impl impl = ((HTTPHC4Impl) samplerImpl.get(sampler)); 
Method method = HTTPHC4Impl.class.getDeclaredMethod("setupClient", URL.class, SampleResult.class); 
method.setAccessible(true); 
URL url = new URL("http://example.com"); 
HttpClient client = (HttpClient) method.invoke(impl, url, new SampleResult()); 
HttpGet get = new HttpGet(); 
get.setURI(url.toURI()); 
HttpResponse response = client.execute(get); 
HttpEntity entity = response.getEntity(); 
log.info("******************* Response *************************"); 
log.info(EntityUtils.toString(entity)); 

演示:

JMeter get HTTPCLient

而且我建议切换到Groovy语言,如果你是通过脚本进行高负荷,检查出Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!一些调查和基准。