qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          多線程測試工具groboutils的使用

           一直使用junit做為服務測試框架,感覺不錯。最近有人反映在高并發(fā)的情況下,存在服務調(diào)不到。無奈再次打開單元測試模擬高并發(fā)的情況,卻發(fā)現(xiàn)junit不支持并發(fā)測試
            引入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; // 測試線程總數(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);
          }
          // 生成測試線程運行器
          MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(test);
          // 運行測試線程
          mttr.runTestRunnables();
          long used = System.currentTimeMillis() - start;
          System.out.printf("%s 調(diào)用花費 %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);
          }
          }

           運行代碼,并發(fā)數(shù)開到100個后觀察運行時間發(fā)現(xiàn)運行運行時間到了12秒了,看來問題出在DAO。需要進行sql代碼優(yōu)化了
            導入的測試包有:
          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;

          posted on 2013-11-13 10:19 順其自然EVO 閱讀(445) 評論(0)  編輯  收藏 所屬分類: jmeter and badboy

          <2013年11月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 呼玛县| 西和县| 漾濞| 肇源县| 延安市| 常州市| 雷州市| 理塘县| 西城区| 东海县| 克什克腾旗| 南丹县| 慈利县| 县级市| 梅州市| 郎溪县| 新巴尔虎右旗| 海阳市| 哈尔滨市| 乐亭县| 革吉县| 民权县| 大同县| 炉霍县| 兴化市| 孟连| 大冶市| 黑龙江省| 陕西省| 嘉峪关市| 济阳县| 江达县| 阿拉善左旗| 江川县| 明星| 平山县| 临泉县| 阳谷县| 巴楚县| 聂拉木县| 共和县|