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. 25
      benchmarks/ios/jvm-vs-kotlin-native/src/commonMain/kotlin/MeasureComposable.kt
  4. 26
      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() }
}

25
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 {
var nanoTime = 0L
repeat(frameCount) {
scene.render(surface.canvas, nanoTime)
nanoTime += nanosPerFrame
}
(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]

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

@ -20,18 +20,20 @@ import org.jetbrains.compose.resources.painterResource
@OptIn(ExperimentalResourceApi::class)
@Composable
fun AnimatedVisibility() {
MaterialTheme {
var showImage by remember { mutableStateOf(false) }
LaunchedEffect(showImage) {
delay(200)
showImage = !showImage
}
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
AnimatedVisibility(showImage) {
Image(
painterResource("compose-multiplatform.xml"),
null
)
repeat(100) {
MaterialTheme {
var showImage by remember { mutableStateOf(false) }
LaunchedEffect(showImage) {
delay(1000)
showImage = !showImage
}
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
AnimatedVisibility(showImage) {
Image(
painterResource("compose-multiplatform.xml"),
null
)
}
}
}
}

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