Java并发测试扩展插件JunitPerf的使用

来源:互联网 发布:网络赚钱是真的吗 编辑:程序博客网 时间:2024/05/16 01:56
        首先,不得不承认一个悲剧的事实,Junit4.0根本不支持并发测试。于是,我从网上挖掘到两个可以用于并发测试的eclipse插件。一个是GroboUtils-5,另一个是JunitPerf。由于某些原因,GroboUtils-5的教程和资源都被墙掉,所以目前我只掌握了JunitPerf进行并发测试。大家看了这篇文章之后,一定能掌握JunitPerf的测试方法。

1.下载JunitPerf插件
要想获得该插件,最方便的办法就是进入maven,去下载该插件。一般能在maven上找到的资源都是可以良好运行的。
在这里,已经找好了。
http://www.mvnrepository.com/artifact/junitperf/junitperf

2.将下载下来的JunitPerf插件导入你要的工程
导入方法为,进入maven,点对应的版本,如下图所示。

点击后,获取它给予的xml代码,将其加入到你指定工程中的pom.xml文件中,maven就会自动导入jar文件。

3.使用junitPerf进行并发测试
在这里就可以进行并发测试了,可以通过阅读已经给的test代码来书写测试代码,但是我提供一种简单的模板来帮助大家进行并发测试。

package test.framework.io;

import junit.framework.TestCase;
import junit.framework.TestSuite;

import com.clarkware.junitperf.TestMethodFactory;
import com.clarkware.junitperf.TimedTest;
import com.uni2uni.framework.io.DirectoryUtil;
//注意要继承TestCase类
public class DirectoryUtilTest extends TestCase{
    //由于继承了TestCase类,所以在这里要使用super语句  带参
 public DirectoryUtilTest(String name){
     super(name); 
 }
public void testCreate() {
String directoryName ="c:/test";
assertEquals(true,DirectoryUtil.create(directoryName));
                       //assertEquals用于单元测试在这里用于支持并发测试 通过assert来实现是否是我们期望的结果。左边参数为期望结果,右边为你要测试的方法。
}

public static void main(String[] args) {
TestSuite suite = new TestSuite();
//1.create
//性能测试  TestMethodFactory参数分别为测试类和测试方法。注意,测试方法的命名应以test为前缀。TimedTest另一整型参数为期望时间
suite.addTest(new TimedTest(new TestMethodFactory(DirectoryUtil.class,"testCreate"),1)); 
//并发测试 与性能测试参数一致,另一整型参数为并发数量
//suite.addTest(new LoadTest(new TestMethodFactory(DirectoryUtil.class,"testCreate"),10));
     junit.textui.TestRunner.run(suite);
}
}

编写完成后,Run->Application 在控制台就会输出结果。

    如图就是结果,显然性能测试失败,超出了期望时间。在这里就需要你去修改原方法或者修改测试的时间参数。


0 0