|
|
@ -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] |