Compare commits

...

6 Commits

Author SHA1 Message Date
Bunny c51c07a7b8 feat: JMHExample17 2025-01-19 16:06:32 +08:00
Bunny 351a6059ac feat: JMHExample16 2025-01-19 16:03:46 +08:00
Bunny 0c0ded951b feat: JMHExample15 2025-01-19 15:57:59 +08:00
Bunny 35cf842258 feat: JMHExample14 2025-01-19 15:53:38 +08:00
Bunny e327a1ca33 feat: JMHExample13 2025-01-19 15:49:55 +08:00
Bunny c8c2a0e8bc feat: JMHExample12 2025-01-19 15:47:36 +08:00
6 changed files with 338 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}