日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多线程测试工具groboutils的使用

發(fā)布時(shí)間:2024/4/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多线程测试工具groboutils的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。