Jmeter是个强大性能检测工具,主页java压力测试工具有丰富的介绍
公司开发了一系列规则引擎相关的项目,现用spring的RMI封装完成测试
右键“测试计划”,建立测试组
查看线程组属性(设置并发数,执行数,级线程启动特征
线程数:并发数量
RAMP-up:决定了线程之前的启动间隔时间 RAMP-up/线程数
循环次数:指定了每个线程执行测试方法的次数
右键 “线程组”,建立JAVA请求
了解Jmeter对JAVA请求的支持
jmeter会扫描lib目录下所有的Jar,找出实现org.apache.jmeter.protocol.java.sampler.JavaSamplerClient的所有类形成
下拉列表供你选择,请看上图的“类名称”一栏
其次,还有“同请求一起发送的参数”,下列的参数列表是由JavaSamplerClient.getDefaultParameters()决定的,所以如果需要,可实现它
右键 “线程组”添加监听器,Jmeter提供了方方面面的实现,我们看看 “聚合报告”
了解“聚合报告”重要属性
Average 平均响应时间
min 最快响应时间
max 最慢响应时间
Throughout 吞吐量
90%Line 90%响应时间快于此值(重要参考性)
编写代码,抛钻引玉
package com.skyon.rule.service;
import java.util.HashMap;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor;
import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;
public class RuleServiceTest extends AbstractJavaSamplerClient {
public RuleService ruleService;
public static final String RULE_URI = "rule_uri";
public static final String DEFAULT_RULE = "//project/89a26ce8";
public static final String SERVICE_URL = "service_url";//http://localhost:8080/ruleservice/RuleService";
public static final String DEFAULT_SERVICE = "http://localhost:8080/ruleservice/RuleService";
public static final Class serviceInterface = RuleService.class;
@Override
public void setupTest(JavaSamplerContext context) {
try{
HttpInvokerClientInterceptor interceptor = new HttpInvokerClientInterceptor();
SimpleHttpInvokerRequestExecutor executor = new SimpleHttpInvokerRequestExecutor();
interceptor.setHttpInvokerRequestExecutor(executor);
String serviceURL = context.getParameter(SERVICE_URL);
if(serviceURL == null || "".equals(serviceURL.trim())){
interceptor.setServiceUrl(serviceURL);
}else{
interceptor.setServiceUrl(context.getParameter(SERVICE_URL));
}
interceptor.setServiceInterface(serviceInterface);
ruleService = (RuleService) ProxyFactory.getProxy(serviceInterface, interceptor);
}catch(Exception e){
getLogger().warn("thread " + Thread.currentThread().getId() + "initialize fail", e);
}
}
@Override
public Arguments getDefaultParameters() {
// TODO Auto-generated method stub
Arguments params = new Arguments();//836e0302
params.addArgument(RULE_URI,"//project/89a26ce8");
params.addArgument(SERVICE_URL,"");
return params;
}
public SampleResult runTest(JavaSamplerContext context) {
SampleResult results = new SampleResult();
try {
results.setDataEncoding("UTF-8");
results.sampleStart(); //定义一个事务,表示这是事务的起始点,类似于LoadRunner的 lr.start_transaction
String ruleURI = context.getParameter(RULE_URI);
if(ruleURI == null || "".equals(ruleURI.trim())){
ruleURI = DEFAULT_RULE;
}
ruleService.invoke(ruleURI, new HashMap(), false);
results.setSuccessful(true);
} catch (RuleServiceException e) {
results.setSuccessful(false);
getLogger().error(e.getMessage(), e);
}finally{
results.sampleEnd(); //定义一个事务,表示这是事务的结束点,类似于LoadRunner的 lr.end_transaction
}
return results;
}
}
代码会依赖jmeter相关包ApacheJMeter_core,ApacheJMeter_java,jorphan,logkit,avalon-framework-4.1.4.jar 这些都在jmeter主目录/lib下,代码中也用到了spring Http invoker,当然少不了spring相关包
将该文件及依赖文件打包JAR,copy至jmeter主目录/lib/ext/。同时spring相关包也拷入。重启jmeter。如果代码被正确加载,会显示图4中的下列表中