Skip to content

Instantly share code, notes, and snippets.

@etki
Created December 16, 2019 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etki/cafad62627652946bcf95768ccf4473b to your computer and use it in GitHub Desktop.
Save etki/cafad62627652946bcf95768ccf4473b to your computer and use it in GitHub Desktop.
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.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.TimeUnit;
@Threads(4)
@State(Scope.Thread)
@BenchmarkMode({Mode.AverageTime, Mode.SampleTime})
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(time = 10, iterations = 2)
@Warmup(iterations = 2)
@Fork(2)
public class ExceptionPenalty {
private static final Exception STACKTRACED = new RuntimeException();
private static final Exception EMPTY = new NoStackTrace();
@Benchmark
public void tryCatch(Blackhole blackhole) {
try {
throw STACKTRACED;
} catch (Exception e) {
blackhole.consume(e);
}
}
@Benchmark
public void tryCatchEmpty(Blackhole blackhole) {
try {
throw EMPTY;
} catch (Exception e) {
blackhole.consume(e);
}
}
@Benchmark
public void consume(Blackhole blackhole) {
blackhole.consume(STACKTRACED);
}
@Benchmark
public void create(Blackhole blackhole) {
blackhole.consume(new RuntimeException());
}
@Benchmark
public void createEmpty(Blackhole blackhole) {
blackhole.consume(new NoStackTrace());
}
public static class NoStackTrace extends Exception {
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}
}
Benchmark Mode Cnt Score Error Units
ExceptionPenalty.consume avgt 4 2.704 ± 0.671 ns/op
ExceptionPenalty.create avgt 4 1033.261 ± 267.595 ns/op
ExceptionPenalty.createEmpty avgt 4 16.300 ± 1.142 ns/op
ExceptionPenalty.tryCatch avgt 4 2.491 ± 2.110 ns/op
ExceptionPenalty.tryCatchEmpty avgt 4 2.318 ± 1.092 ns/op
ExceptionPenalty.consume sample 4456832 34.685 ± 0.288 ns/op
ExceptionPenalty.consume:consume·p0.00 sample 16.000 ns/op
ExceptionPenalty.consume:consume·p0.50 sample 33.000 ns/op
ExceptionPenalty.consume:consume·p0.90 sample 43.000 ns/op
ExceptionPenalty.consume:consume·p0.95 sample 47.000 ns/op
ExceptionPenalty.consume:consume·p0.99 sample 52.000 ns/op
ExceptionPenalty.consume:consume·p0.999 sample 175.000 ns/op
ExceptionPenalty.consume:consume·p0.9999 sample 840.651 ns/op
ExceptionPenalty.consume:consume·p1.00 sample 199424.000 ns/op
ExceptionPenalty.create sample 4860198 1067.421 ± 8.214 ns/op
ExceptionPenalty.create:create·p0.00 sample 758.000 ns/op
ExceptionPenalty.create:create·p0.50 sample 812.000 ns/op
ExceptionPenalty.create:create·p0.90 sample 1682.000 ns/op
ExceptionPenalty.create:create·p0.95 sample 1714.000 ns/op
ExceptionPenalty.create:create·p0.99 sample 1800.000 ns/op
ExceptionPenalty.create:create·p0.999 sample 3692.000 ns/op
ExceptionPenalty.create:create·p0.9999 sample 17822.090 ns/op
ExceptionPenalty.create:create·p1.00 sample 2240512.000 ns/op
ExceptionPenalty.createEmpty sample 3921206 47.114 ± 6.102 ns/op
ExceptionPenalty.createEmpty:createEmpty·p0.00 sample 28.000 ns/op
ExceptionPenalty.createEmpty:createEmpty·p0.50 sample 39.000 ns/op
ExceptionPenalty.createEmpty:createEmpty·p0.90 sample 59.000 ns/op
ExceptionPenalty.createEmpty:createEmpty·p0.95 sample 62.000 ns/op
ExceptionPenalty.createEmpty:createEmpty·p0.99 sample 66.000 ns/op
ExceptionPenalty.createEmpty:createEmpty·p0.999 sample 359.000 ns/op
ExceptionPenalty.createEmpty:createEmpty·p0.9999 sample 5564.138 ns/op
ExceptionPenalty.createEmpty:createEmpty·p1.00 sample 6619136.000 ns/op
ExceptionPenalty.tryCatch sample 3962574 31.485 ± 1.978 ns/op
ExceptionPenalty.tryCatch:tryCatch·p0.00 sample 17.000 ns/op
ExceptionPenalty.tryCatch:tryCatch·p0.50 sample 29.000 ns/op
ExceptionPenalty.tryCatch:tryCatch·p0.90 sample 40.000 ns/op
ExceptionPenalty.tryCatch:tryCatch·p0.95 sample 45.000 ns/op
ExceptionPenalty.tryCatch:tryCatch·p0.99 sample 51.000 ns/op
ExceptionPenalty.tryCatch:tryCatch·p0.999 sample 185.000 ns/op
ExceptionPenalty.tryCatch:tryCatch·p0.9999 sample 1876.000 ns/op
ExceptionPenalty.tryCatch:tryCatch·p1.00 sample 2371584.000 ns/op
ExceptionPenalty.tryCatchEmpty sample 4241631 33.251 ± 0.407 ns/op
ExceptionPenalty.tryCatchEmpty:tryCatchEmpty·p0.00 sample 17.000 ns/op
ExceptionPenalty.tryCatchEmpty:tryCatchEmpty·p0.50 sample 29.000 ns/op
ExceptionPenalty.tryCatchEmpty:tryCatchEmpty·p0.90 sample 45.000 ns/op
ExceptionPenalty.tryCatchEmpty:tryCatchEmpty·p0.95 sample 48.000 ns/op
ExceptionPenalty.tryCatchEmpty:tryCatchEmpty·p0.99 sample 54.000 ns/op
ExceptionPenalty.tryCatchEmpty:tryCatchEmpty·p0.999 sample 186.000 ns/op
ExceptionPenalty.tryCatchEmpty:tryCatchEmpty·p0.9999 sample 1611.674 ns/op
ExceptionPenalty.tryCatchEmpty:tryCatchEmpty·p1.00 sample 440832.000 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment