0

我需要爲我的服務層寫一個壓力測試。我開始用Unitils,JUnit4和JUnitPerf來做,但沒有運氣。這就是爲什麼我決定嘗試TestNG,但是當嘗試以多線程模式運行測試時,會話關閉和事務有問題。如何用TestNG編寫服務層壓力測試?

我使用下面的框架堆棧:spring,hibernate的,TestNG的,Unitils

我需要一個非事務性的多線程測試。測試將調用服務層方法。每次通話都會開始新的交易。所以有幾個線程可以同時啓動多個事務。

所以這個測試應該是這樣的:

public class ServiceStressTest extends UnitilsTestNG 
    @SpringBeanByType 
    private MyService myService; 

    @DataSet 
    @Test 
    public void createTestData() { 
    } 

    @Test(invocationCount = 100, threadPoolSize = 50, dependsOnMethods = "createTestData") 
    public void test1() throws Exception { 
     myService.method1(); // Starts new transaction and commit it. 
     myService.method2(); // Starts new transaction and commit it. 
    } 
} 

,服務看起來如下:

... 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 
... 
@Transactional 
@Service 
public class MyServiceImpl implements MyService { 
    public void method1(){ 
     sessionFactory.getCurrentSession().save(smth); (update/delete) 
    } 
    public void method2(){ 
     sessionFactory.getCurrentSession().save(smth); (update/delete) 
    } 
} 

如果我設置threadPoolSize 1一切工作正常。但是,無論何時我將其更改爲多線程,我都會得到說明會話已關閉的異常。

回答

0

這是我們需要了解的所有代碼嗎?這聽起來像你可能有一個@BeforeMethod或@AfterMethod關閉會話。

或者這是由我不熟悉的其他註釋之一完成的(@Transactional也許?)。

+0

我想知道爲什麼會話和新交易不打開每個服務調用 – Zalivaka 2011-04-06 06:53:33