Repository: JetBrains/compose-multiplatform
File: benchmarks/multiplatform/benchmarks/src/commonMain/kotlin/Benchmarks.kt (Kotlin)

Code before:
    if (Config.isBenchmarkEnabled(benchmark.name)) {
        Box(modifier = Modifier.fillMaxSize()) {
            if (!isEmptyScreen) {
                benchmark.content()
            }

            LaunchedEffect(benchmark.name, isWarmup, isEmptyScreen) {
                if (isWarmup) {
                    repeat(Config.warmupCount) {
                        withFrameNanos { }
                    }
                    isWarmup = false
                    isEmptyScreen = true
                } else if (isEmptyScreen) {
                    delay(Config.emptyScreenDelay.milliseconds)
                    withFrameNanos {  }
                    isEmptyScreen = false
                } else {
                    val start = TimeSource.Monotonic.markNow()
                    val frames = MutableList(benchmark.frameCount) {
                        BenchmarkFrame(Duration.ZERO, Duration.ZERO)
                    }
                    // skip first frame waiting
                    withFrameNanos {  }
                    repeat(benchmark.frameCount) {
                        val frameStart = TimeSource.Monotonic.markNow()
                        withFrameNanos { }
                        val rawFrameTime = frameStart.elapsedNow()
                        // rawFrameTime isn't reliable for missed-frame checks: small timing inaccuracies can push
                        // it just over the frame budget even when the frame would effectively fit.
                        // Instead, estimate how many vsync intervals the frame took by rounding the measured time to
                        // the nearest multiple of nanosPerFrame.
                        // Note: this is only an estimate — we can’t determine here whether a frame was truly missed.
                        val normalizedFrameTime = ((rawFrameTime.inWholeNanoseconds + nanosPerFrame/2) / nanosPerFrame) * nanosPerFrame
                        frames[it] = BenchmarkFrame(normalizedFrameTime.nanoseconds, Duration.ZERO)
                    }
                    val duration = start.elapsedNow()
                    val stats = BenchmarkResult(
                        name = benchmark.name,
                        frameBudget = nanosPerFrame.nanoseconds,
                        conditions = BenchmarkConditions(benchmark.frameCount, 0),
                        averageFrameInfo = FrameInfo(duration / benchmark.frameCount, Duration.ZERO),
                        averageFPSInfo = FPSInfo(benchmark.frameCount.toDouble() / duration.toDouble(DurationUnit.SECONDS)),
                        frames = frames
                    ).generateStats()
                    if (!Config.reportAtTheEnd) {
                        stats.prettyPrint()
                    } else {
                        results.add(stats)
                    }
                    if (Config.saveStats()) {

Review comment (suggestion):
We should move it after `withFrameNanos { }`, as it can still wait for vsync in some cases (when we start another benchmark in the same window)

Code after:
    if (Config.isBenchmarkEnabled(benchmark.name)) {
        Box(modifier = Modifier.fillMaxSize()) {
            if (!isEmptyScreen) {
                benchmark.content()
            }

            LaunchedEffect(benchmark.name, isWarmup, isEmptyScreen) {
                if (isWarmup) {
                    repeat(Config.warmupCount) {
                        withFrameNanos { }
                    }
                    isWarmup = false
                    isEmptyScreen = true
                } else if (isEmptyScreen) {
                    delay(Config.emptyScreenDelay.milliseconds)
                    withFrameNanos {  }
                    isEmptyScreen = false
                } else {
                    val frames = MutableList(benchmark.frameCount) {
                        BenchmarkFrame(Duration.ZERO, Duration.ZERO)
                    }
                    // skip first frame waiting
                    withFrameNanos {  }
                    val start = TimeSource.Monotonic.markNow()
                    repeat(benchmark.frameCount) {
                        val frameStart = TimeSource.Monotonic.markNow()
                        withFrameNanos { }
                        val rawFrameTime = frameStart.elapsedNow()
                        // rawFrameTime isn't reliable for missed-frame checks: small timing inaccuracies can push
                        // it just over the frame budget even when the frame would effectively fit.
                        // Instead, estimate how many vsync intervals the frame took by rounding the measured time to
                        // the nearest multiple of nanosPerFrame.
                        // Note: this is only an estimate — we can’t determine here whether a frame was truly missed.
                        val normalizedFrameTime = ((rawFrameTime.inWholeNanoseconds + nanosPerFrame/2) / nanosPerFrame) * nanosPerFrame
                        frames[it] = BenchmarkFrame(normalizedFrameTime.nanoseconds, Duration.ZERO)
                    }
                    val duration = start.elapsedNow()
                    val stats = BenchmarkResult(
                        name = benchmark.name,
                        frameBudget = nanosPerFrame.nanoseconds,
                        conditions = BenchmarkConditions(benchmark.frameCount, 0),
                        averageFrameInfo = FrameInfo(duration / benchmark.frameCount, Duration.ZERO),
                        averageFPSInfo = FPSInfo(benchmark.frameCount.toDouble() / duration.toDouble(DurationUnit.SECONDS)),
                        frames = frames
                    ).generateStats()
                    if (!Config.reportAtTheEnd) {
                        stats.prettyPrint()
                    } else {
                        results.add(stats)
                    }
                    if (Config.saveStats()) {