参考:http://zhanghuangzhu.blogspot.com/search/label/p-unit
http://blog.csdn.net/zhanghuangzhu/
从https://sourceforge.net/projects/p-unit下载.
Samples文件编译runner.runVMs失败,不再支持runVMs.
加入libs下jar以及p-unit-0.15.319.jar, p-unit-0.15.319-extension.jar
从执行结果看,p-unit与junitperf是不同的。P-unit有内存消耗图片。
Junitperf并发执行控制粒度控制更加细致。
ParamTestClass.java内容:
packagesamples;
importorg.punit.reporter.chart.OverviewReporter;
importorg.punit.reporter.chart.image.ImageRender;
importorg.punit.reporter.chart.pdf.PDFRender;
importorg.punit.reporter.stream.file.FileLogger;
importorg.punit.runner.*;
importorg.punit.type.*;
importorg.punit.watcher.MemoryWatcher;
publicclassParamTestClassimplementsParameterized {
publicstaticvoidmain(String[] args) {
ConcurrentRunner runner =newConcurrentRunner();
//加入内存消耗监控
runner.methodRunner().addWatcher(newMemoryWatcher());
runner.addEventListener(newFileLogger());
runner.addEventListener(newOverviewReporter(newImageRender()));
runner.addEventListener(newOverviewReporter(newPDFRender()));
//利用多核特性
runner.setExecutorPool(newExecutorPoolImpl(2));
runner.run(ParamTestClass.class);
}
publicParameter[] parameters() {
returnnewParameter[] {newParameterImpl(2),newParameterImpl(10),
newParameterImpl(30) };
}
publicvoidtestA(ParameterImpl param) {
System.out.println("testA count = "+ param.count());//$NON-NLS-1$
SampleUtil.doSomething(param.count());
}
publicvoidsetUpAfterWatchers(Parameter param)throwsException {
}
publicvoidsetUpBeforeWatchers(Parameter param)throwsException {
}
publicvoidtearDownAfterWatchers(Parameter param)throwsException {
}
publicvoidtearDownBeforeWatchers(Parameter param)throwsException {
}
staticclassParameterImplimplementsParameter {
privateint_count;
ParameterImpl(intcount) {
_count= count;
}
publicintcount() {
return_count;
}
publicString toString() {
returnString.valueOf(_count);
}
}
}
SampleUtil.java内容
packagesamples;
import java.util.*;
import org.punit.util.*;
public class SampleUtil {
private static Random _random = new Random();
public static void consumeMemory(int length) {
byte[] data = new byte[length];
for(int i = 0, j = 0; i < data.length; ++i) {
++j;
}
}
public static void consumeTime(int time) {
ThreadUtil.sleepIgnoreInterruption(time);
}
public static void doSomething() {
consumeTime(Math.abs(_random.nextInt()) % 500);
consumeMemory(Math.abs(_random.nextInt()) % 100000);
}
public static void doSomething(int count) {
for(int i=0;i<count;i++)
{
consumeTime(Math.abs(_random.nextInt()) % 500);
consumeMemory(Math.abs(_random.nextInt()) % 100000);
}
}
}
控制台执行时间,与内存消耗值。
testA(30) - [9741.979497ms,504864.0bytes]
total: 3, failures:0 (GREEN) - 16922.606365ms
执行目录下result下生成pdf,txt,png格式的文件结果