【问题标题】:How to write service layer stress test with TestNG?如何用TestNG编写服务层压力测试?
【发布时间】: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


    【解决方案1】:

    这就是我们需要了解的所有代码吗?听起来您可能有一个关闭会话的@BeforeMethod 或@AfterMethod。

    或者这是由我不熟悉的其他注释之一完成的(可能是@Transactional?)。

    【讨论】:

    • 我想知道为什么每次服务调用都没有打开会话和新事务
    猜你喜欢
    • 2017-04-15
    • 1970-01-01
    • 2011-03-24
    • 2012-12-19
    • 2010-09-26
    • 2011-04-16
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多