Skip to content

Instantly share code, notes, and snippets.

@thewisenerd
Created January 3, 2023 06:34
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 thewisenerd/e7afb9403c55970de92c5105451dd9d6 to your computer and use it in GitHub Desktop.
Save thewisenerd/e7afb9403c55970de92c5105451dd9d6 to your computer and use it in GitHub Desktop.
jit-test
package org.dummy.jmhbench
object FeatureFlags {
val enabled: String = System.getenv("FEATURE-FLAG-ENABLED") ?: "false"
val enabledT: Boolean = enabled.toBoolean()
fun viaFunction(): String {
return System.getenv("FEATURE-FLAG-ENABLED") ?: "false"
}
fun viaFunctionT(): Boolean {
return (System.getenv("FEATURE-FLAG-ENABLED") ?: "false") == "true"
}
}
package org.dummy.jmhbench
import org.openjdk.jmh.annotations.*
import java.util.concurrent.TimeUnit
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
open class MyBenchmark {
companion object {
const val COUNT = 1024 * 1024
}
@Benchmark
fun value(): Boolean {
return FeatureFlags.enabled == "true"
}
@Benchmark
fun valueT(): Boolean {
return FeatureFlags.enabledT
}
@Benchmark
fun fn(): Boolean {
return FeatureFlags.viaFunction() == "true"
}
@Benchmark
fun fnT(): Boolean {
return FeatureFlags.viaFunctionT()
}
@Benchmark
@OperationsPerInvocation(1024)
fun throughValue(): Int {
var a = 0
for (i in 1 .. COUNT) {
a += 1
if (FeatureFlags.enabledT) {
a += 1
}
}
return a
}
@Benchmark
@OperationsPerInvocation(1024)
fun throughFunction(): Int {
var a = 0
for (i in 1 .. COUNT) {
a += 1
if (FeatureFlags.viaFunctionT()) {
a += 1
}
}
return a
}
}
Benchmark Mode Cnt Score Error Units
MyBenchmark.fn avgt 5 35.091 ± 0.962 ns/op
MyBenchmark.fnT avgt 5 33.893 ± 0.533 ns/op
MyBenchmark.throughFunction avgt 5 35012.899 ± 763.721 ns/op
MyBenchmark.throughValue avgt 5 0.002 ± 0.001 ns/op
MyBenchmark.value avgt 5 2.242 ± 0.113 ns/op
MyBenchmark.valueT avgt 5 2.228 ± 0.037 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment