
如何在浏览器中运行 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-multiplatformCompose Multiplatform 仓库内置了一套跨平台基准测试工程benchmarks/multiplatform覆盖 Desktop、iOS、macOS 以及 WebKotlin/Wasm 和 Kotlin/JS目标。本文聚焦其中一条路径在浏览器中运行 Kotlin/Wasm 基准测试。该路径有一个前置条件——浏览器必须以启用手动 GC 的方式启动否则基准测试在清理堆内存时会退化为不可用的状态。完成本文后你将得到一个以--expose-gc启动的浏览器以及一次在页面上直接打印结果的 Wasm 基准运行。为什么 Web 基准需要手动 GC基准测试框架在每个基准开始测量前、以及上一个基准结束后都会调用runGC()来清理堆避免残留对象影响测量。这个函数在 Web 目标下的实现是 runGC.web.ktactual 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 WrapperREADME.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 -PrunArgumentsbenchmarksAnimatedVisibilityrunArguments也可以写入gradle.properties效果相同。可用的筛选参数包括参数用途benchmarks逗号分隔的基准名列表可带括号指定问题规模如benchmarksLazyGrid(100),AnimatedVisibilitydisabledBenchmarks要跳过的基准名如disabledBenchmarksHeavyShaderframeCount每个基准测量的帧数如frameCount500warmupCount开始测量前的预热帧数如warmupCount50modes执行模式SIMPLE、VSYNC_EMULATION、REAL、STARTUP默认不指定时启用SIMPLE和VSYNC_EMULATIONlistBenchmarks只列出可用基准名后退出如listBenchmarkstrue当前可用的基准名清单见 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 runs3 benchmarksAnimatedVisibility与直接跑 Gradle 任务相比web 平台的行为有一个额外步骤见脚本中Platform.WEB分支它会先在后台启动一个桌面端的 benchmark server./gradlew :benchmarks:run -PrunArgumentsrunServertrue ...等待 5 秒后再执行:benchmarks:wasmJsBrowserProductionRun结束时用destroy()/destroyForcibly()终止该 server 进程。副作用是运行期间会多出一个 JVM server 进程脚本结束时被强制销毁如果指定了versionversion脚本会改写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),仅供参考