|
| 1 | +// Copyright 2025 the V8 project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// Excerpt from `polyfills.mjs` from the upstream Kotlin compose-multiplatform |
| 6 | +// benchmark directory, with minor changes for JetStream. |
| 7 | + |
| 8 | +globalThis.window ??= globalThis; |
| 9 | + |
| 10 | +globalThis.navigator ??= {}; |
| 11 | +if (!globalThis.navigator.languages) { |
| 12 | + globalThis.navigator.languages = ['en-US', 'en']; |
| 13 | + globalThis.navigator.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'; |
| 14 | + globalThis.navigator.platform = "MacIntel"; |
| 15 | +} |
| 16 | + |
| 17 | +// Compose reads `window.isSecureContext` in its Clipboard feature: |
| 18 | +globalThis.isSecureContext = false; |
| 19 | + |
| 20 | +// Disable explicit GC (it wouldn't work in browsers anyway). |
| 21 | +globalThis.gc = () => { |
| 22 | + // DEBUG |
| 23 | + // console.log("gc()"); |
| 24 | +} |
| 25 | + |
| 26 | +class URL { |
| 27 | + href; |
| 28 | + constructor(url, base) { |
| 29 | + // DEBUG |
| 30 | + // console.log('URL', url, base); |
| 31 | + this.href = url; |
| 32 | + } |
| 33 | +} |
| 34 | +globalThis.URL = URL; |
| 35 | + |
| 36 | +// We always polyfill `fetch` and `instantiateStreaming` for consistency between |
| 37 | +// engine shells and browsers and to avoid introducing network latency into the |
| 38 | +// first iteration / instantiation measurement. |
| 39 | +// The downside is that this doesn't test streaming Wasm instantiation, which we |
| 40 | +// are willing to accept. |
| 41 | +let preload = { /* Initialized in init() below due to async. */ }; |
| 42 | +const originalFetch = globalThis.fetch ?? function(url) { |
| 43 | + throw new Error("no fetch available"); |
| 44 | +} |
| 45 | +globalThis.fetch = async function(url) { |
| 46 | + // DEBUG |
| 47 | + // console.log('fetch', url); |
| 48 | + |
| 49 | + // Redirect some paths to cached/preloaded resources. |
| 50 | + if (preload[url]) { |
| 51 | + return { |
| 52 | + ok: true, |
| 53 | + status: 200, |
| 54 | + arrayBuffer() { return preload[url]; }, |
| 55 | + async blob() { |
| 56 | + return { |
| 57 | + size: preload[url].byteLength, |
| 58 | + async arrayBuffer() { return preload[url]; } |
| 59 | + } |
| 60 | + }, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + // This should only be called in the browser, where fetch() is available. |
| 65 | + return originalFetch(url); |
| 66 | +}; |
| 67 | +globalThis.WebAssembly.instantiateStreaming = async function(m,i) { |
| 68 | + // DEBUG |
| 69 | + // console.log('instantiateStreaming',m,i); |
| 70 | + return WebAssembly.instantiate((await m).arrayBuffer(),i); |
| 71 | +}; |
| 72 | + |
| 73 | +// Provide `setTimeout` for Kotlin coroutines. |
| 74 | +const originalSetTimeout = setTimeout; |
| 75 | +globalThis.setTimeout = function(f, delayMs) { |
| 76 | + // DEBUG |
| 77 | + // console.log('setTimeout', f, t); |
| 78 | + |
| 79 | + // Deep in the Compose UI framework, one task is scheduled every 16ms, see |
| 80 | + // https://github.com/JetBrains/compose-multiplatform-core/blob/a52f2981b9bc7cdba1d1fbe71654c4be448ebea7/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/spatial/RectManager.kt#L138 |
| 81 | + // and |
| 82 | + // https://github.com/JetBrains/compose-multiplatform-core/blob/a52f2981b9bc7cdba1d1fbe71654c4be448ebea7/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/layout/OnLayoutRectChangedModifier.kt#L56 |
| 83 | + // We don't want to delay work in the Wall-time based measurement in JetStream, |
| 84 | + // but executing this immediately (without delay) produces redundant work that |
| 85 | + // is not realistic for a full-browser Kotlin/multiplatform application either, |
| 86 | + // according to Kotlin/JetBrains folks. |
| 87 | + // Hence the early return for 16ms delays. |
| 88 | + if (delayMs === 16) return; |
| 89 | + if (delayMs !== 0) { |
| 90 | + throw new Error('Unexpected delay for setTimeout polyfill: ' + delayMs); |
| 91 | + } |
| 92 | + originalSetTimeout(f); |
| 93 | +} |
| 94 | + |
| 95 | +// Don't automatically run the main function on instantiation. |
| 96 | +globalThis.skipFunMain = true; |
| 97 | + |
| 98 | +// Prevent this from being detected as a shell environment, so that we use the |
| 99 | +// same code paths as in the browser. |
| 100 | +// See `compose-benchmarks-benchmarks.uninstantiated.mjs`. |
| 101 | +delete globalThis.d8; |
| 102 | +delete globalThis.inIon; |
| 103 | +delete globalThis.jscOptions; |
| 104 | + |
| 105 | +class Benchmark { |
| 106 | + skikoInstantiate; |
| 107 | + mainInstantiate; |
| 108 | + wasmInstanceExports; |
| 109 | + |
| 110 | + async init() { |
| 111 | + // DEBUG |
| 112 | + // console.log("init"); |
| 113 | + |
| 114 | + preload = { |
| 115 | + 'skiko.wasm': await getBinary(skikoWasmBinary), |
| 116 | + './compose-benchmarks-benchmarks.wasm': await getBinary(composeWasmBinary), |
| 117 | + './composeResources/compose_benchmarks.benchmarks.generated.resources/drawable/compose-multiplatform.png': await getBinary(inputImageCompose), |
| 118 | + './composeResources/compose_benchmarks.benchmarks.generated.resources/drawable/example1_cat.jpg': await getBinary(inputImageCat), |
| 119 | + './composeResources/compose_benchmarks.benchmarks.generated.resources/files/example1_compose-community-primary.png': await getBinary(inputImageComposeCommunity), |
| 120 | + './composeResources/compose_benchmarks.benchmarks.generated.resources/font/jetbrainsmono_italic.ttf': await getBinary(inputFontItalic), |
| 121 | + './composeResources/compose_benchmarks.benchmarks.generated.resources/font/jetbrainsmono_regular.ttf': await getBinary(inputFontRegular), |
| 122 | + }; |
| 123 | + |
| 124 | + // We patched `skiko.mjs` to not immediately instantiate the `skiko.wasm` |
| 125 | + // module, so that we can move the dynamic JS import here but measure |
| 126 | + // WebAssembly compilation and instantiation as part of the first iteration. |
| 127 | + this.skikoInstantiate = (await dynamicImport(skikoJsModule)).default; |
| 128 | + this.mainInstantiate = (await dynamicImport(composeJsModule)).instantiate; |
| 129 | + } |
| 130 | + |
| 131 | + async runIteration() { |
| 132 | + // DEBUG |
| 133 | + // console.log("runIteration"); |
| 134 | + |
| 135 | + // Compile once in the first iteration. |
| 136 | + if (!this.wasmInstanceExports) { |
| 137 | + const skikoExports = (await this.skikoInstantiate()).wasmExports; |
| 138 | + this.wasmInstanceExports = (await this.mainInstantiate({ './skiko.mjs': skikoExports })).exports; |
| 139 | + } |
| 140 | + |
| 141 | + // We render/animate/process fewer frames than in the upstream benchmark, |
| 142 | + // since we run multiple iterations in JetStream (to measure first, worst, |
| 143 | + // and average runtime) and don't want the overall workload to take too long. |
| 144 | + const frameCountFactor = 5; |
| 145 | + |
| 146 | + // The factors for the subitems are chosen to make them take the same order |
| 147 | + // of magnitude in terms of Wall time. |
| 148 | + await this.wasmInstanceExports.customLaunch("AnimatedVisibility", 100 * frameCountFactor); |
| 149 | + await this.wasmInstanceExports.customLaunch("LazyGrid", 1 * frameCountFactor); |
| 150 | + await this.wasmInstanceExports.customLaunch("LazyGrid-ItemLaunchedEffect", 1 * frameCountFactor); |
| 151 | + // The `SmoothScroll` variants of the LazyGrid workload are much faster. |
| 152 | + await this.wasmInstanceExports.customLaunch("LazyGrid-SmoothScroll", 5 * frameCountFactor); |
| 153 | + await this.wasmInstanceExports.customLaunch("LazyGrid-SmoothScroll-ItemLaunchedEffect", 5 * frameCountFactor); |
| 154 | + // This is quite GC-heavy, is this realistic for Kotlin/compose applications? |
| 155 | + await this.wasmInstanceExports.customLaunch("VisualEffects", 1 * frameCountFactor); |
| 156 | + await this.wasmInstanceExports.customLaunch("MultipleComponents-NoVectorGraphics", 10 * frameCountFactor); |
| 157 | + } |
| 158 | +} |
0 commit comments