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") 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() }
} }

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.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 {
var nanoTime = 0L (1..200).map {
repeat(frameCount) { kotlin.time.measureTime {
scene.render(surface.canvas, nanoTime) var nanoTime = 0L
nanoTime += nanosPerFrame repeat(frameCount) {
} scene.render(surface.canvas, nanoTime)
nanoTime += nanosPerFrame
}
}.inWholeMilliseconds
} }
} finally { } finally {
scene.close() 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) @OptIn(ExperimentalResourceApi::class)
@Composable @Composable
fun AnimatedVisibility() { fun AnimatedVisibility() {
MaterialTheme { repeat(100) {
var showImage by remember { mutableStateOf(false) } MaterialTheme {
LaunchedEffect(showImage) { var showImage by remember { mutableStateOf(false) }
delay(200) LaunchedEffect(showImage) {
showImage = !showImage delay(1000)
} showImage = !showImage
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { }
AnimatedVisibility(showImage) { Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Image( AnimatedVisibility(showImage) {
painterResource("compose-multiplatform.xml"), Image(
null 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.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