feat: JMHExample06
This commit is contained in:
parent
7ecc283972
commit
690d9c99e7
|
@ -0,0 +1,50 @@
|
|||
package cn.bunny;
|
||||
|
||||
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.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@Fork(1)
|
||||
@Warmup(iterations = 5)
|
||||
@Measurement(iterations = 10)
|
||||
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||
@Threads(5)
|
||||
@State(Scope.Benchmark)
|
||||
public class JMHExample06 {
|
||||
|
||||
private Map<Long, Long> concurrentMap;
|
||||
private Map<Long, Long> synchronizedMap;
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options options = new OptionsBuilder()
|
||||
.include(JMHExample06.class.getSimpleName())
|
||||
.build();
|
||||
|
||||
new Runner(options).run();
|
||||
}
|
||||
|
||||
@Setup
|
||||
public void setUp() {
|
||||
concurrentMap = new ConcurrentHashMap<>();
|
||||
synchronizedMap = Collections.synchronizedMap(new HashMap<>());
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testConcurrencyMap() {
|
||||
concurrentMap.put(System.nanoTime(), System.nanoTime());
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testSynchronizedMap() {
|
||||
synchronizedMap.put(System.nanoTime(), System.nanoTime());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue