如何在浏览器中运行 Compose Multiplatform 的 Wasm 基准测试并开启手动 GC
【免费下载链接】compose-multiplatformCompose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.项目地址: https://gitcode.com/GitHub_Trending/co/compose-multiplatform
Compose Multiplatform 仓库内置了一套跨平台基准测试工程(benchmarks/multiplatform),覆盖 Desktop、iOS、macOS 以及 Web(Kotlin/Wasm 和 Kotlin/JS)目标。本文聚焦其中一条路径:在浏览器中运行 Kotlin/Wasm 基准测试。该路径有一个前置条件——浏览器必须以启用手动 GC 的方式启动,否则基准测试在清理堆内存时会退化为不可用的状态。完成本文后,你将得到:一个以--expose-gc启动的浏览器,以及一次在页面上直接打印结果的 Wasm 基准运行。
为什么 Web 基准需要手动 GC
基准测试框架在每个基准开始测量前、以及上一个基准结束后都会调用runGC()来清理堆,避免残留对象影响测量。这个函数在 Web 目标下的实现是 runGC.web.kt:
actual fun runGC() { js("(typeof gc === 'function')? gc() : console.log('Manual GC is not available. Ensure that the browser was started with the appropriate flags.')") }即:只有当浏览器把 JS 全局函数gc暴露出来时,runGC()才真正执行垃圾回收;否则每次调用都会在控制台打印提示Manual GC is not available. Ensure that the browser was started with the appropriate flags.。调用点位于 MeasureComposable.skiko.kt(测量前的准备、测量循环之间、以及基准结束后的清理)。
benchmarks/multiplatform/README.md 给出的启用方式(以 Google Chrome 为例,macOS 命令)是:
open -a Google\ Chrome --args --js-flags="--expose-gc"该命令会启动一个带--js-flags="--expose-gc"参数的 Chrome 实例,副作用仅是额外打开一个浏览器进程,不涉及文件修改或系统包安装。
运行 K/Wasm 基准
以下命令都在benchmarks/multiplatform目录下执行(该目录自带 Gradle Wrapper,README.md中的示例命令即以此为工作目录)。
主路径命令:
./gradlew clean :benchmarks:wasmJsBrowserProductionRunREADME 说明结果会直接打印在浏览器页面本身。任务名中的Production表示走生产构建;任务执行时会启动 webpack dev server,并按 benchmarks/build.gradle.kts 中的配置自动打开http://localhost:8080(附加参数以查询串形式带上),因此请确认本机 8080 端口未被占用。
可选:只跑单个基准
基准参数通过-PrunArguments="..."传入,参数表见 README.md。例如只跑AnimatedVisibility:
./gradlew clean :benchmarks:wasmJsBrowserProductionRun -PrunArguments="benchmarks=AnimatedVisibility"runArguments也可以写入gradle.properties,效果相同。可用的筛选参数包括:
| 参数 | 用途 |
|---|---|
benchmarks | 逗号分隔的基准名列表,可带括号指定问题规模,如benchmarks=LazyGrid(100),AnimatedVisibility |
disabledBenchmarks | 要跳过的基准名,如disabledBenchmarks=HeavyShader |
frameCount | 每个基准测量的帧数,如frameCount=500 |
warmupCount | 开始测量前的预热帧数,如warmupCount=50 |
modes | 执行模式(SIMPLE、VSYNC_EMULATION、REAL、STARTUP),默认不指定时启用SIMPLE和VSYNC_EMULATION |
listBenchmarks | 只列出可用基准名后退出,如listBenchmarks=true |
当前可用的基准名清单见 README.md 的 Benchmarks description 表格(AnimatedVisibility、LazyGrid、LazyList、VisualEffects、HeavyShader等)。
验证运行是否成功
- 页面结果:基准完成后,结果直接打印在浏览器页面本身(README 原文:“you can see the results printed on the page itself”)。
- 手动 GC 是否生效:打开浏览器控制台检查。如果每次出现
Manual GC is not available. Ensure that the browser was started with the appropriate flags.,说明gc全局函数不存在,即浏览器没有按“为什么 Web 基准需要手动 GC”一节的方式启动;重新用--expose-gc参数启动后再跑即可。
这两点就是文档给出的判断依据:页面能出结果说明基准跑完,控制台不再出现 manual GC 提示说明 GC 清理环节正常工作。
可选分支:通过脚本以 web 平台运行
如果需要多轮迭代、自动归档 JSON 结果,可以用 run_benchmarks.main.kts 的web平台:
./run_benchmarks.main.kts web runs=3 benchmarks=AnimatedVisibility与直接跑 Gradle 任务相比,web 平台的行为有一个额外步骤(见脚本中Platform.WEB分支):它会先在后台启动一个桌面端的 benchmark server(./gradlew :benchmarks:run -PrunArguments="runServer=true ..."),等待 5 秒后再执行:benchmarks:wasmJsBrowserProductionRun,结束时用destroy()/destroyForcibly()终止该 server 进程。副作用是:运行期间会多出一个 JVM server 进程,脚本结束时被强制销毁;如果指定了version=<version>,脚本会改写gradle/libs.versions.toml并在结束后恢复。JSON 结果归档到benchmarks/build/benchmarks/archive/web/${version}_run${runIndex}。
边界与限制
- 手动 GC 启动命令文档只给出了 Google Chrome 的示例;其他浏览器是否以及如何暴露
gc函数,文档未说明,以浏览器自身机制为准。 - 同一条命令链中不要混用 D8 路径:README 中
wasmJsD8ProductionRun与 D8 分发构建(buildD8Distribution)是独立的运行方式,与浏览器路径互不依赖,本文不展开。 - K/JS 目标有对应的浏览器任务
./gradlew clean :benchmarks:jsBrowserProductionRun,验证方式相同;它是与 Wasm 并列的另一目标,不属于本文路径。
【免费下载链接】compose-multiplatformCompose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.项目地址: https://gitcode.com/GitHub_Trending/co/compose-multiplatform
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考