Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

realtime-diagnostics.ts

28 lines1.1 KB

1import type { RawLiveQueryRow } from "@neon/realtime/client"
2import type { BoardTile } from "../../shared/tiles"
3​
4/**
5 * Size of board rows in protocol v1 JSON, including row keys and base64 wrappers.
6 * The SDK exposes decoded rows, so reconstruct their size without allocating
7 * base64 strings. This excludes message envelopes, separators and transport
8 * overhead; it is not a measurement of compressed network traffic.
9 */
10export function boardPayloadSize(rows: readonly RawLiveQueryRow<BoardTile>[]) {
11 let packedBytes = 0
12 let protocolRowBytes = 0
13 for (const { rowId, row } of rows) {
14 packedBytes += row.edges.byteLength + row.boxes.byteLength
15 protocolRowBytes +=
16 JSON.stringify({
17 row_key: rowId,
18 values: [String(row.tile_id), String(row.revision), { base64: "" }, { base64: "" }],
19 }).length +
20 4 * Math.ceil(row.edges.byteLength / 3) +
21 4 * Math.ceil(row.boxes.byteLength / 3)
22 }
23 return {
24 rows: rows.length,
25 packedKB: Number((packedBytes / 1000).toFixed(3)),
26 protocolRowsKB: Number((protocolRowBytes / 1000).toFixed(3)),
27 }