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 | |
| 15 | import { DefaultTestFileLoader } from '../third_party/webgpu-cts/src/common/internal/file_loader.js'; |
| 16 | import { prettyPrintLog } from '../third_party/webgpu-cts/src/common/internal/logging/log_message.js'; |
| 17 | import { Logger } from '../third_party/webgpu-cts/src/common/internal/logging/logger.js'; |
| 18 | import { parseQuery } from '../third_party/webgpu-cts/src/common/internal/query/parseQuery.js'; |
| 19 | |
| 20 | import { TestWorker } from '../third_party/webgpu-cts/src/common/runtime/helper/test_worker.js'; |
| 21 | |
Brian Sheedy | 17b1a45 | 2022-04-14 17:19:11 +0000 | [diff] [blame] | 22 | // The Python-side websockets library has a max payload size of 72638. Set the |
| 23 | // max allowable logs size in a single payload to a bit less than that. |
| 24 | const LOGS_MAX_BYTES = 72000; |
| 25 | |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 26 | var socket; |
| 27 | |
Brian Sheedy | 17b1a45 | 2022-04-14 17:19:11 +0000 | [diff] [blame] | 28 | function byteSize(s) { |
| 29 | return new Blob([s]).size; |
| 30 | } |
| 31 | |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 32 | async function setupWebsocket(port) { |
| 33 | socket = new WebSocket('ws://127.0.0.1:' + port) |
| 34 | socket.addEventListener('message', runCtsTestViaSocket); |
| 35 | } |
| 36 | |
| 37 | async function runCtsTestViaSocket(event) { |
| 38 | let input = JSON.parse(event.data); |
| 39 | runCtsTest(input['q'], input['w']); |
| 40 | } |
| 41 | |
| 42 | async function runCtsTest(query, use_worker) { |
| 43 | const workerEnabled = use_worker; |
| 44 | const worker = workerEnabled ? new TestWorker(false) : undefined; |
| 45 | |
| 46 | const loader = new DefaultTestFileLoader(); |
| 47 | const filterQuery = parseQuery(query); |
| 48 | const testcases = await loader.loadCases(filterQuery); |
| 49 | |
| 50 | const expectations = []; |
| 51 | |
| 52 | const log = new Logger(); |
| 53 | |
| 54 | for (const testcase of testcases) { |
| 55 | const name = testcase.query.toString(); |
| 56 | |
| 57 | const wpt_fn = async () => { |
| 58 | const [rec, res] = log.record(name); |
| 59 | if (worker) { |
| 60 | await worker.run(rec, name, expectations); |
| 61 | } else { |
| 62 | await testcase.run(rec, expectations); |
| 63 | } |
| 64 | |
Brian Sheedy | 17b1a45 | 2022-04-14 17:19:11 +0000 | [diff] [blame] | 65 | let fullLogs = (res.logs ?? []).map(prettyPrintLog); |
| 66 | fullLogs = fullLogs.join('\n\n\n'); |
| 67 | let logPieces = [fullLogs] |
| 68 | // Split the log pieces until they all are guaranteed to fit into a |
| 69 | // websocket payload. |
| 70 | while (true) { |
| 71 | let tempLogPieces = [] |
| 72 | for (const piece of logPieces) { |
| 73 | if (byteSize(piece) > LOGS_MAX_BYTES) { |
| 74 | let midpoint = Math.floor(piece.length / 2); |
| 75 | tempLogPieces.push(piece.substring(0, midpoint)); |
| 76 | tempLogPieces.push(piece.substring(midpoint)); |
| 77 | } else { |
| 78 | tempLogPieces.push(piece) |
| 79 | } |
| 80 | } |
| 81 | // Didn't make any changes - all pieces are under the size limit. |
| 82 | if (logPieces.every((value, index) => value == tempLogPieces[index])) { |
| 83 | break; |
| 84 | } |
| 85 | logPieces = tempLogPieces; |
| 86 | } |
| 87 | |
| 88 | logPieces.forEach((piece, index, arr) => { |
| 89 | let isFinal = index == arr.length - 1; |
| 90 | socket.send(JSON.stringify({'s': res.status, |
| 91 | 'l': piece, |
| 92 | 'final': isFinal})); |
| 93 | }); |
Austin Eng | 1cdea90 | 2022-03-24 00:21:55 +0000 | [diff] [blame] | 94 | }; |
| 95 | await wpt_fn(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | window.runCtsTest = runCtsTest; |
| 100 | window.setupWebsocket = setupWebsocket |