126 lines
6.6 KiB
JavaScript
126 lines
6.6 KiB
JavaScript
// The pure parts of the profiler's flame graph and charts.
|
||
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/flame.test.mjs
|
||
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { nodeSpace, layoutFlame, hitTest, searchNodes, rangeTotals, brushSlices, formatMs, formatBytes, formatCount, shortName, valueOf, colorOf } from './flame.js';
|
||
|
||
const tree = {
|
||
n: '(root)', f: '', v: 100, m: 1000, c: 1, ch: [
|
||
{ n: 'App\\Scene\\PlayScene->update', f: 'src/Scene/PlayScene.php', v: 60, m: 800, c: 10, ch: [
|
||
{ n: 'App\\Entity\\Hero->update', f: 'src/Entity/Hero.php', v: 40, m: 300, c: 10 },
|
||
{ n: 'php::count', f: 'php:internal', v: 5, m: -50, c: 30 },
|
||
] },
|
||
{ n: 'JumPHP\\Renderer\\Renderer->endFrame', f: '/workspace/engine/src/Renderer/Renderer.php', v: 30, m: 100, c: 10 },
|
||
],
|
||
};
|
||
|
||
test('nodeSpace tells app, engine, vendor and php like the PHP side', () => {
|
||
assert.equal(nodeSpace('JumPHP\\Kernel\\Engine->tick', '/workspace/engine/src/Kernel/Engine.php'), 'engine');
|
||
assert.equal(nodeSpace('App\\Scene\\PlayScene->update', 'src/Scene/PlayScene.php'), 'app');
|
||
assert.equal(nodeSpace('React\\EventLoop\\Loop->run', '/workspace/games/x/vendor/react/Loop.php'), 'vendor');
|
||
assert.equal(nodeSpace('php::str_repeat', 'php:internal'), 'php');
|
||
assert.equal(nodeSpace('(root)', ''), 'root');
|
||
});
|
||
|
||
test('layout: widths are proportional to the values, a child never exceeds its parent', () => {
|
||
const l = layoutFlame(tree);
|
||
const root = l.rects[0];
|
||
assert.equal(root.x0, 0); assert.equal(root.x1, 1);
|
||
const update = l.rects.find((r) => r.node.n.endsWith('PlayScene->update'));
|
||
assert.ok(Math.abs((update.x1 - update.x0) - 0.6) < 1e-9);
|
||
const hero = l.rects.find((r) => r.node.n.endsWith('Hero->update'));
|
||
assert.ok(Math.abs((hero.x1 - hero.x0) - 0.4) < 1e-9);
|
||
assert.equal(hero.depth, 2);
|
||
assert.equal(hero.x0, update.x0);
|
||
for (const r of l.rects) for (const c of l.rects.filter((c) => c.depth === r.depth + 1 && c.x0 >= r.x0 && c.x1 <= r.x1 + 1e-9)) assert.ok(c.x1 <= r.x1 + 1e-9);
|
||
assert.equal(l.maxDepth, 2);
|
||
});
|
||
|
||
test('layout: children that add up to more than their parent are scaled down to it', () => {
|
||
const t = { n: 'a', v: 10, ch: [{ n: 'b', v: 8 }, { n: 'c', v: 8 }] };
|
||
const l = layoutFlame(t);
|
||
const widths = l.rects.slice(1).map((r) => r.x1 - r.x0);
|
||
assert.ok(Math.abs(widths[0] + widths[1] - 1) < 1e-9);
|
||
});
|
||
|
||
test('layout: zooming on a node makes it the whole width', () => {
|
||
const l = layoutFlame(tree, { focus: [0] });
|
||
assert.equal(l.rects[0].node.n, 'App\\Scene\\PlayScene->update');
|
||
assert.equal(l.rects[0].x1, 1);
|
||
const hero = l.rects.find((r) => r.node.n.endsWith('Hero->update'));
|
||
assert.ok(Math.abs((hero.x1 - hero.x0) - 40 / 60) < 1e-9);
|
||
assert.deepEqual(hero.path, [0, 0]);
|
||
assert.equal(l.total, 60);
|
||
});
|
||
|
||
test('layout by memory ignores negative memory and sizes by bytes', () => {
|
||
const l = layoutFlame(tree, { metric: 'm' });
|
||
assert.equal(l.rects.find((r) => r.node.n === 'php::count'), undefined); // zero width: not drawn
|
||
assert.equal(valueOf(tree.ch[0].ch[1], 'm'), 0);
|
||
const update = l.rects.find((r) => r.node.n.endsWith('PlayScene->update'));
|
||
assert.ok(Math.abs((update.x1 - update.x0) - 0.8) < 1e-9);
|
||
});
|
||
|
||
test('tiny nodes are dropped from the layout', () => {
|
||
const t = { n: 'a', v: 1000, ch: [{ n: 'big', v: 999 }, { n: 'tiny', v: 0.1 }] };
|
||
const names = layoutFlame(t, { minWidth: 0.001 }).rects.map((r) => r.node.n);
|
||
assert.deepEqual(names, ['a', 'big']);
|
||
});
|
||
|
||
test('hitTest finds the node under the pointer, flame (bottom up) and icicle (top down)', () => {
|
||
const l = layoutFlame(tree);
|
||
const w = 1000; const rowH = 20; const h = 3 * rowH;
|
||
assert.equal(hitTest(l, 100, h - 10, w, h, rowH, false).node.n, '(root)');
|
||
assert.equal(hitTest(l, 100, h - 30, w, h, rowH, false).node.n, 'App\\Scene\\PlayScene->update');
|
||
assert.equal(hitTest(l, 800, h - 30, w, h, rowH, false).node.n, 'JumPHP\\Renderer\\Renderer->endFrame');
|
||
assert.equal(hitTest(l, 100, 50, w, h, rowH, true).node.n, 'App\\Entity\\Hero->update');
|
||
assert.equal(hitTest(l, 900, 50, w, h, rowH, true), null);
|
||
});
|
||
|
||
test('search finds by name or file; no query = no filter', () => {
|
||
assert.equal(searchNodes(tree, ''), null);
|
||
const found = searchNodes(tree, 'hero');
|
||
assert.equal(found.size, 1);
|
||
assert.equal(searchNodes(tree, 'PLAYSCENE').size, 1);
|
||
assert.equal(searchNodes(tree, 'src/entity').size, 1);
|
||
assert.equal(searchNodes(tree, 'nothing').size, 0);
|
||
});
|
||
|
||
const slices = { count: 4, names: ['a', 'b', '(other)'], t: [[1, 0, 0], [2, 3, 0], [0, 4, 1], [0, 0, 9]], m: [[10, 0, 0], [0, -5, 0], [100, 50, 0], [0, 0, 7]] };
|
||
|
||
test('rangeTotals sums the slices of the selected range only', () => {
|
||
const r = rangeTotals(slices, 1, 2);
|
||
assert.deepEqual(r.time.map((x) => [x.n, x.ms]), [['b', 7], ['a', 2]]); // the (other) bucket is in the totals, not in the lists
|
||
assert.deepEqual(r.memory.map((x) => [x.n, x.bytes]), [['a', 100], ['b', 45]]);
|
||
assert.equal(r.ms, 10); assert.equal(r.bytes, 145);
|
||
const all = rangeTotals(slices, 0, 3);
|
||
assert.equal(all.ms, 20);
|
||
assert.equal(rangeTotals(slices, 3, 1).ms, rangeTotals(slices, 1, 3).ms); // a backwards brush is the same range
|
||
assert.equal(rangeTotals(slices, -5, 99).ms, 20);
|
||
assert.equal(rangeTotals(null, 0, 1), null);
|
||
assert.equal(rangeTotals(slices, 0, 3, 1).time.length, 1);
|
||
});
|
||
|
||
test('brushSlices maps a drag over the chart to slices', () => {
|
||
assert.deepEqual(brushSlices(0, 1), [0, 99]);
|
||
assert.deepEqual(brushSlices(0.5, 0.25), [25, 49]);
|
||
assert.deepEqual(brushSlices(0.101, 0.101), [10, 10]);
|
||
assert.deepEqual(brushSlices(-1, 2), [0, 99]);
|
||
});
|
||
|
||
test('formats', () => {
|
||
assert.equal(formatMs(0.05), '50 µs'); assert.equal(formatMs(2.5), '2.50 ms'); assert.equal(formatMs(42), '42.0 ms');
|
||
assert.equal(formatMs(1500), '1.50 s'); assert.equal(formatMs(120000), '2.0 min'); assert.equal(formatMs(null), '–');
|
||
assert.equal(formatBytes(512), '512 B'); assert.equal(formatBytes(2048), '2.00 KB'); assert.equal(formatBytes(7 * 1024 * 1024), '7.00 MB');
|
||
assert.equal(formatBytes(-2048), '-2.00 KB'); assert.equal(formatBytes(null), '–');
|
||
assert.equal(formatCount(999), '999'); assert.equal(formatCount(25000), '25k'); assert.equal(formatCount(2500000), '2.5M');
|
||
assert.equal(shortName('JumPHP\\Scene\\SceneManager->update'), 'SceneManager->update');
|
||
assert.equal(shortName('fib'), 'fib');
|
||
});
|
||
|
||
test('colours differ by namespace and are stable per name', () => {
|
||
const a = colorOf({ n: 'App\\X->a', f: '' }); const b = colorOf({ n: 'JumPHP\\X->a', f: '' });
|
||
assert.notEqual(a, b);
|
||
assert.equal(a, colorOf({ n: 'App\\X->a', f: '' }));
|
||
assert.match(a, /^hsl\(/);
|
||
});
|