Browse Source

Benchmarks. Calculate median.

Pros: it is very stable between runs (+-1%)
Cons: we don't compare extreme values
igor.demin/benchmark-median1
Igor Demin 1 year ago
parent
commit
551fdc1e4f
  1. 2
      benchmarks/ios/jvm-vs-kotlin-native/build.gradle.kts
  2. 6
      benchmarks/ios/jvm-vs-kotlin-native/src/commonMain/kotlin/Benchmarks.kt
  3. 15
      benchmarks/ios/jvm-vs-kotlin-native/src/commonMain/kotlin/MeasureComposable.kt
  4. 4
      benchmarks/ios/jvm-vs-kotlin-native/src/commonMain/kotlin/benchmarks/animation/AnimatedVisibility.kt
  5. 3
      benchmarks/ios/jvm-vs-kotlin-native/src/desktopMain/kotlin/main.desktop.kt

2
benchmarks/ios/jvm-vs-kotlin-native/build.gradle.kts

@ -5,7 +5,7 @@ plugins {
id("org.jetbrains.compose") id("org.jetbrains.compose")
} }
version = "1.0-SNAPSHOT" version = "1.0"
repositories { repositories {
mavenLocal() mavenLocal()

6
benchmarks/ios/jvm-vs-kotlin-native/src/commonMain/kotlin/Benchmarks.kt

@ -8,7 +8,7 @@ fun runBenchmark(name: String, frameCount: Int, content: @Composable () -> Unit)
} }
fun runBenchmarks() { fun runBenchmarks() {
runBenchmark("AnimatedVisibility", 1000000) { AnimatedVisibility() } runBenchmark("AnimatedVisibility", 2000) { AnimatedVisibility() }
runBenchmark("LazyGrid",2000) { LazyGrid() } runBenchmark("LazyGrid",40) { LazyGrid() }
runBenchmark("VisualEffects",1000) { NYContent() } runBenchmark("VisualEffects",20) { NYContent() }
} }

15
benchmarks/ios/jvm-vs-kotlin-native/src/commonMain/kotlin/MeasureComposable.kt

@ -2,7 +2,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.ComposeScene import androidx.compose.ui.ComposeScene
import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.Constraints
import kotlin.time.Duration import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime import kotlin.time.ExperimentalTime
import kotlin.time.toDuration
const val width = 640 const val width = 640
const val height = 480 const val height = 480
@ -12,22 +14,29 @@ const val nanosPerFrame = (0.16 * nanosPerSecond).toLong()
@OptIn(ExperimentalTime::class) @OptIn(ExperimentalTime::class)
fun measureComposable( fun measureComposable(
frameCount: Int = 1000, frameCount: Int = 100,
content: @Composable () -> Unit content: @Composable () -> Unit
): Duration { ): Duration {
val scene = ComposeScene() val scene = ComposeScene()
try { val times = try {
scene.setContent(content) scene.setContent(content)
scene.constraints = Constraints.fixed(width, height) scene.constraints = Constraints.fixed(width, height)
val surface = org.jetbrains.skia.Surface.makeNull(width, height) val surface = org.jetbrains.skia.Surface.makeNull(width, height)
return kotlin.time.measureTime {
(1..200).map {
kotlin.time.measureTime {
var nanoTime = 0L var nanoTime = 0L
repeat(frameCount) { repeat(frameCount) {
scene.render(surface.canvas, nanoTime) scene.render(surface.canvas, nanoTime)
nanoTime += nanosPerFrame nanoTime += nanosPerFrame
} }
}.inWholeMilliseconds
} }
} finally { } finally {
scene.close() scene.close()
} }
return times.median().toDuration(DurationUnit.MILLISECONDS)
} }
private fun List<Long>.median() = sorted()[size/ 2]

4
benchmarks/ios/jvm-vs-kotlin-native/src/commonMain/kotlin/benchmarks/animation/AnimatedVisibility.kt

@ -20,10 +20,11 @@ import org.jetbrains.compose.resources.painterResource
@OptIn(ExperimentalResourceApi::class) @OptIn(ExperimentalResourceApi::class)
@Composable @Composable
fun AnimatedVisibility() { fun AnimatedVisibility() {
repeat(100) {
MaterialTheme { MaterialTheme {
var showImage by remember { mutableStateOf(false) } var showImage by remember { mutableStateOf(false) }
LaunchedEffect(showImage) { LaunchedEffect(showImage) {
delay(200) delay(1000)
showImage = !showImage showImage = !showImage
} }
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
@ -35,4 +36,5 @@ fun AnimatedVisibility() {
} }
} }
} }
}
} }

3
benchmarks/ios/jvm-vs-kotlin-native/src/desktopMain/kotlin/main.desktop.kt

@ -5,5 +5,6 @@
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.jetbrains.skiko.MainUIDispatcher
fun main() = runBlocking(Dispatchers.Main) { runBenchmarks() } fun main() = runBlocking(MainUIDispatcher) { runBenchmarks() }

Loading…
Cancel
Save