(一)认识 p-unit : 一款开源的性能测试工具

发表于:2007-7-30 13:44  作者:张黄瞩   来源:网络转载

字体: | 上一篇 | 下一篇 |我要投稿 | 推荐标签:

参数化测试案例

        性能测试,不同于单元测试,经常要求测试不同数量级在同一个测试场景中的表现,JUnit 是一款非常优秀的单元测试工具,但没覆盖到这个方面。比如我们比较类库 Foo1 的方法 bar() 和类库 Foo2 的方法 bar() 哪个更符合自己的应用程序,我们需要测试该函数在应用程序可能的数量级的范围内的表现。有经验的开发者知道经常碰到在小数量级 A 更好大数量级 B 更好的局面,因此全面的测试对于代码的性能理解非常重要,能帮助开发者做出正确的决定。p-unit 支持将参数传给测试方法,测试案例需要实现 p-unit 的 parameterizable 接口,该接口的主要方法是返回一组参数列表,这组列表的参数将会一一传给测试方法。


        清单 7. p-unit 参数化测试案例
public class ParamTestClass implements Parameterizable {

 public static void main(String[] args) {
  new PUnitSoloRunner().run(ParamTestClass.class);
 }
 
 public Parameter[] parameters() {
  return new Parameter[] { new ParameterImpl(10), new ParameterImpl(20) };
 }

 public void testA(ParameterImpl param) {
  SampleUtil.doSomething();
 }
 
 public void testB(ParameterImpl param) {
  SampleUtil.doSomething();
 }
 
 public void testC(ParameterImpl param) {
  SampleUtil.doSomething();
 }
 
 public void setUpAfterWatchers(Parameter param) throws Exception {

 }

 public void setUpBeforeWatchers(Parameter param) throws Exception {

 }

 public void tearDownAfterWatchers(Parameter param) throws Exception {

 }

 public void tearDownBeforeWatchers(Parameter param) throws Exception {

 }

 static class ParameterImpl implements Parameter {
  private int _count;

  ParameterImpl(int count) {
   _count = count;
  }

  public int count() {
   return _count;
  }
  
  public String toString() {
   return String.valueOf(_count);
  }
 }
}

 


        上述代码的执行结果为:


        清单 8. p-unit 参数化测试案例输出
[solo] Started running samples.ParamTestClass
samples.ParamTestClass
testA(10) - [57936.0bytes,447.0ms]
testA(20) - [33128.0bytes,61.0ms]
testB(10) - [24832.0bytes,137.0ms]
testB(20) - [0.0bytes,63.0ms]
testC(10) - [83560.0bytes,468.0ms]
testC(20) - [16528.0bytes,47.0ms]
total: 6, failures:0 (GREEN) 1450.0ms
 


        从上述结果看出,每个方法被执行了 2 次,每次传入不同的参数。多线程运行参数化测试程序?相信读者已经明白怎么去实现了,只需将 PUnitSoloRunner 替换成 PUnitConcurrentRunner。

运行环境测试案例

        随着 Java 开源,出现了更多的 Java 运行环境,除了 SUN 的参考实现外,BEA、IBM 均有自己的 Java 运行环境,更有如 Apache Harmony 的开源运行环境(尽管现在 Apache Harmony 尚不能称为 Java 运行环境)。运行环境测试案例,为运行环境开发者以及选择运行环境,都能提供一定的帮助。比如说下面的例子就是测试 java.util.ArrayList 和 java.util.Vector 在两个不同运行环境的表现。测试案例写法和普通的测试案例完全一样,我们只需告诉 p-unit 不同的运行环境的 Java 路径以及正确的 classpath,然后调用 runVMs 函数即可:


        清单 9. p-unit 运行环境测试案例
public static void main(String[] args) {
 PUnitSoloRunner runner = new PUnitSoloRunner();
 runner.addPUnitEventListener(new OverviewReporter(new ImageRender()));
 runner.runVMs(ListTestClass.class, new VM[] { VMConfig.HARMONY, VMConfig.SUN });
}

public class VMConfig {
 private static String CLASSPATH = " -cp correct_classpath_including_all_jars_and_path";
 private static String HARMONY_PATH = "harmony_path\\bin\\java" + CLASSPATH;
 private static String SUN_PATH = "sun_path\\bin\\java" + CLASSPATH;
 public static VM HARMONY = new VM(HARMONY_PATH, "HARMONY");
 public static VM SUN = new VM(SUN_PATH, "SUN");
}

public class ListTestClass {
 
    private static final int LIST_COUNT = 100000;
    private static Object element = new Object();
 private Random indexGenerator = new Random();;
  
    public void testInsertArrayList() {       
        ArrayList arrayList = new ArrayList(LIST_COUNT);
        insertSequence(arrayList);
        insertRandom(arrayList);
    }
   
    public void testInsertVector() {
     Vector vector = new Vector(LIST_COUNT);
     insertSequence(vector);
     insertRandom(vector);
    }
   
    public void insertSequence(List list) {
        for (int i = 0; i < LIST_COUNT; ++i) {
            list.add(element);
        }
    }
   
    public void insertRandom(List list) {
        for (int i = 0; i < LIST_COUNT; ++i) {
            list.add(indexGenerator .nextInt(LIST_COUNT),element);
        }
    }
   
}
 


        上述代码的运行结果如下:

                                                                                                                            下页链接


【福利】填问卷 送2019精选测试大礼包+接口测试实战课程!
22/2<12

评 论

论坛新帖

顶部 底部


建议使用IE 6.0以上浏览器,800×600以上分辨率,法律顾问:上海瀛东律师事务所 张楠律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2020, 沪ICP备05003035号
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪公网安备 31010102002173号

51Testing官方微信

51Testing官方微博

扫一扫 测试知识全知道