1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  | package benchmark
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.BenchmarkMode
import kotlinx.benchmark.BenchmarkTimeUnit
import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.Measurement
import kotlinx.benchmark.Mode
import kotlinx.benchmark.OutputTimeUnit
import kotlinx.benchmark.Scope
import kotlinx.benchmark.State
import kotlinx.benchmark.Warmup
import kotlin.random.Random
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(BenchmarkTimeUnit.NANOSECONDS)
@Warmup(iterations = 10, time = 500, timeUnit = BenchmarkTimeUnit.NANOSECONDS)
@Measurement(iterations = 100, time = 1, timeUnit = BenchmarkTimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class ExceptionBenchmark {
    class CustomException(
        message: String? = null,
        cause: Throwable? = null,
    ) : Exception(message, cause) {
        constructor(cause: Throwable) : this(null, cause)
        override fun fillInStackTrace(): Throwable = this
    }
    class RegularException(
        message: String? = null,
        cause: Throwable? = null,
    ) : Exception(message, cause) {
        constructor(cause: Throwable) : this(null, cause)
    }
    @Benchmark
    fun customExceptionCaught() {
        try {
            try {
                check(Random.nextBoolean())
            } catch (e: IllegalStateException) {
                throw CustomException("Custom exception occurred!")
            }
        } catch (e: CustomException) {
            // Do nothing
        }
    }
    @Benchmark
    fun regularExceptionCaught() {
        try {
            try {
                check(Random.nextBoolean())
            } catch (e: IllegalStateException) {
                throw RegularException("Regular exception occurred!")
            }
        } catch (e: RegularException) {
            // Do nothing
        }
    }
    @Benchmark
    fun throwAndCatchCustomException(blackhole: Blackhole) {
        try {
            throw CustomException("Custom exception occurred!")
        } catch (e: CustomException) {
            blackhole.consume(e)
        }
    }
    @Benchmark
    fun throwAndCatchRegularException(blackhole: Blackhole) {
        try {
            throw RegularException("Regular exception occurred!")
        } catch (e: RegularException) {
            blackhole.consume(e)
        }
    }
    @Benchmark
    fun throwCustomExceptionAndUnwindStackTrace(blackhole: Blackhole) {
        try {
            throw CustomException("Custom exception occurred!")
        } catch (e: CustomException) {
            blackhole.consume(e.fillInStackTrace())
        }
    }
    @Benchmark
    fun throwRegularExceptionAndUnwindStackTrace(blackhole: Blackhole) {
        try {
            throw RegularException("Regular exception occurred!")
        } catch (e: RegularException) {
            blackhole.consume(e.fillInStackTrace())
        }
    }
}
  |