Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Dawn Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Austin Eng | b00c50e | 2022-08-26 22:34:27 +0000 | [diff] [blame] | 15 | import { globalTestConfig } from '../third_party/webgpu-cts/src/common/framework/test_config.js'; |
Austin Eng | 92b32e8 | 2022-11-21 15:16:51 +0000 | [diff] [blame] | 16 | import { dataCache } from '../third_party/webgpu-cts/src/common/framework/data_cache.js'; |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 17 | import { DefaultTestFileLoader } from '../third_party/webgpu-cts/src/common/internal/file_loader.js'; |
| 18 | import { prettyPrintLog } from '../third_party/webgpu-cts/src/common/internal/logging/log_message.js'; |
| 19 | import { Logger } from '../third_party/webgpu-cts/src/common/internal/logging/logger.js'; |
| 20 | import { parseQuery } from '../third_party/webgpu-cts/src/common/internal/query/parseQuery.js'; |
| 21 | |
| 22 | import { TestWorker } from '../third_party/webgpu-cts/src/common/runtime/helper/test_worker.js'; |
| 23 | |
Brian Sheedy | 17b1a45 | 2022-04-14 17:19:11 +0000 | [diff] [blame] | 24 | // The Python-side websockets library has a max payload size of 72638. Set the |
| 25 | // max allowable logs size in a single payload to a bit less than that. |
| 26 | const LOGS_MAX_BYTES = 72000; |
| 27 | |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 28 | var socket; |
| 29 | |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 30 | // Returns a wrapper around `fn` which gets called at most once every `intervalMs`. |
| 31 | // If the wrapper is called when `fn` was called too recently, `fn` is scheduled to |
| 32 | // be called later in the future after the interval passes. |
| 33 | // Returns [ wrappedFn, {start, stop}] where wrappedFn is the rate-limited function, |
| 34 | // and start/stop control whether or not the function is enabled. If it is stopped, calls |
| 35 | // to the fn will no-op. If it is started, calls will be rate-limited, starting from |
| 36 | // the time `start` is called. |
| 37 | function rateLimited(fn, intervalMs) { |
| 38 | let last = undefined; |
| 39 | let timer = undefined; |
| 40 | const wrappedFn = (...args) => { |
Austin Eng | 26ffcd1 | 2022-09-13 18:28:31 +0000 | [diff] [blame] | 41 | if (last === undefined) { |
| 42 | // If the function is not enabled, return. |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | // Get the current time as a number. |
| 46 | const now = +new Date(); |
| 47 | const diff = now - last; |
| 48 | if (diff >= intervalMs) { |
| 49 | // Clear the timer, if there was one. This could happen if a timer |
| 50 | // is scheduled, but it never runs due to long-running synchronous |
| 51 | // code. |
| 52 | if (timer) { |
| 53 | clearTimeout(timer); |
| 54 | timer = undefined; |
| 55 | } |
| 56 | |
| 57 | // Call the function. |
| 58 | last = now; |
| 59 | fn(...args); |
| 60 | } else if (timer === undefined) { |
| 61 | // Otherwise, we have called `fn` too recently. |
| 62 | // Schedule a future call. |
| 63 | timer = setTimeout(() => { |
| 64 | // Clear the timer to indicate nothing is scheduled. |
| 65 | timer = undefined; |
| 66 | last = +new Date(); |
| 67 | fn(...args); |
| 68 | }, intervalMs - diff + 1); |
| 69 | } |
| 70 | }; |
| 71 | return [ |
| 72 | wrappedFn, |
| 73 | { |
| 74 | start: () => { |
| 75 | last = +new Date(); |
| 76 | }, |
| 77 | stop: () => { |
| 78 | last = undefined; |
| 79 | if (timer) { |
| 80 | clearTimeout(timer); |
| 81 | timer = undefined; |
| 82 | } |
| 83 | }, |
| 84 | } |
| 85 | ]; |
| 86 | } |
| 87 | |
Brian Sheedy | 17b1a45 | 2022-04-14 17:19:11 +0000 | [diff] [blame] | 88 | function byteSize(s) { |
| 89 | return new Blob([s]).size; |
| 90 | } |
| 91 | |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 92 | async function setupWebsocket(port) { |
| 93 | socket = new WebSocket('ws://127.0.0.1:' + port) |
| 94 | socket.addEventListener('message', runCtsTestViaSocket); |
| 95 | } |
| 96 | |
| 97 | async function runCtsTestViaSocket(event) { |
| 98 | let input = JSON.parse(event.data); |
| 99 | runCtsTest(input['q'], input['w']); |
| 100 | } |
| 101 | |
Austin Eng | 92b32e8 | 2022-11-21 15:16:51 +0000 | [diff] [blame] | 102 | dataCache.setStore({ |
| 103 | load: async (path) => { |
| 104 | return await (await fetch(`/third_party/webgpu-cts/cache/data/${path}`)).text(); |
| 105 | } |
| 106 | }); |
| 107 | |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 108 | // Make a rate-limited version `sendMessageTestHeartbeat` that executes |
| 109 | // at most once every 500 ms. |
| 110 | const [sendHeartbeat, { |
| 111 | start: beginHeartbeatScope, |
| 112 | stop: endHeartbeatScope |
| 113 | }] = rateLimited(sendMessageTestHeartbeat, 500); |
| 114 | |
| 115 | function wrapPromiseWithHeartbeat(prototype, key) { |
| 116 | const old = prototype[key]; |
| 117 | prototype[key] = function (...args) { |
| 118 | return new Promise((resolve, reject) => { |
| 119 | // Send the heartbeat both before and after resolve/reject |
| 120 | // so that the heartbeat is sent ahead of any potentially |
| 121 | // long-running synchronous code awaiting the Promise. |
| 122 | old.call(this, ...args) |
| 123 | .then(val => { sendHeartbeat(); resolve(val) }) |
| 124 | .catch(err => { sendHeartbeat(); reject(err) }) |
| 125 | .finally(sendHeartbeat); |
| 126 | }); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | wrapPromiseWithHeartbeat(GPU.prototype, 'requestAdapter'); |
| 131 | wrapPromiseWithHeartbeat(GPUAdapter.prototype, 'requestAdapterInfo'); |
| 132 | wrapPromiseWithHeartbeat(GPUAdapter.prototype, 'requestDevice'); |
| 133 | wrapPromiseWithHeartbeat(GPUDevice.prototype, 'createRenderPipelineAsync'); |
| 134 | wrapPromiseWithHeartbeat(GPUDevice.prototype, 'createComputePipelineAsync'); |
| 135 | wrapPromiseWithHeartbeat(GPUDevice.prototype, 'popErrorScope'); |
| 136 | wrapPromiseWithHeartbeat(GPUQueue.prototype, 'onSubmittedWorkDone'); |
| 137 | wrapPromiseWithHeartbeat(GPUBuffer.prototype, 'mapAsync'); |
James Price | 55509fa | 2023-03-10 11:06:36 +0000 | [diff] [blame] | 138 | wrapPromiseWithHeartbeat(GPUShaderModule.prototype, 'getCompilationInfo'); |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 139 | |
Austin Eng | b00c50e | 2022-08-26 22:34:27 +0000 | [diff] [blame] | 140 | globalTestConfig.testHeartbeatCallback = sendHeartbeat; |
Austin Eng | e0cbb0c | 2022-09-13 22:16:42 +0000 | [diff] [blame] | 141 | globalTestConfig.noRaceWithRejectOnTimeout = true; |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 142 | |
Ben Clayton | 33bfc988 | 2023-01-05 21:44:37 +0000 | [diff] [blame] | 143 | // FXC is very slow to compile unrolled const-eval loops, where the metal shader |
| 144 | // compiler (Intel GPU) is very slow to compile rolled loops. Intel drivers for |
| 145 | // linux may also suffer the same performance issues, so unroll const-eval loops |
| 146 | // if we're not running on Windows. |
| 147 | if (navigator.userAgent.indexOf("Windows") !== -1) { |
| 148 | globalTestConfig.unrollConstEvalLoops = true; |
| 149 | } |
| 150 | |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 151 | async function runCtsTest(query, use_worker) { |
| 152 | const workerEnabled = use_worker; |
| 153 | const worker = workerEnabled ? new TestWorker(false) : undefined; |
| 154 | |
| 155 | const loader = new DefaultTestFileLoader(); |
| 156 | const filterQuery = parseQuery(query); |
| 157 | const testcases = await loader.loadCases(filterQuery); |
| 158 | |
| 159 | const expectations = []; |
| 160 | |
| 161 | const log = new Logger(); |
| 162 | |
| 163 | for (const testcase of testcases) { |
| 164 | const name = testcase.query.toString(); |
| 165 | |
| 166 | const wpt_fn = async () => { |
Brian Sheedy | 0653501 | 2022-08-11 14:39:51 +0000 | [diff] [blame] | 167 | sendMessageTestStarted(); |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 168 | const [rec, res] = log.record(name); |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 169 | |
| 170 | beginHeartbeatScope(); |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 171 | if (worker) { |
Austin Eng | b00c50e | 2022-08-26 22:34:27 +0000 | [diff] [blame] | 172 | await worker.run(rec, name, expectations); |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 173 | } else { |
Austin Eng | b00c50e | 2022-08-26 22:34:27 +0000 | [diff] [blame] | 174 | await testcase.run(rec, expectations); |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 175 | } |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 176 | endHeartbeatScope(); |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 177 | |
Brian Sheedy | 0653501 | 2022-08-11 14:39:51 +0000 | [diff] [blame] | 178 | sendMessageTestStatus(res.status, res.timems); |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 179 | sendMessageTestLog(res.logs); |
Brian Sheedy | 0653501 | 2022-08-11 14:39:51 +0000 | [diff] [blame] | 180 | sendMessageTestFinished(); |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 181 | }; |
| 182 | await wpt_fn(); |
| 183 | } |
| 184 | } |
| 185 | |
Brian Sheedy | 0653501 | 2022-08-11 14:39:51 +0000 | [diff] [blame] | 186 | function splitLogsForPayload(fullLogs) { |
| 187 | let logPieces = [fullLogs] |
| 188 | // Split the log pieces until they all are guaranteed to fit into a |
| 189 | // websocket payload. |
| 190 | while (true) { |
| 191 | let tempLogPieces = [] |
| 192 | for (const piece of logPieces) { |
| 193 | if (byteSize(piece) > LOGS_MAX_BYTES) { |
| 194 | let midpoint = Math.floor(piece.length / 2); |
| 195 | tempLogPieces.push(piece.substring(0, midpoint)); |
| 196 | tempLogPieces.push(piece.substring(midpoint)); |
| 197 | } else { |
| 198 | tempLogPieces.push(piece) |
| 199 | } |
| 200 | } |
| 201 | // Didn't make any changes - all pieces are under the size limit. |
| 202 | if (logPieces.every((value, index) => value == tempLogPieces[index])) { |
| 203 | break; |
| 204 | } |
| 205 | logPieces = tempLogPieces; |
| 206 | } |
| 207 | return logPieces |
| 208 | } |
| 209 | |
| 210 | function sendMessageTestStarted() { |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 211 | socket.send('{"type":"TEST_STARTED"}'); |
| 212 | } |
| 213 | |
| 214 | function sendMessageTestHeartbeat() { |
| 215 | socket.send('{"type":"TEST_HEARTBEAT"}'); |
Brian Sheedy | 0653501 | 2022-08-11 14:39:51 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | function sendMessageTestStatus(status, jsDurationMs) { |
Ben Clayton | 33bfc988 | 2023-01-05 21:44:37 +0000 | [diff] [blame] | 219 | socket.send(JSON.stringify({ |
| 220 | 'type': 'TEST_STATUS', |
| 221 | 'status': status, |
| 222 | 'js_duration_ms': jsDurationMs |
| 223 | })); |
Brian Sheedy | 0653501 | 2022-08-11 14:39:51 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 226 | function sendMessageTestLog(logs) { |
| 227 | splitLogsForPayload((logs ?? []).map(prettyPrintLog).join('\n\n')) |
| 228 | .forEach((piece) => { |
| 229 | socket.send(JSON.stringify({ |
| 230 | 'type': 'TEST_LOG', |
| 231 | 'log': piece |
| 232 | })); |
| 233 | }); |
Brian Sheedy | 0653501 | 2022-08-11 14:39:51 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | function sendMessageTestFinished() { |
Austin Eng | caa9bae | 2022-08-18 22:49:10 +0000 | [diff] [blame] | 237 | socket.send('{"type":"TEST_FINISHED"}'); |
Brian Sheedy | 0653501 | 2022-08-11 14:39:51 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 240 | window.runCtsTest = runCtsTest; |
| 241 | window.setupWebsocket = setupWebsocket |