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.LinkedList;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;

@Measurement(iterations = 10)
@Warmup(iterations = 10)
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class AtomicExample09 {
    private final static String DATA = "TEST";
    private final static Object LOCK = new Object();
    private SynchronizedLinkedList synchronizedLinkedList;
    private ConcurrentLinkedQueue<String> concurrentLinkedQueue;

    // 如果使用不当反而会降低性能
    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(AtomicExample09.class.getSimpleName())
                .forks(1)
                .build();
        new Runner(options).run();
    }

    @Setup(Level.Iteration)
    public void setUp() {
        synchronizedLinkedList = new SynchronizedLinkedList();
        concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
    }

    // 测试 SynchronizedLinkedList
    @Group("sync")
    @Benchmark
    @GroupThreads(5)
    public void synchronizedListAdd() {
        synchronizedLinkedList.addLast(DATA);
    }

    @Group("sync")
    @Benchmark
    @GroupThreads(5)
    public String synchronizedListGet() {
        return synchronizedLinkedList.removeFirst();
    }

    // 测试 ConcurrentLinkedQueue
    @Group("concurrent")
    @Benchmark
    @GroupThreads(5)
    public void concurrentLinkedQueueAdd() {
        concurrentLinkedQueue.offer(DATA);
    }

    @Group("concurrent")
    @Benchmark
    @GroupThreads(5)
    public String concurrentLinkedQueueGet() {
        return concurrentLinkedQueue.poll();
    }

    private static class SynchronizedLinkedList {
        private final LinkedList<String> list = new LinkedList<>();

        void addLast(String element) {
            synchronized (LOCK) {
                list.addLast(element);
            }
        }

        String removeFirst() {
            synchronized (LOCK) {
                if (list.isEmpty()) {
                    return null;
                }
                return list.removeLast();
            }
        }
    }
}
// 测试结果
// Benchmark                                            Mode  Cnt  Score   Error  Units
// AtomicExample09.concurrent                           avgt   10  0.221 ± 0.649  us/op
// AtomicExample09.concurrent:concurrentLinkedQueueAdd  avgt   10  0.407 ± 1.296  us/op
// AtomicExample09.concurrent:concurrentLinkedQueueGet  avgt   10  0.034 ± 0.048  us/op
// AtomicExample09.sync                                 avgt   10  0.240 ± 0.039  us/op
// AtomicExample09.sync:synchronizedListAdd             avgt   10  0.232 ± 0.034  us/op
// AtomicExample09.sync:synchronizedListGet             avgt   10  0.248 ± 0.044  us/op