多线程测试工具groboutils的使用
作者:网络转载 发布时间:[ 2013/11/13 14:09:31 ] 推荐标签:
一直使用junit做为服务测试框架,感觉不错。近有人反映在高并发的情况下,存在服务调不到。无奈再次打开单元测试模拟高并发的情况,却发现junit不支持并发测试
引入groboutils jar包,其实我主要使用MultiThreadedTestRunner类和TestRunnable类。
原有的junit框架不做改变,导入GroboTestingJUnit-1.2.1-core.jar包
代码如下
public class FaultServiceTest extends TestCase {
/**
* @param args
* @throws FaultException
* @throws ExpParamNotFoundException
* @throws ParseException
*/
private IFaultService faultService;
private static final int NUM_THREAD = 100; // 测试线程总数
public FaultServiceTest() {
super();
IInitService initService = (IInitService) CustomBeanFactory
.getBean("initService");
initService.initSiteDatabase();
this.faultService = (IFaultService) CustomBeanFactory
.getBean("faultService");
}
public FaultServiceTest(String name) {
super(name);
IInitService initService = (IInitService) CustomBeanFactory
.getBean("initService");
initService.initSiteDatabase();
this.faultService = (IFaultService) CustomBeanFactory
.getBean("faultService");
}
// 高并发测试
public void testGetEquipEventAlertListByPage() throws Throwable {
EquipmentQueryBean equipmentQueryBean = new EquipmentQueryBean();
// 生成所有测试线程
TestRunnable[] test = new TestRunnable[NUM_THREAD];
long start = System.currentTimeMillis();
for (int i = 0; i < test.length; i++) {
test[i] = new FaultServiceThread(faultService, equipmentQueryBean);
}
// 生成测试线程运行器
MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(test);
// 运行测试线程
mttr.runTestRunnables();
long used = System.currentTimeMillis() - start;
System.out.printf("%s 调用花费 %s milli-seconds.
", NUM_THREAD, used);
}
public static Test suite() {
TestSuite test = new TestSuite("HealthService接口类测试");
test.addTest(new FaultServiceTest("testGetEquipEventAlertListByPage"));
return test;
}
/*
* 测试线程类定义
*/
private static class FaultServiceThread extends TestRunnable {
private IFaultService faultService;
private EquipmentQueryBean equipmentQueryBean;
public FaultServiceThread(IFaultService faultService,
EquipmentQueryBean equipmentQueryBean) {
super();
this.faultService = faultService;
this.equipmentQueryBean = equipmentQueryBean;
}
@Override
public void runTest() throws Throwable {
faultService.getEquipEventAlertListByPage(equipmentQueryBean);
}
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11