<div class="htmledit_views" id="content_views">html
一直使用junit作爲服務測試框架,感受不錯。最近有人反映在高併發的狀況下,存在服務調不到。無奈再次打開單元測試模擬高併發的<br> 狀況,卻發現junit不支持併發測試<br> 引入groboutils jar包,其實我主要使用MultiThreadedTestRunner類和TestRunnable類。<br> 原有的junit框架不作改變,導入GroboTestingJUnit-1.2.1-core.jar包<br> 代碼以下<br> public class FaultServiceTest extends TestCase {<br><br> /**<br> * @param args<br> * @throws FaultException<br> * @throws ExpParamNotFoundException<br> * @throws ParseException<br> /<br> private IFaultService faultService;<br> private static final int NUM_THREAD = 100; // 測試線程總數<br><br> public FaultServiceTest() {<br> super();<br> IInitService initService = (IInitService) CustomBeanFactory<br> .getBean("initService");<br> initService.initSiteDatabase();<br> this.faultService = (IFaultService) CustomBeanFactory<br> .getBean("faultService");<br> }<br><br> public FaultServiceTest(String name) {<br> super(name);<br> IInitService initService = (IInitService) CustomBeanFactory<br> .getBean("initService");<br> initService.initSiteDatabase();<br> this.faultService = (IFaultService) CustomBeanFactory<br> .getBean("faultService");<br> }<br> // 高併發測試<br> public void testGetEquipEventAlertListByPage() throws Throwable {<br> EquipmentQueryBean equipmentQueryBean = new EquipmentQueryBean();<br> // 生成全部測試線程<br> TestRunnable[] test = new TestRunnable[NUM_THREAD];<br> long start = System.currentTimeMillis();<br> for (int i = 0; i < test.length; i++) {<br> test[i] = new FaultServiceThread(faultService, equipmentQueryBean);<br> }<br> // 生成測試線程運行器<br> MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(test);<br> // 運行測試線程<br> mttr.runTestRunnables();<br> long used = System.currentTimeMillis() - start;<br> System.out.printf("%s 調用花費 %s milli-seconds.\n", NUM_THREAD, used);<br> }<br> public static Test suite() {<br> TestSuite test = new TestSuite("HealthService接口類測試");<br> test.addTest(new FaultServiceTest("testGetEquipEventAlertListByPage"));<br> return test;<br> }<br> /<br> * 測試線程類定義<br> */<br> private static class FaultServiceThread extends TestRunnable {<br> private IFaultService faultService;<br> private EquipmentQueryBean equipmentQueryBean;<br><br> public FaultServiceThread(IFaultService faultService,<br> EquipmentQueryBean equipmentQueryBean) {<br> super();<br> this.faultService = faultService;<br> this.equipmentQueryBean = equipmentQueryBean;<br> }<br><br> @Override<br> public void runTest() throws Throwable {<br> faultService.getEquipEventAlertListByPage(equipmentQueryBean);<br> }<br> }<br><p>}</p>sql
<p>運行代碼,併發數開到100個後觀察運行時間發現運行運行時間到了12秒了,看來問題出在DAO。須要進行sql代碼優化了<br></p> 導入的測試包有:<br> import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;<br> import net.sourceforge.groboutils.junit.v1.TestRunnable;<br><br> import junit.framework.Test;<br> import junit.framework.TestCase;<br> import junit.framework.TestSuite;<br> 原文地址:https://blog.csdn.net/zhangyaoming2004/article/details/7619489 </div>併發