Compare commits

...

3 Commits

Author SHA1 Message Date
Bunny 690d9c99e7 feat: JMHExample06 2025-01-19 12:56:17 +08:00
Bunny 7ecc283972 feat: JMHExample05 2025-01-19 12:43:59 +08:00
Bunny 50ab14b432 feat: JMHExample04 2025-01-19 12:37:48 +08:00
3 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,40 @@
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.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@Warmup(iterations = 10)
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Threads(5)
public class JMHExample04 {
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(JMHExample04.class.getSimpleName())
.build();
new Runner(options).run();
}
@Benchmark
public void test(Test test) {
test.method();
}
// Test实例会被多个线程共享只有一份Test
@State(Scope.Benchmark)
public static class Test {
public Test() {
System.out.println("create instance");
}
public void method() {
}
}
}

View File

@ -0,0 +1,56 @@
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.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class JMHExample05 {
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(JMHExample05.class.getSimpleName())
.build();
new Runner(options).run();
}
// 在线程组test在线称中有三个线程不断对Test实例write方法进行调用
@GroupThreads(3)
@Group("test")
@Benchmark
public void testWrite(MyState state) {
state.write();
}
// 在线程组test在线程中有三个线程对testRead方法进行调用
@GroupThreads(3)
@Group("test")
@Benchmark
public void testRead(MyState state) {
state.read();
}
@State(Scope.Benchmark)
public static class MyState {
public MyState() {
System.out.println("create instance");
}
public void write() {
System.out.println("write");
}
public void read() {
System.out.println("read");
}
}
}

View File

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