package io.morethan.javabenchmarks.showcase.threads;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Group;
import org.openjdk.jmh.annotations.GroupThreads;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

/**
 * Benchmarks using multithreaded JMH facilities.
 */
@Fork(value = 1)
@Warmup(iterations = 0)
@Measurement(iterations = 5)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Group)
public class MultithreadedBenchmark {

    AtomicLong _counter = new AtomicLong();

    @Group("withThreads1")
    @GroupThreads(1)
    @Benchmark
    public void withThreads1() throws Exception {
        _counter.incrementAndGet();
    }

    @Group("withThreads2")
    @GroupThreads(2)
    @Benchmark
    public void withThreads2() throws Exception {
        _counter.incrementAndGet();
    }

    @Group("withThreads4")
    @GroupThreads(4)
    @Benchmark
    public void withThreads4() throws Exception {
        _counter.incrementAndGet();
    }

}

@@ -891,16 +891,24 @@
  %7B%0A%0A    
+private 
 AtomicLo
@@ -956,24 +956,23 @@
 up(%22with
+1
 Thread
-s1
 %22)%0A    @
@@ -1022,20 +1022,315 @@
 oid 
-withThreads1
+increment1() throws Exception %7B%0A        _counter.incrementAndGet();%0A    %7D%0A%0A    @Group(%22with1Thread%22)%0A    @GroupThreads(1)%0A    @Benchmark%0A    public long read1() throws Exception %7B%0A        return _counter.get();%0A    %7D%0A%0A    @Group(%22with2Threads%22)%0A    @GroupThreads(2)%0A    @Benchmark%0A    public void increment2
 () t
@@ -1406,24 +1406,24 @@
 up(%22with
+2
 Threads
-2
 %22)%0A    @
@@ -1468,25 +1468,167 @@
 lic 
-void 
+long read2() throws Exception %7B%0A        return _counter.get();%0A    %7D%0A%0A    @Group(%22
 with
+4
 Threads
-2
+%22)%0A    @GroupThreads(4)%0A    @Benchmark%0A    public void increment4
 () t
@@ -1704,24 +1704,24 @@
 up(%22with
+4
 Threads
-4
 %22)%0A    @
@@ -1766,24 +1766,17 @@
 lic 
-void withTh
+long 
 read
-s
 4() 
@@ -1794,32 +1794,39 @@
 ption %7B%0A        
+return 
 _counter.increme
@@ -1814,37 +1814,25 @@
 rn _counter.
-incrementAndG
+g
 et();%0A    %7D%0A

Improve sample benchmark