Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* MAIN CLASS */
- package dk.xakeps.benchmarks.instancespeed;
- import org.openjdk.jmh.annotations.*;
- import org.openjdk.jmh.runner.Runner;
- import org.openjdk.jmh.runner.RunnerException;
- import org.openjdk.jmh.runner.options.Options;
- import org.openjdk.jmh.runner.options.OptionsBuilder;
- import java.util.concurrent.TimeUnit;
- @State(Scope.Thread)
- @BenchmarkMode(Mode.Throughput)
- @OutputTimeUnit(TimeUnit.NANOSECONDS)
- public class Main {
- private static Main instance;
- public static Main getInstance() {
- if(instance == null) {
- instance = new Main();
- }
- return instance;
- }
- @Benchmark
- public void constructorTest() {
- new TestClass(this);
- }
- @Benchmark
- public void instanceTest() {
- TestClass.getInstance();
- }
- public static void main(String[] args) throws RunnerException {
- Options options = new OptionsBuilder()
- .include(Main.class.getSimpleName())
- .warmupIterations(10)
- .measurementIterations(5)
- .forks(5)
- .build();
- new Runner(options).run();
- }
- }
- /* TESTING CLASS */
- package dk.xakeps.benchmarks.instancespeed;
- public class TestClass {
- private Main mainClass;
- private static final class Holder {
- static final TestClass instance = new TestClass();
- }
- private TestClass() {
- }
- public TestClass(Main main) {
- this.mainClass = main;
- }
- public static TestClass getInstance() {
- return Holder.instance;
- }
- }
- /* RESULTS */
- /*
- Result "constructorTest":
- 3,743 ±(99.9%) 0,240 ops/ns [Average]
- (min, avg, max) = (3,208, 3,743, 4,157), stdev = 0,321
- CI (99.9%): [3,503, 3,983] (assumes normal distribution)
- Result "instanceTest":
- 3,762 ±(99.9%) 0,088 ops/ns [Average]
- (min, avg, max) = (3,515, 3,762, 3,919), stdev = 0,117
- CI (99.9%): [3,675, 3,850] (assumes normal distribution)
- # Run complete. Total time: 00:02:32
- Benchmark Mode Cnt Score Error Units
- Main.constructorTest thrpt 25 3,743 ± 0,240 ops/ns
- Main.instanceTest thrpt 25 3,762 ± 0,088 ops/ns
- HIGHER - BETTER
- */
Advertisement
Add Comment
Please, Sign In to add comment