dubbo接口的性能测试脚本
使用loadrunner编写dubbo接口的性能测试脚本
通过读取xml配置文件的方式来初始化,因此对脚本做了简单的修改。
将spring框架和dubbo相关的jar包拷贝到loadrunner脚本文件夹下,classpath里增加当前目录,applicationContext.xml也拷贝到脚本文件夹下,里面定义好dubbo的provider信息,loadrunner脚本内容如下:
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.unj.dubbotest.provider.DemoService;
import lrapi.lr;
public class Actions
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
DemoService demoService;
public int init() throws Throwable {
demoService = (DemoService) context.getBean("demoService");
return 0;
}//end of init
public int action() throws Throwable {
lr.start_transaction("dubbo");
String hello = demoService.sayHello("dio");
System.out.println(hello);
lr.end_transaction("dubbo",lr.PASS);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
使用jmeter封装java request同样可以实现dubbo类接口的调用
测试Dubbo框架下的接口,lib下要导入Dubbo框架的jar,还有需要测试的接口的jar包,jmeter测试脚本如下:
package cn.test.dubbo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.li.domain.Product;
import com.li.service.ProductService;
public class MyDubboTest {
public static void main(String[] args) {
//1、首先启动Dubbo框架,并且加载applicationContext.xml配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、直接获取远程接口的对象(由框架自动完成)
//后面那个productService为配置文件中zookeeper获取的远程接口对象的实例(id=“···”)
ProductService productService = (ProductService) context.getBean("productService");
//3、调用接口对象的方法
Product product = productService.getProductByPid(1);
if (product == null){
System.out.println("事物失败");
}else{
System.out.println("事物成功");
System.out.println(product);
}
}
}
先写一个抽象类来继承AbstractJavaSamplerClient,里面只写动态调用的方法,其它方法还是在用例类里面重写。
package com.huangjie.dubbo_Consumer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public abstract class AbstractServiceClient extends AbstractJavaSamplerClient{
public Object invokeRunTest(String testName, JavaSamplerContext context , SampleResult sampleResult){
Method[] methods = this.getClass().getMethods();
for (Method method : methods) {
if (method.getName().equalsIgnoreCase(testName)) {
try {
return method.invoke(this, context, sampleResult);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
return null;
}
}
测试用例类继承上面的动态方法调用的抽象类
package com.huangjie.dubbo_Consumer;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.huangjie.dubbo_Service.service.IProcessData;
public class DynamicMethodInvokeTest extends AbstractServiceClient{
private static String label_name = "dubbo_consumer";//定义label名称,显示在jmeter的结果窗口
private static final ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("classpath*:applicationConsumer.xml");
private static IProcessData demoService = null;
@Override
public void setupTest(JavaSamplerContext context) {
//定义测试初始值,setupTest只在测试开始前使用
super.setupTest(context);
demoService = (IProcessData) cxt.getBean("demoService");
}
@Override
public Arguments getDefaultParameters() {
//参数定义,显示在前台,也可以不定义
Arguments params = new Arguments();
params.addArgument("name", "Tom");
params.addArgument("age", "20");
params.addArgument("testName", "setAge");
return params;
}
@Override
public SampleResult runTest(JavaSamplerContext context) {
String testName = context.getParameter("testName");
SampleResult sr = new SampleResult();
super.invokeRunTest(testName, context, sr);
return sr;
}
public void setAge(JavaSamplerContext context, SampleResult sr){
boolean success = true;
sr.setSampleLabel("setAge");
sr.sampleStart();
try {
String age = context.getParameter("age");
String msg = demoService.setAge(age);
Thread.sleep(5000);
System.out.println(msg);
sr.setResponseMessage(msg);
sr.setResponseCode("1000");
} catch (Exception e) {
success = false;
}finally{
sr.sampleEnd();
sr.setSuccessful(success);
}
}
public void sayHi(JavaSamplerContext context, SampleResult sr){
boolean success = true;
sr.setSampleLabel("sayHi");
sr.sampleStart();
try {
String name = context.getParameter("name");
String msg = demoService.sayHi("Hi,"+name);
Thread.sleep(5000);
System.out.println(msg);
sr.setResponseMessage(msg);
sr.setResponseCode("1001");
} catch (Exception e) {
success = false;
}finally{
sr.sampleEnd();
sr.setSuccessful(success);
}
}
@Override
public void teardownTest(JavaSamplerContext context){
super.teardownTest(context);
}
}
这里我们新增了一个参数testName,通过这个参数来决定具体调用哪个用例方法。这样,我们就实现了在同一个类里面写不同的测试用例了,感觉优雅多了,哈哈!
想要在Jmeter中执行上面写的测试用例,有以下几个步骤:
1. 把项目打成jar包,方法1用eclipse或者myeclipse的export直接导出jar包,方法2用maven的package或这install指令打包,这里建议用maven来管理项目,打包方便并且不会出错,我试过用IDE直接export出jar包,但执行的过程中报找不到spring的bean配置文件,用maven打包没有出现这种情况。
2.将打好的jar包放到Jmeter的lib/ext路径下,运行Jmeter,在新建的Java请求Sampler中就能找到自己写的测试用例类了。
3.把项目依赖的所有jar包拷贝到Jmeter的lib路径下,因为跑自己的测试用例可能用到这些jar中的类,否则执行过程中会报找不到class。如果是maven项目,则可以通过执行指令mvn dependency:copy-dependencies 把项目中所有依赖的jar导出到项目的target/dependency文件夹中,然后拷贝到Jmeter的lib下。
4.运行Jmeter按照具体的性能指标和场景,开始创建你的测试计划吧。
参考链接:http://www.51testing.com/html/39/5939-3709930.html
http://www.baidu.com/link?url=kGd1gvzh_VLlgYCS0imkEhgIDVn4q6wfQh6kaN8FSCsRAM6QBxW29w3VZftUp1j6Ck2_yDGZjj1PgdLB3vnd3q&wd=&eqid=e71cff640000020d000000055ebcae58
http://www.cnblogs.com/beetle-shu/p/4086026.html