package atomic;

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;
import java.util.concurrent.atomic.AtomicInteger;

@Measurement(iterations = 10)
@Warmup(iterations = 10)
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class AtomicExample02 {

    private AtomicInteger atomicInteger;

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

    @Setup(Level.Iteration)
    public void setup() {
        this.atomicInteger = new AtomicInteger(0);
    }

    @Benchmark
    public void testSet() {
        this.atomicInteger.set(10);
    }

    @Benchmark
    public void testLazySet() {
        this.atomicInteger.lazySet(10);
    }
}
