Merge pull request #9724 from nguyennamthai/BAEL-4113
BAEL-4113 Measure exception performance
This commit is contained in:
commit
53a7dae4b8
|
@ -120,6 +120,18 @@
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>${maven-jar-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.baeldung.performancetests.MappingFrameworksPerformance</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
<pluginManagement>
|
<pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -178,6 +190,7 @@
|
||||||
<jmh-core.version>1.21</jmh-core.version>
|
<jmh-core.version>1.21</jmh-core.version>
|
||||||
<jmh-generator.version>1.21</jmh-generator.version>
|
<jmh-generator.version>1.21</jmh-generator.version>
|
||||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||||
|
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
Java source/target to use for compilation.
|
Java source/target to use for compilation.
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.baeldung.performancetests.exception;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
|
import org.openjdk.jmh.annotations.Measurement;
|
||||||
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
|
import org.openjdk.jmh.annotations.Warmup;
|
||||||
|
import org.openjdk.jmh.infra.Blackhole;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Fork(1)
|
||||||
|
@Warmup(iterations = 2)
|
||||||
|
@Measurement(iterations = 10)
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||||
|
public class ExceptionBenchmark {
|
||||||
|
private static final int LIMIT = 10_000;
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void doNotThrowException(Blackhole blackhole) {
|
||||||
|
for (int i = 0; i < LIMIT; i++) {
|
||||||
|
blackhole.consume(new Object());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void throwAndCatchException(Blackhole blackhole) {
|
||||||
|
for (int i = 0; i < LIMIT; i++) {
|
||||||
|
try {
|
||||||
|
throw new Exception();
|
||||||
|
} catch (Exception e) {
|
||||||
|
blackhole.consume(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void createExceptionWithoutThrowingIt(Blackhole blackhole) {
|
||||||
|
for (int i = 0; i < LIMIT; i++) {
|
||||||
|
blackhole.consume(new Exception());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@Fork(value = 1, jvmArgs = "-XX:-StackTraceInThrowable")
|
||||||
|
public void throwExceptionWithoutAddingStackTrace(Blackhole blackhole) {
|
||||||
|
for (int i = 0; i < LIMIT; i++) {
|
||||||
|
try {
|
||||||
|
throw new Exception();
|
||||||
|
} catch (Exception e) {
|
||||||
|
blackhole.consume(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void throwExceptionAndUnwindStackTrace(Blackhole blackhole) {
|
||||||
|
for (int i = 0; i < LIMIT; i++) {
|
||||||
|
try {
|
||||||
|
throw new Exception();
|
||||||
|
} catch (Exception e) {
|
||||||
|
blackhole.consume(e.getStackTrace());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue