Created
January 16, 2023 11:29
-
-
Save chemicL/67b4f87a58950ae6e5d4dbcd4d2564b7 to your computer and use it in GitHub Desktop.
Benchmark of Mono::singleOptional vs map + defaultIfEmpty
This file contains hidden or 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
@BenchmarkMode({Mode.AverageTime}) | |
@Warmup(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS) | |
@Fork(value = 1) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@State(Scope.Benchmark) | |
public class MonoOptionalBenchmark { | |
private static final Throwable EXCEPTION = new RuntimeException() { | |
@Override | |
public synchronized Throwable fillInStackTrace() { | |
return this; | |
} | |
}; | |
@Benchmark | |
public void singleOptionalBaseline(Blackhole blackhole) { | |
blackhole.consume( | |
Mono.just(1) | |
.map(Optional::ofNullable) | |
.defaultIfEmpty(Optional.empty()) | |
.block() | |
); | |
} | |
@Benchmark | |
public void singleOptionalEmptyBaseline(Blackhole blackhole) { | |
blackhole.consume( | |
Mono.empty() | |
.map(Optional::ofNullable) | |
.defaultIfEmpty(Optional.empty()) | |
.block() | |
); | |
} | |
@Benchmark | |
public void singleOptionalErrorBaseline(Blackhole blackhole) { | |
blackhole.consume( | |
Mono.error(EXCEPTION) | |
.map(Optional::ofNullable) | |
.defaultIfEmpty(Optional.empty()) | |
.onErrorComplete() | |
.block() | |
); | |
} | |
@Benchmark | |
public void singleOptional(Blackhole blackhole) { | |
blackhole.consume( | |
Mono.just(1) | |
.singleOptional() | |
.block() | |
); | |
} | |
@Benchmark | |
public void singleOptionalEmpty(Blackhole blackhole) { | |
blackhole.consume( | |
Mono.empty() | |
.singleOptional() | |
.block() | |
); | |
} | |
@Benchmark | |
public void singleOptionalError(Blackhole blackhole) { | |
blackhole.consume( | |
Mono.error(EXCEPTION) | |
.singleOptional() | |
.onErrorComplete() | |
.block() | |
); | |
} | |
} |
Author
chemicL
commented
Jan 18, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment