Compare commits
6 Commits
78ff512f26
...
c51c07a7b8
Author | SHA1 | Date |
---|---|---|
|
c51c07a7b8 | |
|
351a6059ac | |
|
0c0ded951b | |
|
35cf842258 | |
|
e327a1ca33 | |
|
c8c2a0e8bc |
|
@ -0,0 +1,72 @@
|
||||||
|
package cn.bunny.jmh;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@Fork(0)
|
||||||
|
@Warmup(iterations = 5)
|
||||||
|
@Measurement(iterations = 5)
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
@State(Scope.Thread)
|
||||||
|
public class JMHExample12 {
|
||||||
|
private final Inc inc1 = new Inc1();
|
||||||
|
private final Inc inc2 = new Inc2();
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
final Options options = new OptionsBuilder()
|
||||||
|
.include(JMHExample12.class.getSimpleName())
|
||||||
|
.build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int measure(Inc inc) {
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
result += inc.inc();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public int measure_inc_1() {
|
||||||
|
return this.measure(inc1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public int measure_inc_2() {
|
||||||
|
return this.measure(inc2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public int measure_inc_3() {
|
||||||
|
return this.measure(inc1);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Inc {
|
||||||
|
int inc();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Inc1 implements Inc {
|
||||||
|
private int i = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int inc() {
|
||||||
|
return ++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Inc2 implements Inc {
|
||||||
|
private int i = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int inc() {
|
||||||
|
return ++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package cn.bunny.jmh;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@Fork(1)
|
||||||
|
@Warmup(iterations = 5)
|
||||||
|
@Measurement(iterations = 5)
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
@State(Scope.Group)
|
||||||
|
public class JMHExample13 {
|
||||||
|
|
||||||
|
private AtomicInteger counter;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
final Options options = new OptionsBuilder()
|
||||||
|
.include(JMHExample13.class.getSimpleName())
|
||||||
|
.build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void setup() {
|
||||||
|
this.counter = new AtomicInteger();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Group("q")
|
||||||
|
@Benchmark
|
||||||
|
public void inc() {
|
||||||
|
this.counter.incrementAndGet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Group("q")
|
||||||
|
@Benchmark
|
||||||
|
public int get() {
|
||||||
|
return this.counter.get();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package cn.bunny.jmh;
|
||||||
|
|
||||||
|
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.BlockingDeque;
|
||||||
|
import java.util.concurrent.LinkedBlockingDeque;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@Fork(1)
|
||||||
|
@Warmup(iterations = 5)
|
||||||
|
@Measurement(iterations = 5)
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
@State(Scope.Group)
|
||||||
|
public class JMHExample14 {
|
||||||
|
private final static int VALUE = Integer.MAX_VALUE;
|
||||||
|
private BlockingDeque<Integer> queue;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
final Options options = new OptionsBuilder()
|
||||||
|
.include(JMHExample14.class.getSimpleName())
|
||||||
|
.build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void init() {
|
||||||
|
this.queue = new LinkedBlockingDeque<>(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 会出现长时间的阻塞最长时间正常是10分钟,在下一章节会对这个进行优化
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Group("blockingQueue")
|
||||||
|
@Benchmark
|
||||||
|
public void put() throws InterruptedException {
|
||||||
|
this.queue.put(VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Group("blockingQueue")
|
||||||
|
@Benchmark
|
||||||
|
public int take() throws InterruptedException {
|
||||||
|
return this.queue.take();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package cn.bunny.jmh;
|
||||||
|
|
||||||
|
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 org.openjdk.jmh.runner.options.TimeValue;
|
||||||
|
|
||||||
|
import java.util.concurrent.BlockingDeque;
|
||||||
|
import java.util.concurrent.LinkedBlockingDeque;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@Fork(1)
|
||||||
|
@Warmup(iterations = 5)
|
||||||
|
@Measurement(iterations = 5)
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
@State(Scope.Group)
|
||||||
|
public class JMHExample15 {
|
||||||
|
private final static int VALUE = Integer.MAX_VALUE;
|
||||||
|
private BlockingDeque<Integer> queue;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
final Options options = new OptionsBuilder()
|
||||||
|
.include(JMHExample15.class.getSimpleName())
|
||||||
|
// 最长批次超时时间不能大于10秒
|
||||||
|
.timeout(TimeValue.seconds(10))
|
||||||
|
.build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void init() {
|
||||||
|
this.queue = new LinkedBlockingDeque<>(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 会出现长时间的阻塞最长时间正常是10分钟,在下一章节会对这个进行优化
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Group("blockingQueue")
|
||||||
|
@Benchmark
|
||||||
|
public void put() throws InterruptedException {
|
||||||
|
this.queue.put(VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Group("blockingQueue")
|
||||||
|
@Benchmark
|
||||||
|
public int take() throws InterruptedException {
|
||||||
|
return this.queue.take();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package cn.bunny.jmh;
|
||||||
|
|
||||||
|
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.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentSkipListMap;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@Fork(1)
|
||||||
|
@Warmup(iterations = 5)
|
||||||
|
@Measurement(iterations = 5)
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
@State(Scope.Group)
|
||||||
|
public class JMHExample16 {
|
||||||
|
// 为type提供了4中可配置的参数值
|
||||||
|
@Param({"1", "2", "3", "4"})
|
||||||
|
private int type;
|
||||||
|
|
||||||
|
private Map<Integer, Integer> map;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
Options options = new OptionsBuilder()
|
||||||
|
.include(JMHExample16.class.getSimpleName())
|
||||||
|
.build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void setup() {
|
||||||
|
switch (type) {
|
||||||
|
case 1:
|
||||||
|
this.map = new ConcurrentHashMap<>();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
this.map = new ConcurrentSkipListMap<>();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
this.map = new Hashtable<>();
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
this.map = Collections.synchronizedMap(new HashMap<>());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Illegal map type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Group("g")
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Benchmark
|
||||||
|
public void put() {
|
||||||
|
int num = new Random().nextInt();
|
||||||
|
this.map.put(num, num);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer get() {
|
||||||
|
int num = new Random().nextInt();
|
||||||
|
return this.map.get(num);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package cn.bunny.jmh;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.*;
|
||||||
|
import org.openjdk.jmh.profile.StackProfiler;
|
||||||
|
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 org.openjdk.jmh.runner.options.TimeValue;
|
||||||
|
|
||||||
|
import java.util.concurrent.BlockingDeque;
|
||||||
|
import java.util.concurrent.LinkedBlockingDeque;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@Fork(1)
|
||||||
|
@Warmup(iterations = 5)
|
||||||
|
@Measurement(iterations = 5)
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
@State(Scope.Group)
|
||||||
|
public class JMHExample17 {
|
||||||
|
private final static int VALUE = Integer.MAX_VALUE;
|
||||||
|
private BlockingDeque<Integer> queue;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
final Options options = new OptionsBuilder()
|
||||||
|
.include(JMHExample17.class.getSimpleName())
|
||||||
|
.timeout(TimeValue.seconds(10))
|
||||||
|
.addProfiler(StackProfiler.class)
|
||||||
|
.build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void init() {
|
||||||
|
this.queue = new LinkedBlockingDeque<>(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Group("blockingQueue")
|
||||||
|
@Benchmark
|
||||||
|
public void put() throws InterruptedException {
|
||||||
|
this.queue.put(VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(5)
|
||||||
|
@Group("blockingQueue")
|
||||||
|
@Benchmark
|
||||||
|
public int take() throws InterruptedException {
|
||||||
|
return this.queue.take();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue