Last active
January 6, 2021 05:38
-
-
Save ZenLiuCN/f708c0904a771118d6288f03ac15040c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.Test; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import org.openjdk.jmh.runner.options.TimeValue; | |
import java.util.Optional; | |
import java.util.concurrent.TimeUnit; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
public class JmhMatchTest { | |
@Test | |
public void launchBenchmarkMatch() throws Exception { | |
Options opt = new OptionsBuilder() | |
// Specify which benchmarks to run. | |
// You can be more specific if you'd like to run only one benchmark per test. | |
.include(this.getClass().getName() + ".*") | |
// Set the following options as needed | |
.mode(Mode.AverageTime) | |
.timeUnit(TimeUnit.NANOSECONDS) | |
.warmupTime(TimeValue.seconds(1)) | |
.warmupIterations(20) | |
.measurementTime(TimeValue.seconds(1)) | |
.measurementIterations(200) | |
.threads(20) | |
.forks(1) | |
.shouldFailOnError(true) | |
.shouldDoGC(true) | |
//.jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining") | |
//.addProfiler(WinPerfAsmProfiler.class) | |
.build(); | |
new Runner(opt).run(); | |
} | |
@State(Scope.Thread) | |
public static class BenchmarkState { | |
Consumer<Integer> execution; | |
Function<Integer, Optional<Long>> eval; | |
Function<Integer, Optional<Long>> eval2; | |
@Setup(Level.Trial) | |
public void initialize() { | |
execution = i->Match.on(i) | |
.caseEqual(2,x->{}) | |
.caseMatch(x->x>2,x->{}) | |
.caseElse(x->{}); | |
eval = x->Match.<Integer,Long>lazyOf(x) | |
.caseEqual(1,y->y+1L) | |
.caseIs(Integer.class,y->y*2L) | |
.caseElse(()->1L) | |
.get(); | |
eval2 = x ->{ | |
if (x.equals(1)) { | |
return Optional.of(x + 1L); | |
} else if (x.getClass().isAssignableFrom(Integer.class)) { | |
return Optional.of(x * 2L); | |
} else { | |
return Optional.of(1L); | |
} | |
} | |
; | |
} | |
} | |
@Benchmark | |
public void evalBenchmark(JmhTest.BenchmarkState state, Blackhole bh) { | |
bh.consume(state.eval.eval(2)); | |
} | |
@Benchmark | |
public void execBenchmark(JmhTest.BenchmarkState state, Blackhole bh) { | |
bh.consume(state.execution.exec(2)); | |
} | |
@Benchmark | |
public void evalIfElseBenchmark(JmhTest.BenchmarkState state, Blackhole bh) { | |
bh.consume(state.eval2.eval(2)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jetbrains.annotations.Nullable; | |
import org.junit.jupiter.api.Test; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import org.openjdk.jmh.runner.options.TimeValue; | |
import java.util.concurrent.TimeUnit; | |
public class JmhCaseTest { | |
final static int warmUpBatchSize = 20; | |
final static int warmUpIterations = 10; | |
final static int warmUpTime = 20; | |
final static int measurementTime = 20; | |
final static int measurementIterations = 20; | |
final static int timeoutTime = 20; | |
private static void benchRun(@Nullable String methodName, boolean common) throws Exception { | |
final ChainedOptionsBuilder opt = new OptionsBuilder() | |
.include(JmhMatchTest.class.getName() + (methodName == null || methodName.isEmpty() ? ".*" : "." + methodName)); | |
//region config | |
if (common) | |
opt.mode(Mode.AverageTime) | |
.timeUnit(TimeUnit.NANOSECONDS) | |
.warmupTime(TimeValue.milliseconds(1)) | |
.warmupIterations(warmUpIterations) | |
.warmupBatchSize(warmUpBatchSize) | |
.measurementTime(TimeValue.milliseconds(1)) | |
.measurementIterations(measurementIterations) | |
.threads(5) | |
.forks(5) | |
.shouldFailOnError(true) | |
.shouldDoGC(true) | |
//.jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining") | |
//.addProfiler(WinPerfAsmProfiler.class) | |
//endregion | |
; | |
new Runner(opt.build()).run(); | |
} | |
@Test | |
public void allBenchmark() throws Exception { | |
benchRun("",true); | |
} | |
@Test | |
public void execBenchmark() throws Exception { | |
benchRun("execBenchmark",true); | |
} | |
@Test | |
public void evalIfElseBenchmark() throws Exception { | |
benchRun("evalIfElseBenchmark",true); | |
} | |
@Test | |
public void evalBenchmark() throws Exception { | |
benchRun("evalBenchmark",true); | |
} | |
@Benchmark | |
public void evalBenchmark(BenchmarkState state, Blackhole bh) { | |
bh.consume(state.eval.eval(2)); | |
} | |
@Benchmark | |
public void execBenchmark(BenchmarkState state, Blackhole bh) { | |
bh.consume(state.execution.exec(2)); | |
} | |
@Benchmark | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@Warmup(iterations = warmUpBatchSize, batchSize = warmUpBatchSize, time = warmUpTime, timeUnit = TimeUnit.MICROSECONDS) | |
@Timeout(time = timeoutTime) | |
@Measurement(iterations = measurementIterations, time = measurementTime, timeUnit = TimeUnit.MICROSECONDS) | |
public void evalIfElseBenchmark(BenchmarkState state, Blackhole bh) { | |
bh.consume(state.eval2.eval(2)); | |
} | |
@State(Scope.Thread) | |
public static class BenchmarkState { | |
Case.Execution<Object> execution; | |
Case.Evaluation<Integer, Long> eval; | |
Case.Evaluation<Integer, Long> eval2; | |
@Setup(Level.Trial) | |
public void initialize() { | |
execution = Case.execute() | |
.equal(1, x -> { | |
}) | |
.is(Integer.class, x -> { | |
}) | |
.doElse(x -> { | |
}); | |
eval = Case.evaluate(Integer.class, long.class) | |
.equal(1, x -> x + 1L) | |
.is(Integer.class, x -> x * 2L) | |
.doElse(() -> 1L); | |
eval2 = x -> | |
Case.Maybe.wrap(() -> { | |
if (x.equals(1)) { | |
return x + 1L; | |
} else if (x.getClass().isAssignableFrom(Integer.class)) { | |
return x * 2L; | |
} else { | |
return 1L; | |
} | |
}) | |
; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment