多线程测试工具groboutils的使用
http://blog.csdn.net/zhangyaoming2004/article/details/7619489
?? 一直使用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;
總結
以上是生活随笔為你收集整理的多线程测试工具groboutils的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查看Oracle数据库表空间大小(空闲、
- 下一篇: IE8“开发人员工具”使用详解上(各级菜