Saturday, 15 August 2015

java - dynamic annotation for junitbenchmark -



java - dynamic annotation for junitbenchmark -

to time write junitbenchmark tests. want set warmup , test rounds wrapper class. how set benchmarkoptions annotation wrapper?

my wrapper class:

class="lang-java prettyprint-override">import org.junit.runner.junitcore; import org.junit.runner.result; public class wrapper { public static void main(string[] args) { junitcore junit = new junitcore(); result result; result = junit.run(test.class); } }

my test method in test.class:

class="lang-java prettyprint-override">@benchmarkoptions(benchmarkrounds = 50, warmuprounds = 10) @test public void test1() { //something }

first of, code not work.

you missing benchmarkrule in test. you cannot name class test when importing annnotation named test. not compile.

therefore name class benchmarktest.

getting question, can create utilize of benchmarkoptionssystemproperties. in its' documentation written

global settings benchmarks set through scheme properties. if ignore_annotation_options_property specified, scheme properties , defaults take precedence on method- , class-level annotations.

this allows write wrapper follows

class="lang-java prettyprint-override">import org.junit.runner.junitcore; import org.junit.runner.result; import com.carrotsearch.junitbenchmarks.benchmarkoptionssystemproperties; public class wrapper { public static void main(string[] args) { system.setproperty(benchmarkoptionssystemproperties.ignore_annotation_options_property, "true"); system.setproperty(benchmarkoptionssystemproperties.warmup_rounds_property, "20"); system.setproperty(benchmarkoptionssystemproperties.benchmark_rounds_property, "20"); junitcore junit = new junitcore(); result result = junit.run(benchmarktest.class); } }

the according benchmark this

class="lang-java prettyprint-override">import org.junit.rule; import org.junit.test; import org.junit.rules.testrule; import com.carrotsearch.junitbenchmarks.benchmarkoptions; import com.carrotsearch.junitbenchmarks.benchmarkoptionssystemproperties; import com.carrotsearch.junitbenchmarks.benchmarkrule; public class benchmarktest { @rule public testrule benchmarkrun = new benchmarkrule(benchmarkoptionssystemproperties.getdefaultconsumers()); @test @benchmarkoptions(benchmarkrounds = 1, warmuprounds = 1) public void test1() { int tmp = 1 + 2; } }

when execute mein-method of wrapper output, can see annotation values of 1 have been overridden.

benchmarktest.test1: [measured 20 out of 40 rounds, threads: 1 (sequential)] round: 0.00 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], gc.calls: 0, gc.time: 0.00, time.total: 0.01, time.warmup: 0.00, time.bench: 0.00

java dynamic junit annotations

No comments:

Post a Comment