【发布时间】:2011-04-05 08:12:08
【问题描述】:
我需要为我的服务层编写压力测试。我开始使用 Unitils、JUnit4 和 JUnitPerf 来做这件事,但没有运气。这就是我决定尝试 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,一切正常。但是,每当我将其更改为多线程时,我都会收到异常,提示会话已关闭。
【问题讨论】:
标签: multithreading unit-testing hibernate testng stress-testing