单元级性能测试框架——JunitPerf

发表于:2010-11-18 11:39

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:nb_bull    来源:51Testing软件测试博客

  5.其他

  JunitPerf是一个比较初级的性能测试框架,由JunitPerf装饰的测试都通过JUnit框架运行,所以就存在额外的消耗,特别是在利用fixture时。由于JUnit本身用一个setUp和一个tearDown()方法装饰所有测试样例,所以要在测试场景的整个上下文中考虑执行时间。相应地,我经常创建使用我想要的fixture逻辑的测试,但也会运行一个空白测试来确定性能指数基线。这是一个大致的估计,但它必须作为基线添加到任何想要的测试限制中。

 protected void setUp() {
 }
 protected void tearDown() {
 }
 public void testNothing{
//do nothing
}

  特别还需要注意的,如果要采用Junit4.x中采用annotation新特性编写的测试用例来执行性能,还需要对JunitPerf增加一点改造,如下

class JUnit4TestFactory extends TestFactory {  
 static class DummyTestCase extends TestCase {     
  public void test() {
  }
  }
 private Class<?> junit4TestClass;
 public JUnit4TestFactory(Class<?> testClass) {
  super(DummyTestCase.class);
  this.junit4TestClass = testClass;
  }
 @Override  
 protected TestSuite makeTestSuite() {
  JUnit4TestAdapter unit4TestAdapter = new JUnit4TestAdapter(this.junit4TestClass);
  TestSuite testSuite = new TestSuite("JUnit4TestFactory");
  testSuite.addTest(unit4TestAdapter);
  return testSuite;
  }
 }

  Junit4.x测试脚本

public class ExampleTestCase extends TestCase {
@Before
 protected void setUp() {
 }
@After
 protected void tearDown() {
 }
@Test
 public void testOneSecondResponse() throws Exception {
  Thread.sleep(1000);
 }

  采用JunitPerf执行性能测试

public class ExampleTimedTest2 {
public static final long toleranceInMillis = 100;
    public static Test suite() {
     long maxElapsedTimeInMillis = 1000+toleranceInMillis;
     JUnit4TestFactory mytestfactory = new JUnit4TestFactory(ExampleTestCase.class);
     Test testCase = mytestfactory.makeTestSuite();
     Test timedTest = new TimedTest(testCase, maxElapsedTimeInMillis);
     return timedTest;
     }
 public static void main(String args[]) {
  junit.textui.TestRunner.run(suite());
 }
}

  试试吧!

  用JUnitPerf进行性能测试无疑是一门严格的科学,但在开发生命周期的早期,这是确定和监控应用程序代码的低端性能的极佳方式。另外,由于它是一个基于装饰器的JUnit扩展框架,所以可以很容易地用JUnitPerf装饰现有的JUnit测试。

  想想您已经花了这么多时间来担心应用程序在负载下会怎样执行。用JUnitPerf进行性能测试可以为您减少担忧并节省时间,同时也确保了应用程序代码的质量。

版权声明:本文出自 nb_bull 的51Testing软件测试博客:http://www.51testing.com/?23978

原创作品,转载时请务必以超链接形式标明本文原始出处、作者信息和本声明,否则将追究法律责任。

33/3<123
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号