多线程测试工具groboutils的使用
一直使用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.\n", 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);
}
}
}
运行代码,并发数开到100个后观察运行时间发现运行运行时间到了12秒了,看来问题出在DAO。需要进行sql代码优化了
导入的测试包有:
import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
原文地址:https://blog.csdn.net/zhangyaoming2004/article/details/7619489
最新文章
- cocos2dx-3.9 集成admob
- js007-函数表达式
- r.js 前端项目打包
- Java 时间、日期类
- QQ炫舞官网选项卡效果
- CentOS 报no acceptable C compiler found in $PATH的解决办法
- Android手机音量的控制
- $and $not null 正则表达式
- 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
- LeetCode 606 Construct String from Binary Tree 解题报告
- QRCode.js生成二维码
- spring boot重启脚本
- VMWare 安装 Linux
- Python之描述器
- Coursera-AndrewNg(吴恩达)机器学习笔记——第四周
- ExpressMapper- The New .NET Mapper!
- oracle ORA-01722:无效数字 记录
- OpenCV学习(32) 求轮廓的包围盒
- 树链剖分+离散+扫描(HDU5044)
- Notepad++中的高级查找
热门文章
- 字典,set
- 1.WIN10下搭建vue开发环境
- wannafly 练习赛11 B 假的字符串(字典树+建边找环)
- mvn deploy上传私服Nexus时遇到的坑--ReasonPhrase: Forbidden.
- 《Effective Java》读书笔记 - 2.创建和销毁对象
- C#的keyValue
- React Native商城项目实战02 - 主要框架部分(tabBar)
- mysql 5.5 安装教程
- maven scope provided和runtime的例子
- cocos2dx基础篇(17) 音乐音效SimpleAudioEngine