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")
}
version = "1.0-SNAPSHOT"
version = "1.0"
repositories {
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() {
runBenchmark("AnimatedVisibility", 1000000) { AnimatedVisibility() }
runBenchmark("LazyGrid",2000) { LazyGrid() }
runBenchmark("VisualEffects",1000) { NYContent() }
runBenchmark("AnimatedVisibility", 2000) { AnimatedVisibility() }
runBenchmark("LazyGrid",40) { LazyGrid() }
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.unit.Constraints
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import kotlin.time.toDuration
const val width = 640
const val height = 480
@ -12,22 +14,29 @@ const val nanosPerFrame = (0.16 * nanosPerSecond).toLong()
@OptIn(ExperimentalTime::class)
fun measureComposable(
frameCount: Int = 1000,
frameCount: Int = 100,
content: @Composable () -> Unit
): Duration {
val scene = ComposeScene()
try {
val times = try {
scene.setContent(content)
scene.constraints = Constraints.fixed(width, height)
val surface = org.jetbrains.skia.Surface.makeNull(width, height)
return kotlin.time.measureTime {
(1..200).map {
kotlin.time.measureTime {
var nanoTime = 0L
repeat(frameCount) {
scene.render(surface.canvas, nanoTime)
nanoTime += nanosPerFrame
}
}.inWholeMilliseconds
}
} finally {
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)
@Composable
fun AnimatedVisibility() {
repeat(100) {
MaterialTheme {
var showImage by remember { mutableStateOf(false) }
LaunchedEffect(showImage) {
delay(200)
delay(1000)
showImage = !showImage
}
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
@ -36,3 +37,4 @@ 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.runBlocking
import org.jetbrains.skiko.MainUIDispatcher
fun main() = runBlocking(Dispatchers.Main) { runBenchmarks() }
fun main() = runBlocking(MainUIDispatcher) { runBenchmarks() }

Loading…
Cancel
Save