Split large CTS logs

Updates the CTS test runner JS to split logs into multiple
pieces if they're in danger of running into the payload
size limit that the test harness has.

crrev.com/c/3584705 must land in Chromium first.

Bug: chromium:1315658

Change-Id: I30ff416741515ce784116a2cbfbf193b6a4d71f1
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/86509
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Brian Sheedy <bsheedy@google.com>
diff --git a/webgpu-cts/test_runner.js b/webgpu-cts/test_runner.js
index 3f4eea2..eb44de5 100644
--- a/webgpu-cts/test_runner.js
+++ b/webgpu-cts/test_runner.js
@@ -19,8 +19,16 @@
 
 import { TestWorker } from '../third_party/webgpu-cts/src/common/runtime/helper/test_worker.js';
 
+// The Python-side websockets library has a max payload size of 72638. Set the
+// max allowable logs size in a single payload to a bit less than that.
+const LOGS_MAX_BYTES = 72000;
+
 var socket;
 
+function byteSize(s) {
+  return new Blob([s]).size;
+}
+
 async function setupWebsocket(port) {
   socket = new WebSocket('ws://127.0.0.1:' + port)
   socket.addEventListener('message', runCtsTestViaSocket);
@@ -54,8 +62,35 @@
         await testcase.run(rec, expectations);
       }
 
-      socket.send(JSON.stringify({'s': res.status,
-                                  'l': (res.logs ?? []).map(prettyPrintLog)}));
+      let fullLogs = (res.logs ?? []).map(prettyPrintLog);
+      fullLogs = fullLogs.join('\n\n\n');
+      let logPieces = [fullLogs]
+      // Split the log pieces until they all are guaranteed to fit into a
+      // websocket payload.
+      while (true) {
+        let tempLogPieces = []
+        for (const piece of logPieces) {
+          if (byteSize(piece) > LOGS_MAX_BYTES) {
+            let midpoint = Math.floor(piece.length / 2);
+            tempLogPieces.push(piece.substring(0, midpoint));
+            tempLogPieces.push(piece.substring(midpoint));
+          } else {
+            tempLogPieces.push(piece)
+          }
+        }
+        // Didn't make any changes - all pieces are under the size limit.
+        if (logPieces.every((value, index) => value == tempLogPieces[index])) {
+          break;
+        }
+        logPieces = tempLogPieces;
+      }
+
+      logPieces.forEach((piece, index, arr) => {
+        let isFinal = index == arr.length - 1;
+        socket.send(JSON.stringify({'s': res.status,
+                                    'l': piece,
+                                    'final': isFinal}));
+      });
     };
     await wpt_fn();
   }