jeudi 7 septembre 2023

Query about using Benchmark test singleton design pattern

local variable increases performance by 25 percent - Joshua Bloch "Effective Java, Second Edition", p. 283-284

I'm new to benchmark test, and i want to do a test for DoubleCheckLockingSingleton. But the result seems like the performance of DoubleCheckLockingSingletonWithoutLocalVar is better?

I'm using https://github.com/openjdk/jmh

The benchmark result:

# JMH version: 1.33
# VM version: JDK 17.0.6, OpenJDK 64-Bit Server VM, 17.0.6+10-b829.9
# VM invoker: /home/repo/idea-IU-231.9011.34/jbr/bin/java
# VM options: -javaagent:/home/repo/idea-IU-231.9011.34/lib/idea_rt.jar=46469:/home/repo/idea-IU-231.9011.34/bin -Dfile.encoding=UTF-8
# Blackhole mode: full + dont-inline hint (default, use -Djmh.blackhole.autoDetect=true to auto-detect)
# Warmup: 5 iterations, 10 s each
# Measurement: 5 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 10 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: org.example.MyBenchmarkTest.measureWithLocalVar

Benchmark                                Mode  Cnt           Score           Error  Units
MyBenchmarkTest.measureWithLocalVar     thrpt    5    14543289.188 ±   1754652.871  ops/s
MyBenchmarkTest.measureWithoutLocalVar  thrpt    5  2238795119.169 ± 942222432.066  ops/s

MyBenchmarkTest file

@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
public class MyBenchmarkTest {
    @Benchmark
    @Threads(10)
    public DoubleCheckLockingSingleton measureWithLocalVar() {
        return  DoubleCheckLockingSingleton.getInstance();
    }

    @Benchmark
    @Threads(10)
    public DoubleCheckLockingSingletonWithoutLocalVar measureWithoutLocalVar() {
        return DoubleCheckLockingSingletonWithoutLocalVar.getInstance();
    }

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(MyBenchmarkTest.class.getSimpleName())
                .forks(1)
                .build();

        new Runner(options).run();
    }
 }

DoubleCheckLocking file

public class DoubleCheckLockingSingleton {
   
    private static volatile DoubleCheckLockingSingleton instance = null;

    public static DoubleCheckLockingSingleton getInstance(){
        DoubleCheckLockingSingleton result = instance;

        if(result ==null){
            synchronized (DoubleCheckLockingSingleton.class){
                result = instance;
                if(result==null){
                    result = new DoubleCheckLockingSingleton();
                }
            }
        }
        return result;
    }
}

public class DoubleCheckLockingSingletonWithoutLocalVar {

    private static volatile DoubleCheckLockingSingletonWithoutLocalVar instance = null;

    public static DoubleCheckLockingSingletonWithoutLocalVar getInstance(){
        if(instance ==null){
            synchronized (DoubleCheckLockingSingletonWithoutLocalVar.class){
                if(instance==null){
                    instance = new DoubleCheckLockingSingletonWithoutLocalVar();
                }
            }
        }
        return instance;
    }
}

Can somebody give me a hand? thanks!

Aucun commentaire:

Enregistrer un commentaire