多线程测试工具groboutils的使用
http://blog.csdn.net/zhangyaoming2004/article/details/7619489
?? 一直使用junit做為服務(wù)測試框架,感覺不錯(cuò)。最近有人反映在高并發(fā)的情況下,存在服務(wù)調(diào)不到。無奈再次打開單元測試模擬高并發(fā)的
情況,卻發(fā)現(xiàn)junit不支持并發(fā)測試
???? 引入groboutils jar包,其實(shí)我主要使用MultiThreadedTestRunner類和TestRunnable類。
???? 原有的junit框架不做改變,導(dǎo)入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; // 測試線程總數(shù)
?? ?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");
?? ?}
?? ?// 高并發(fā)測試
?? ?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);
?? ??? ?}
?? ??? ?// 生成測試線程運(yùn)行器
?? ??? ?MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(test);
?? ??? ?// 運(yùn)行測試線程
?? ??? ?mttr.runTestRunnables();
?? ??? ?long used = System.currentTimeMillis() - start;
?? ??? ?System.out.printf("%s 調(diào)用花費(fèi) %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);
?? ??? ?}
?? ?}
}
運(yùn)行代碼,并發(fā)數(shù)開到100個(gè)后觀察運(yùn)行時(shí)間發(fā)現(xiàn)運(yùn)行運(yùn)行時(shí)間到了12秒了,看來問題出在DAO。需要進(jìn)行sql代碼優(yōu)化了
導(dǎo)入的測試包有:
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;
總結(jié)
以上是生活随笔為你收集整理的多线程测试工具groboutils的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查看Oracle数据库表空间大小(空闲、
- 下一篇: IE8“开发人员工具”使用详解上(各级菜