blob: eb44de58a8663347c3269f7c50d35ad457045b71 [file] [log] [blame]
Austin Eng1cdea902022-03-24 00:21:55 +00001// 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
15import { DefaultTestFileLoader } from '../third_party/webgpu-cts/src/common/internal/file_loader.js';
16import { prettyPrintLog } from '../third_party/webgpu-cts/src/common/internal/logging/log_message.js';
17import { Logger } from '../third_party/webgpu-cts/src/common/internal/logging/logger.js';
18import { parseQuery } from '../third_party/webgpu-cts/src/common/internal/query/parseQuery.js';
19
20import { TestWorker } from '../third_party/webgpu-cts/src/common/runtime/helper/test_worker.js';
21
Brian Sheedy17b1a452022-04-14 17:19:11 +000022// 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.
24const LOGS_MAX_BYTES = 72000;
25
Austin Eng1cdea902022-03-24 00:21:55 +000026var socket;
27
Brian Sheedy17b1a452022-04-14 17:19:11 +000028function byteSize(s) {
29 return new Blob([s]).size;
30}
31
Austin Eng1cdea902022-03-24 00:21:55 +000032async function setupWebsocket(port) {
33 socket = new WebSocket('ws://127.0.0.1:' + port)
34 socket.addEventListener('message', runCtsTestViaSocket);
35}
36
37async function runCtsTestViaSocket(event) {
38 let input = JSON.parse(event.data);
39 runCtsTest(input['q'], input['w']);
40}
41
42async 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 Sheedy17b1a452022-04-14 17:19:11 +000065 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 Eng1cdea902022-03-24 00:21:55 +000094 };
95 await wpt_fn();
96 }
97}
98
99window.runCtsTest = runCtsTest;
100window.setupWebsocket = setupWebsocket