1.前言
在应用程序的开发周期中,性能测试常被放到最后考虑,这并不是因为它不重要,而是因为存在这么多未知变量,很难有效地测试。由于种种原因,性能测试常被延迟到开发周期的后期。以我的经验,企业之所以在开发过程中不包含性能测试是因为,他们不知道对于正在进行开发的应用程序要期待什么。
发生下列两种情况之一时,性能测试就成为头等大事:1.生产中出现显而易见的性能问题;2.在产品交付之前,客户或潜在客户询问有关性能指数的问题。
2.用JUnitPerf进行测试
作为测试工程师执行性能测试时,首先想到的是LoadRunner、Jmeter等系统级性能测试工具,图表丰富,数据详细精确。但在在软件开发的早期阶段,采用此类方法不免有“牛刀杀鸡”之嫌,而且与我们目前的xUnit测试体系结合时还涉及到代码转换的技术处理工作,相对比较费时。前两天和晓峰交流时,网游测试团队也提到了CP方由于对LR这类系统级测试工具使用不惯,而不愿或不能执行性能测试的问题,那么JunitPerf作为Java语言的一个单元性能测试框架,对于Java开发工程师来说,应该是比较适合和易用的初级性能测试框架。
在软件开发的早期阶段,使用JUnitPerf很容易确定基本的低端性能指数。JUnitPerf框架能够将测试快速地转化为简单的负载测试。可使用JUnitPerf创建两种测试类型:TimedTest和LoadTest。这两种类型都基于Decorator设计模式并利用JUnit的suite机制。
*TimedTest为测试样例创建一个(时间)上限——如果超过这个时间,那么测试失败。
*LoadTest和计时器一起运行,它通过运行所需的次数(时间间隔由配置的计时器控制),在一个特定的测试用例上创建一个人工负载。
3.TimedTest时限测试模式
JUnitPerf TimedTest让您可以编写有相关时间限制的测试——如果超过了该限度,就认为测试是失败的(即便测试逻辑本身实际上是成功的)。在测试对于业务致关重要的方法时,时限测试相比其他测试来说,在确定和监控性能指数方面很有帮助。
创建TimedTest首先要创建一个标准的JUnit测试。换言之,将对TestCase或其派生类进行扩展,并编写一个以test开头的方法,如清单1所示:
public class ExampleTestCase extends TestCase { public ExampleTestCase(String name) { super(name); } protected void setUp() { } protected void tearDown() { } public void testOneSecondResponse() throws Exception { Thread.sleep(1000); } public static Test suite() { return new TestSuite(ExampleTestCase.class); } public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } } |
由于JUnitPerf是一个基于装饰器的框架,为了真正地驾驭它,必须提供一个suite()方法并将现有的测试装饰以TimedTest。TimedTest以Test和执行该测试的最大时间量作为参数。也可以选择传入一个boolean标志作为第三个参数(false),这将导致测试快速失败——意味着如果超过最大时间,JUnitPerf将立即迫使测试失败。否则,测试样例将完整运行,然后失败。区别很微妙:在一个失败的样例中,不带可选标志运行测试可以帮您了解运行总时间。传入false值却意味着得不到运行总时间。
清单2.为生成TimedTest而实现的suite方法
public class ExampleTimedTest { public static final long toleranceInMillis = 100; public static Test suite() { long maxElapsedTimeInMillis = 1000 + toleranceInMillis; Test testCase = new ExampleTestCase("testOneSecondResponse"); Test timedTest = new TimedTest(testCase, maxElapsedTimeInMillis); return timedTest; } public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } } |
在运行测试用例时,我设置了1秒+允许误差时间为时间上限,如果执行总时间超过了这个时间,测试样例将失败。由于我并未传入可选的boolean参数,该测试将完整运行,而不管运行会持续多久。
4.LoadTest负载测试模式
使用LoadTest,可以指定要模拟的用户(线程)数量,甚至为这些线程的启动提供计时机制。JUnitPerf提供两类Timer:ConstantTimer和RandomTimer。通过为LoadTest提供这两类计时器,可以更真实地模拟用户负载。如果没有Timer,所有线程都会同时启动。
清单3是用ConstantTimer实现的含10个模拟用户的负载测试:
public class ExampleLoadTest { public static final long toleranceInMillis = 100; public static Test suite() { TestSuite suite = new TestSuite(); // // Pick any example tests below. // suite.addTest(make1SecondResponse10UserLoad1SecondDelayIterationTest()); return suite; } protected static Test make1SecondResponse10UserLoad1SecondDelayIterationTest() {
int users = 10; int iterations = 10; Timer timer = new ConstantTimer(1000); long maxElapsedTimeInMillis = 20000 + toleranceInMillis; Test testCase = new ExampleTestCase("testOneSecondResponse"); Test loadTest = new LoadTest(testCase, users, iterations, timer); Test timedTest = new TimedTest(loadTest, maxElapsedTimeInMillis); return timedTest; } public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } } |
10个用户并发执行测试,间隔时间设置为1秒,各运行10次,并且设置整个场景的最大时间为20秒加允许误差时间,否则整个测试场景将失败。
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进行性能测试可以为您减少担忧并节省时间,同时也确保了应用程序代码的质量。