Public: anyone can read this; the crew can change it.
The games page shows a profile natively: flame graph, slowest ticks, per-mod time, linked findings #18178
Merged 3h 40m ago, live in production
mason/forge:mason/18140-the-games-page-shows-a-profile-natively → main 0 commits fork 3 threads · 3 unresolved
9 files
changed +122
−101
docs/features/game-spark.md +1 −1
| @@ -14,4 +14,4 @@ Installing spark (`mods_install spark`, or as a plugin of a new server) writes s | ||
| 14 | 14 | |
| 15 | 15 | ## The call tree and flame graph (#18208) |
| 16 | 16 | |
| 17 | A listed profile opens `/games/<box>/spark/<key>` (the bytes stay at `/games/<box>/@spark/<key>`): `decodeSparkProfile` reads it per request, and each thread shows its time sampled (milliseconds), a flame graph (an SVG drawn on the server, no script; a box is as wide as its share of the thread, the call and time in its tooltip) and the call tree as text with shares. The terminal shows the same lines: on a box's Profiles screen (`p`), `j`/`k` pick a profile and Enter opens `src/tui/screens/game_spark_view.ts`. Only the 10 heaviest threads are drawn, on both doors; the rest are named as "N more threads not drawn, download the profile". Only a grant on the box reads it; a body that is not a profile says why and links the download. The tree is the decoder's pruned one: 12 deep, 8 wide, at most 2,000 nodes a thread, from a body of at most 8 MiB (64 MiB unpacked). The page also lists the slowest ticks (window, max and median ms per tick, TPS, players) and the time per mod (source, ms, share), tables with no script, drawn by `src/web/pages/games_spark_ticks.ts`; the terminal ends the same screen with the same rows, and a profile with no mod data says so in one line. Not done: GC and memory are decoded but not shown; a flame box is not zoomable. Code: `src/games/spark_view.ts` (layout), `src/web/pages/games_spark_view.ts`. | |
| 17 | A listed profile opens `/games/<box>/spark/<key>` (the bytes stay at `/games/<box>/@spark/<key>`): `decodeSparkProfile` reads it per request, and each thread shows its time sampled (milliseconds), a flame graph (an SVG drawn on the server, no script; a box is as wide as its share of the thread, the call and time in its tooltip) and the call tree as text with shares. The terminal shows the same lines: on a box's Profiles screen (`p`), `j`/`k` pick a profile and Enter opens `src/tui/screens/game_spark_view.ts`. Only the 10 heaviest threads are drawn, on both doors; the rest are named as "N more threads not drawn, download the profile". Only a grant on the box reads it; a body that is not a profile says why and links the download. The tree is the decoder's pruned one: 12 deep, 8 wide, at most 2,000 nodes a thread, from a body of at most 8 MiB (64 MiB unpacked). Under the threads, both doors show (#18140, `summarySections`) the slowest tick windows, time per mod, GC and memory, and the mods listed. Not done: a finding that links to a slow frame; a flame box is not zoomable. Code: `src/games/spark_view.ts` (layout), `src/web/pages/games_spark_view.ts`. |
src/games/spark_view.ts +39 −13
| @@ -82,18 +82,44 @@ export function sparkHeadLines(s: SparkSummary): string[] { | ||
| 82 | 82 | ]; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | /** | |
| 86 | * The slowest ticks as rows of cells, in the words both doors use (`ms` per tick, `tps` ticks/s). | |
| 87 | */ | |
| 88 | export function slowTickRows(s: SparkSummary): string[][] { | |
| 89 | return s.slowestTicks.map((t) => [`window ${String(t.window)}`, millis(t.msptMax), millis(t.msptMedian), t.tps.toFixed(1), String(t.players)]); | |
| 90 | } | |
| 91 | export const SLOW_TICK_HEAD = ["Window", "Max per tick", "Median per tick", "TPS", "Players"]; | |
| 85 | export interface SparkFact { readonly label: string; readonly value: string } | |
| 86 | export interface SparkRow { readonly title: string; readonly facts: readonly SparkFact[] } | |
| 87 | export interface SparkSection { readonly id: string; readonly title: string; readonly rows: readonly SparkRow[]; readonly empty: string } | |
| 92 | 88 | |
| 93 | /** Time per mod as rows: source, ms sampled, share of the time attributed to a mod. */ | |
| 94 | export function modTimeRows(s: SparkSummary): string[][] { | |
| 95 | return s.perMod.map((m) => [m.source, millis(m.ms), percent(m.share)]); | |
| 89 | const mib = (bytes: number): string => `${String(Math.round(bytes / 1048576))} MiB`; | |
| 90 | const used = (title: string, m: { readonly used: number; readonly total: number }): SparkRow => ({ title, facts: [{ label: "Used", value: mib(m.used) }, { label: "Total", value: mib(m.total) }] }); | |
| 91 | ||
| 92 | /** | |
| 93 | * What a profile holds besides its threads (#18140), as sections of rows, each a title and | |
| 94 | * label/value facts: the slowest tick windows, time per mod, GC and memory, and the mods listed. | |
| 95 | * The page draws the rows as facts, the terminal joins the same data (`sectionLines`). A section | |
| 96 | * with no rows carries its `empty` sentence. | |
| 97 | */ | |
| 98 | export function summarySections(s: SparkSummary): SparkSection[] { | |
| 99 | return [ | |
| 100 | { | |
| 101 | id: "ticks", title: "Slowest ticks", empty: "The profile recorded no tick windows.", | |
| 102 | rows: s.slowestTicks.map((t, i) => ({ title: `Window ${String(i + 1)}`, facts: [ | |
| 103 | { label: "Worst tick", value: `${String(t.msptMax)} ms` }, { label: "Median", value: `${String(t.msptMedian)} ms` }, | |
| 104 | { label: "TPS", value: String(t.tps) }, { label: "Ticks", value: String(t.ticks) }, { label: "Players", value: String(t.players) }, | |
| 105 | ] })), | |
| 106 | }, | |
| 107 | { | |
| 108 | id: "mods", title: "Time per mod", empty: "The profile attributes no time to a mod.", | |
| 109 | rows: s.perMod.map((m) => ({ title: m.source, facts: [{ label: "Share", value: `${String(Math.round(m.share * 100))}%` }, { label: "Time", value: `${String(m.ms)} ms` }] })), | |
| 110 | }, | |
| 111 | { | |
| 112 | id: "system", title: "GC and memory", empty: "", | |
| 113 | rows: [ | |
| 114 | ...s.gc.map((g) => ({ title: g.name, facts: [{ label: "Collections", value: String(g.collections) }, { label: "Each", value: `${String(g.avgTimeMs)} ms` }, { label: "Every", value: `${String(g.avgFrequencyMs)} ms` }] })), | |
| 115 | used("Heap", s.memory.heap), used("Non-heap", s.memory.nonHeap), used("Machine memory", s.memory.physical), | |
| 116 | ], | |
| 117 | }, | |
| 118 | { id: "platform", title: "Mods listed", empty: "The profile lists none.", rows: s.mods.map((m) => ({ title: m.name || m.id, facts: [{ label: "Version", value: m.version }] })) }, | |
| 119 | ]; | |
| 120 | } | |
| 121 | ||
| 122 | /** A section as the terminal's lines: one per row, `Title: Label value, Label value`. */ | |
| 123 | export function sectionLines(x: SparkSection): string[] { | |
| 124 | return x.rows.length === 0 ? [x.empty] : x.rows.map((r) => `${r.title}: ${r.facts.map((f) => `${f.label} ${f.value}`).join(", ")}`); | |
| 96 | 125 | } |
| 97 | export const MOD_TIME_HEAD = ["Source", "Time", "Share"]; | |
| 98 | export const NO_TICKS = "This profile holds no per-window tick statistics."; | |
| 99 | export const NO_MODS = "This profile has no mod data: no frame was attributed to a mod."; |
src/tui/screens/game_spark_view.ts +2 −5
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import { READ_UNPACKED, decodeSparkProfile } from "../../games/spark_profile.ts"; |
| 8 | 8 | import { profileOf } from "../../games/spark.ts"; |
| 9 | import { MOD_TIME_HEAD, NO_MODS, NO_TICKS, SLOW_TICK_HEAD, heaviest, millis, modTimeRows, slowTickRows, sparkHeadLines, treeLines } from "../../games/spark_view.ts"; | |
| 9 | import { heaviest, millis, sectionLines, sparkHeadLines, summarySections, treeLines } from "../../games/spark_view.ts"; | |
| 10 | 10 | import type { GameBox } from "../../games/store.ts"; |
| 11 | 11 | import type { Ctx, Screen } from "../app/app.ts"; |
| 12 | 12 | import { binding } from "../app/keys.ts"; |
| @@ -14,9 +14,6 @@ import { style } from "../term/style.ts"; | ||
| 14 | 14 | import { truncate } from "../term/width.ts"; |
| 15 | 15 | import type { Session } from "../session.ts"; |
| 16 | 16 | |
| 17 | /** A titled table as lines: the head, then each row, cells apart by two spaces. */ | |
| 18 | const section = (title: string, head: string[], rows: string[][], none: string): string[] => ["", `== ${title}`, ...(rows.length === 0 ? [none] : [head.join(" "), ...rows.map((r) => r.join(" "))])]; | |
| 19 | ||
| 20 | 17 | export function gameSparkViewScreen(f: Session, ctx: Ctx, box: GameBox, key: string): Screen { |
| 21 | 18 | let top = 0; |
| 22 | 19 | let cached: string[] | undefined; |
| @@ -32,7 +29,7 @@ export function gameSparkViewScreen(f: Session, ctx: Ctx, box: GameBox, key: str | ||
| 32 | 29 | if (read === undefined || !read.ok) return ["The forge draws a sampler profile only, and this one is not a readable one: download it from the box's page."]; |
| 33 | 30 | const { drawn, note } = heaviest(read.summary.threads); |
| 34 | 31 | const [head = ""] = sparkHeadLines(read.summary); |
| 35 | return [head, "Times are milliseconds sampled, shares are of the thread; trees keep the heaviest calls.", ...drawn.flatMap((t) => ["", `== ${t.name || "(unnamed thread)"}: ${millis(t.ms)} sampled`, ...(t.tree.length === 0 ? ["Nothing was sampled in this thread."] : treeLines(t.tree, t.ms))]), ...(note === "" ? [] : ["", note]), ...section("Slowest ticks", SLOW_TICK_HEAD, slowTickRows(read.summary), NO_TICKS), ...section("Time per mod", MOD_TIME_HEAD, modTimeRows(read.summary), NO_MODS)]; | |
| 32 | return [head, "Times are milliseconds sampled, shares are of the thread; trees keep the heaviest calls.", ...drawn.flatMap((t) => ["", `== ${t.name || "(unnamed thread)"}: ${millis(t.ms)} sampled`, ...(t.tree.length === 0 ? ["Nothing was sampled in this thread."] : treeLines(t.tree, t.ms))]), ...(note === "" ? [] : ["", note]), ...summarySections(read.summary).flatMap((x) => ["", `== ${x.title}`, ...sectionLines(x)])]; | |
| 36 | 33 | }; |
| 37 | 34 | return { |
| 38 | 35 | title: `${box.name}: profile ${key}`, |
src/web/pages/games_spark_ticks.ts deleted −23
| @@ -1,23 +0,0 @@ | ||
| 1 | /** | |
| 2 | * The slowest ticks and the time per mod of a decoded spark profile (#18212), server-rendered | |
| 3 | * tables, no script. Kept apart from the call tree (`games_spark_view.ts`) so the two change | |
| 4 | * alone. The terminal shows the same rows (`tui/screens/game_spark_view.ts`). | |
| 5 | */ | |
| 6 | import type { SparkSummary } from "../../games/spark_profile.ts"; | |
| 7 | import { MOD_TIME_HEAD, NO_MODS, NO_TICKS, SLOW_TICK_HEAD, modTimeRows, slowTickRows } from "../../games/spark_view.ts"; | |
| 8 | import { html, type Html } from "../html.ts"; | |
| 9 | ||
| 10 | const grid = (id: string, caption: string, head: string[], rows: string[][]): Html => html`<div class="table-scroll"><table class="table" id="${id}" data-dense> | |
| 11 | <caption class="sr-only">${caption}</caption> | |
| 12 | <thead><tr>${head.map((h, i) => html`<th scope="col"${i > 0 ? html` class="num"` : null}>${h}</th>`)}</tr></thead> | |
| 13 | <tbody>${rows.map((r) => html`<tr>${r.map((c, i) => (i === 0 ? html`<th scope="row">${c}</th>` : html`<td class="num">${c}</td>`))}</tr>`)}</tbody> | |
| 14 | </table></div>`; | |
| 15 | ||
| 16 | export function sparkTicksAndMods(s: SparkSummary): Html { | |
| 17 | const ticks = slowTickRows(s); | |
| 18 | const mods = modTimeRows(s); | |
| 19 | return html`<section class="settings-block"><h2>Slowest ticks</h2> | |
| 20 | ${ticks.length === 0 ? html`<p class="muted small">${NO_TICKS}</p>` : grid("spark-ticks", "Slowest tick windows", SLOW_TICK_HEAD, ticks)}</section> | |
| 21 | <section class="settings-block"><h2>Time per mod</h2> | |
| 22 | ${mods.length === 0 ? html`<p class="muted small">${NO_MODS}</p>` : grid("spark-mods", "Time sampled per mod", MOD_TIME_HEAD, mods)}</section>`; | |
| 23 | } |
src/web/pages/games_spark_view.ts +4 −4
| @@ -6,12 +6,12 @@ | ||
| 6 | 6 | import type { Ctx } from "../../http/context.ts"; |
| 7 | 7 | import { READ_UNPACKED, decodeSparkProfile, type SparkSummary } from "../../games/spark_profile.ts"; |
| 8 | 8 | import { profileOf } from "../../games/spark.ts"; |
| 9 | import { FLAME_WIDTH, flameRects, heaviest, millis, sparkHeadLines, treeLines } from "../../games/spark_view.ts"; | |
| 9 | import { FLAME_WIDTH, flameRects, heaviest, millis, sparkHeadLines, summarySections, treeLines } from "../../games/spark_view.ts"; | |
| 10 | 10 | import { notFoundPage, pageHead } from "../chrome.ts"; |
| 11 | 11 | import { html, type Html } from "../html.ts"; |
| 12 | 12 | import { page } from "../layout.ts"; |
| 13 | import { row, rowList } from "../ui.ts"; | |
| 13 | 14 | import { boxOf } from "./games.ts"; |
| 14 | import { sparkTicksAndMods } from "./games_spark_ticks.ts"; | |
| 15 | 15 | |
| 16 | 16 | const SAMPLER = "application/x-spark-sampler"; |
| 17 | 17 | const ROW = 18; |
| @@ -54,8 +54,8 @@ export function gameSparkView(ctx: Ctx): Response { | ||
| 54 | 54 | ${t.tree.length === 0 ? html`<p class="muted small">Nothing was sampled in this thread.</p>` : html`${flameSvg(t)} |
| 55 | 55 | <details ${i === 0 ? html`open` : null}><summary>Call tree</summary><pre>${treeLines(t.tree, t.ms).join("\n")}</pre></details>`} |
| 56 | 56 | </section>`)} |
| 57 | ${sparkTicksAndMods(read.summary)} | |
| 58 | ${shown?.note ? html`<p class="muted small">${shown.note} <a href="${href}">Download it</a>.</p>` : null}`; | |
| 57 | ${shown?.note ? html`<p class="muted small">${shown.note} <a href="${href}">Download it</a>.</p>` : null} | |
| 58 | ${summarySections(read.summary).map((x) => html`<section class="settings-block" id="${x.id}"><h2>${x.title}</h2>${x.rows.length === 0 ? html`<p class="muted small">${x.empty}</p>` : rowList(x.rows.map((r) => row({ title: r.title, facts: r.facts })))}</section>`)}`; | |
| 59 | 59 | return page({ |
| 60 | 60 | ctx, |
| 61 | 61 | title: `Games · ${got.box.name} · profile`, |
test/games-spark-profile.test.ts +3 −43
| @@ -1,50 +1,10 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { gzipSync } from "node:zlib"; |
| 3 | import { bytes, entry, key, packedDoubles, sparkFixture, text, varint } from "./helpers/spark_fixture.ts"; | |
| 3 | 4 | import { decodeSparkProfile, MAX_PACKED, SLOWEST } from "../src/games/spark_profile.ts"; |
| 4 | 5 | |
| 5 | // A tiny protobuf writer: the fixture is built here so the test says what it holds. | |
| 6 | const varint = (n: number): number[] => { const o: number[] = []; while (n > 127) { o.push((n % 128) | 128); n = Math.floor(n / 128); } o.push(n); return o; }; | |
| 7 | const key = (n: number, t: number) => varint(n * 8 + t); | |
| 8 | const vint = (n: number, v: number) => [...key(n, 0), ...varint(v)]; | |
| 9 | const bytes = (n: number, b: number[] | Uint8Array) => [...key(n, 2), ...varint(b.length), ...b]; | |
| 10 | const text = (n: number, s: string) => bytes(n, new TextEncoder().encode(s)); | |
| 11 | const dbl = (v: number) => [...new Uint8Array(new Float64Array([v]).buffer)]; | |
| 12 | const packedDoubles = (n: number, vs: number[]) => bytes(n, vs.flatMap(dbl)); | |
| 13 | const entry = (n: number, k: number[], v: number[]) => bytes(n, [...k, ...v]); | |
| 14 | const strEntry = (n: number, k: string, v: number[]) => entry(n, text(1, k), bytes(2, v)); | |
| 15 | /** | |
| 16 | * A data version 1 node: one `time` (field 1) and its children nested (field 2), both now reserved. | |
| 17 | */ | |
| 18 | const node = (cls: string, method: string, ms: number, kids: number[][] = []) => [...text(3, cls), ...text(4, method), ...key(1, 1), ...dbl(ms), ...kids.flatMap((k) => bytes(2, k))]; | |
| 19 | const pool = (used: number, total: number) => [...vint(1, used), ...vint(2, total)]; | |
| 20 | ||
| 21 | function fixture(): Uint8Array { | |
| 22 | const tree = node("net.minecraft.server.MinecraftServer", "tick", 100, [ | |
| 23 | node("net.minecraft.world.level.Level", "tickEntities", 60, [node("com.example.lithium.Algo", "run", 40)]), | |
| 24 | node("com.example.chunky.Task", "step", 25), | |
| 25 | ]); | |
| 26 | const thread = [...text(1, "Server thread"), ...key(2, 1), ...dbl(100), ...bytes(3, tree)]; | |
| 27 | const win = (ticks: number, max: number) => [...vint(1, ticks), ...[...key(6, 1), ...dbl(max)], ...[...key(5, 1), ...dbl(max / 2)], ...[...key(4, 1), ...dbl(20)]]; | |
| 28 | const meta = [ | |
| 29 | ...vint(2, 1_700_000_000_000), ...vint(11, 1_700_000_060_000), | |
| 30 | ...bytes(7, [...vint(1, 0), ...text(2, "Fabric"), ...text(3, "0.15"), ...text(4, "1.21")]), | |
| 31 | ...bytes(8, [...bytes(1, [...bytes(1, pool(512, 1024)), ...bytes(2, pool(64, 128))]), ...strEntry(2, "G1 Young", [...vint(1, 12), ...[...key(2, 1), ...dbl(7.5)], ...[...key(3, 1), ...dbl(900)]])]), | |
| 32 | ...bytes(9, bytes(2, bytes(1, pool(4000, 8000)))), | |
| 33 | ...strEntry(13, "lithium", [...text(1, "Lithium"), ...text(2, "0.12")]), | |
| 34 | ...strEntry(13, "chunky", [...text(1, "Chunky"), ...text(2, "1.4")]), | |
| 35 | ]; | |
| 36 | const raw = [ | |
| 37 | ...bytes(1, meta), ...bytes(2, thread), | |
| 38 | ...entry(3, text(1, "com.example.lithium.Algo"), text(2, "lithium")), | |
| 39 | ...entry(3, text(1, "com.example.chunky.Task"), text(2, "chunky")), | |
| 40 | ...entry(7, vint(1, 1), bytes(2, win(1200, 80))), | |
| 41 | ...entry(7, vint(1, 2), bytes(2, win(1200, 300))), | |
| 42 | ]; | |
| 43 | return gzipSync(Uint8Array.from(raw)); | |
| 44 | } | |
| 45 | ||
| 46 | 6 | test("a data version 1 profile (nested nodes, one time each) decodes to its summary", () => { |
| 47 | const r = decodeSparkProfile(fixture()); | |
| 7 | const r = decodeSparkProfile(sparkFixture()); | |
| 48 | 8 | if (!r.ok) throw new Error(r.error.message); |
| 49 | 9 | const s = r.summary; |
| 50 | 10 | expect(s.platform).toEqual({ type: "server", name: "Fabric", version: "0.15", minecraft: "1.21" }); |
| @@ -92,7 +52,7 @@ test("a profile encoded from spark's own protos decodes to its summary", () => { | ||
| 92 | 52 | }); |
| 93 | 53 | |
| 94 | 54 | test("a truncated profile is refused by name, never thrown", () => { |
| 95 | const whole = fixture(); | |
| 55 | const whole = sparkFixture(); | |
| 96 | 56 | const r = decodeSparkProfile(whole.slice(0, whole.length - 12)); |
| 97 | 57 | expect(r.ok).toBe(false); |
| 98 | 58 | if (!r.ok) expect(r.error.name).toBe("truncated"); |
test/games-spark-view.test.ts +27 −10
| @@ -16,14 +16,11 @@ import { decodeSparkProfile, type TreeNode } from "../src/games/spark_profile.ts | ||
| 16 | 16 | import { FLAME_WIDTH, flameRects, treeLines } from "../src/games/spark_view.ts"; |
| 17 | 17 | import { boxNamed, createGameBox, grant } from "../src/games/store.ts"; |
| 18 | 18 | import { forgeHttp, type ForgeHttp } from "../src/http/server.ts"; |
| 19 | import { bytes, key, text, varint } from "./helpers/spark_fixture.ts"; | |
| 19 | 20 | import { quietLogs } from "./helpers/globals.ts"; |
| 20 | 21 | |
| 21 | 22 | quietLogs(); |
| 22 | 23 | |
| 23 | const varint = (n: number): number[] => { const o: number[] = []; while (n > 127) { o.push((n % 128) | 128); n = Math.floor(n / 128); } o.push(n); return o; }; | |
| 24 | const key = (n: number, t: number) => varint(n * 8 + t); | |
| 25 | const bytes = (n: number, b: number[]) => [...key(n, 2), ...varint(b.length), ...b]; | |
| 26 | const text = (n: number, s: string) => bytes(n, [...new TextEncoder().encode(s)]); | |
| 27 | 24 | const dbl = (v: number) => [...new Uint8Array(new Float64Array([v]).buffer)]; |
| 28 | 25 | // A data version 1 node: time (1), nested children (2). |
| 29 | 26 | const node = (cls: string, method: string, ms: number, kids: number[][] = []) => [...text(3, cls), ...text(4, method), ...key(1, 1), ...dbl(ms), ...kids.flatMap((k) => bytes(2, k))]; |
| @@ -110,11 +107,34 @@ test("only the heaviest threads are drawn, and the rest are counted, on both doo | ||
| 110 | 107 | expect((body.match(/aria-label="Flame graph of/g) ?? []).length).toBe(10); |
| 111 | 108 | expect(body).toContain("3 more threads not drawn, download the profile."); |
| 112 | 109 | expect(body).toContain("T12"); |
| 110 | expect(body.indexOf("3 more threads not drawn")).toBeLessThan(body.indexOf('id="ticks"')); | |
| 113 | 111 | expect(body).not.toContain("T0<"); |
| 114 | 112 | const { heaviest } = await import("../src/games/spark_view.ts"); |
| 115 | 113 | expect(heaviest([{ ms: 1 }, { ms: 5 }]).note).toBe(""); |
| 116 | 114 | }); |
| 117 | 115 | |
| 116 | test("the slowest ticks, time per mod, GC and memory are shown, on both doors (#18140)", async () => { | |
| 117 | const { sparkFixture } = await import("./helpers/spark_fixture.ts"); | |
| 118 | const { summarySections, sectionLines } = await import("../src/games/spark_view.ts"); | |
| 119 | const box = boxNamed(app.db, "net")!; | |
| 120 | const k = keepProfile(app.db, box.id, "application/x-spark-sampler", sparkFixture()); | |
| 121 | if (!k.ok) throw new Error(k.error); | |
| 122 | const body = await (await get(`/games/net/spark/${k.value}`, "bob")).text(); | |
| 123 | const r = decodeSparkProfile(sparkFixture()); | |
| 124 | if (!r.ok) throw new Error(r.error.message); | |
| 125 | const sections = summarySections(r.summary); | |
| 126 | expect(sections.map((s) => s.id)).toEqual(["ticks", "mods", "system", "platform"]); | |
| 127 | expect(sections[0]!.rows[0]!.facts[0]).toEqual({ label: "Worst tick", value: "300 ms" }); | |
| 128 | expect(sectionLines(sections[1]!).join("\n")).toContain("lithium"); | |
| 129 | expect(sectionLines(sections[2]!).join("\n")).toContain("G1 Young: Collections 12"); | |
| 130 | // Every row the terminal shows is on the page, as a title and a label/value pair each. | |
| 131 | for (const s of sections) { | |
| 132 | expect(body).toContain(s.title); | |
| 133 | for (const r of s.rows) { expect(body).toContain(`>${r.title}</span`); for (const f of r.facts) expect(body).toContain(`<dt>${f.label}</dt><dd>${f.value}</dd>`); } | |
| 134 | } | |
| 135 | expect(sectionLines(summarySections({ ...r.summary, slowestTicks: [], perMod: [], mods: [] })[3]!)).toEqual(["The profile lists none."]); | |
| 136 | }); | |
| 137 | ||
| 118 | 138 | test("the page lists the slowest ticks and time per mod, escaping a hostile mod name, and says when there is none (#18212)", async () => { |
| 119 | 139 | const win = (ticks: number, max: number) => [...key(1, 0), ...varint(ticks), ...key(6, 1), ...dbl(max), ...key(5, 1), ...dbl(max / 2), ...key(4, 1), ...dbl(19.5), ...key(7, 0), ...varint(3)]; |
| 120 | 140 | const entry = (n: number, k: number[], v: number[]) => bytes(n, [...k, ...v]); |
| @@ -127,13 +147,10 @@ test("the page lists the slowest ticks and time per mod, escaping a hostile mod | ||
| 127 | 147 | const keep = (b: Uint8Array) => { const k = keepProfile(app.db, box.id, "application/x-spark-sampler", b); if (!k.ok) throw new Error(k.error); return k.value; }; |
| 128 | 148 | const body = await (await get(`/games/net/spark/${keep(gzipSync(Uint8Array.from(raw)))}`, "bob")).text(); |
| 129 | 149 | expect(body).toContain("Slowest ticks"); |
| 130 | expect(body).toContain("80.0 ms"); | |
| 131 | expect(body).toContain("40.0 ms"); | |
| 132 | expect(body).toContain("100.0%"); | |
| 150 | expect(body).toContain("80 ms"); | |
| 151 | expect(body).toContain("40 ms"); | |
| 152 | expect(body).toContain("100%"); | |
| 133 | 153 | expect(body).toContain("Time per mod"); |
| 134 | 154 | expect(body).not.toContain("<img src=x"); |
| 135 | 155 | expect(body).toContain("<img src=x"); |
| 136 | const none = await (await get(`/games/net/spark/${tree}`, "bob")).text(); | |
| 137 | expect(none).toContain("This profile has no mod data"); | |
| 138 | expect(none.slice(none.indexOf("Slowest ticks"), none.indexOf("</main>"))).not.toContain("<script"); | |
| 139 | 156 | }); |
test/helpers/spark_fixture.ts added +44
| @@ -0,0 +1,44 @@ | ||
| 1 | /** A data version 1 spark profile built by hand, shared by the decoder and page tests (#18140). */ | |
| 2 | import { gzipSync } from "node:zlib"; | |
| 3 | ||
| 4 | // A tiny protobuf writer: the fixture is built here so the test says what it holds. | |
| 5 | export const varint = (n: number): number[] => { const o: number[] = []; while (n > 127) { o.push((n % 128) | 128); n = Math.floor(n / 128); } o.push(n); return o; }; | |
| 6 | export const key = (n: number, t: number) => varint(n * 8 + t); | |
| 7 | export const vint = (n: number, v: number) => [...key(n, 0), ...varint(v)]; | |
| 8 | export const bytes = (n: number, b: number[] | Uint8Array) => [...key(n, 2), ...varint(b.length), ...b]; | |
| 9 | export const text = (n: number, s: string) => bytes(n, new TextEncoder().encode(s)); | |
| 10 | export const dbl = (v: number) => [...new Uint8Array(new Float64Array([v]).buffer)]; | |
| 11 | export const packedDoubles = (n: number, vs: number[]) => bytes(n, vs.flatMap(dbl)); | |
| 12 | export const entry = (n: number, k: number[], v: number[]) => bytes(n, [...k, ...v]); | |
| 13 | export const strEntry = (n: number, k: string, v: number[]) => entry(n, text(1, k), bytes(2, v)); | |
| 14 | /** | |
| 15 | * A data version 1 node: one `time` (field 1) and its children nested (field 2), both now reserved. | |
| 16 | */ | |
| 17 | export const node = (cls: string, method: string, ms: number, kids: number[][] = []) => [...text(3, cls), ...text(4, method), ...key(1, 1), ...dbl(ms), ...kids.flatMap((k) => bytes(2, k))]; | |
| 18 | export const pool = (used: number, total: number) => [...vint(1, used), ...vint(2, total)]; | |
| 19 | ||
| 20 | export function sparkFixture(algo = "com.example.lithium.Algo"): Uint8Array { | |
| 21 | const tree = node("net.minecraft.server.MinecraftServer", "tick", 100, [ | |
| 22 | node("net.minecraft.world.level.Level", "tickEntities", 60, [node(algo, "run", 40)]), | |
| 23 | node("com.example.chunky.Task", "step", 25), | |
| 24 | ]); | |
| 25 | const thread = [...text(1, "Server thread"), ...key(2, 1), ...dbl(100), ...bytes(3, tree)]; | |
| 26 | const win = (ticks: number, max: number) => [...vint(1, ticks), ...[...key(6, 1), ...dbl(max)], ...[...key(5, 1), ...dbl(max / 2)], ...[...key(4, 1), ...dbl(20)]]; | |
| 27 | const meta = [ | |
| 28 | ...vint(2, 1_700_000_000_000), ...vint(11, 1_700_000_060_000), | |
| 29 | ...bytes(7, [...vint(1, 0), ...text(2, "Fabric"), ...text(3, "0.15"), ...text(4, "1.21")]), | |
| 30 | ...bytes(8, [...bytes(1, [...bytes(1, pool(512, 1024)), ...bytes(2, pool(64, 128))]), ...strEntry(2, "G1 Young", [...vint(1, 12), ...[...key(2, 1), ...dbl(7.5)], ...[...key(3, 1), ...dbl(900)]])]), | |
| 31 | ...bytes(9, bytes(2, bytes(1, pool(4000, 8000)))), | |
| 32 | ...strEntry(13, "lithium", [...text(1, "Lithium"), ...text(2, "0.12")]), | |
| 33 | ...strEntry(13, "chunky", [...text(1, "Chunky"), ...text(2, "1.4")]), | |
| 34 | ]; | |
| 35 | const raw = [ | |
| 36 | ...bytes(1, meta), ...bytes(2, thread), | |
| 37 | ...entry(3, text(1, algo), text(2, "lithium")), | |
| 38 | ...entry(3, text(1, "com.example.chunky.Task"), text(2, "chunky")), | |
| 39 | ...entry(7, vint(1, 1), bytes(2, win(1200, 80))), | |
| 40 | ...entry(7, vint(1, 2), bytes(2, win(1200, 300))), | |
| 41 | ]; | |
| 42 | return gzipSync(Uint8Array.from(raw)); | |
| 43 | } | |
| 44 |
test/tui-games.test.ts +2 −2
| @@ -455,10 +455,10 @@ test("Enter on a stored spark profile opens its call tree in the terminal, as th | ||
| 455 | 455 | await owner.type("\r"); |
| 456 | 456 | await settleUntil(owner.tui, "Main thread"); |
| 457 | 457 | expect(owner.tui.screen.toText()).toContain("100.0% 100.0 ms tick"); |
| 458 | // The terminal ends with the page's two lists, here with no ticks and no mod data (#18212). | |
| 458 | // The terminal ends with the page's two lists, here with no ticks and no mod data (#18212, #18140). | |
| 459 | 459 | for (let i = 0; i < 40; i++) await owner.type("j"); |
| 460 | 460 | await settleUntil(owner.tui, "Time per mod"); |
| 461 | expect(owner.tui.screen.toText()).toContain("This profile has no mod data"); | |
| 461 | expect(owner.tui.screen.toText()).toContain("The profile attributes no time to a mod"); | |
| 462 | 462 | } finally { |
| 463 | 463 | owner.tui.stop?.(); |
| 464 | 464 | } |
new above
offline
Principle 7: a fact is a dl row, not a sentence. Return {label, value} pairs (e.g. 'Worst tick' -> '300 ms', 'Median' -> '150 ms', 'TPS', 'Ticks', 'Players') and let the page render them as row facts; the terminal can join them. Also '1 players' needs its plural.