1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-07-26 19:14:36 +02:00
Files
misskey/packages-private/diagnostics-shared/test/heap-snapshot-render.test.ts
2026-07-23 21:46:19 +09:00

104 lines
3.4 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { describe, expect, test } from 'vitest';
import {
createEmptyHeapSnapshotData,
renderHeapSnapshotTable,
summarizeHeapSnapshotDataSamples,
type HeapSnapshotData,
type HeapSnapshotReport,
} from '../src/heap-snapshot';
function snapshot(total: number): HeapSnapshotData {
const data = createEmptyHeapSnapshotData();
data.categories.total = total;
return data;
}
function report(totals: number[]): HeapSnapshotReport {
const samples = totals.map((total, index) => ({
round: index + 1,
data: snapshot(total),
}));
const summary = summarizeHeapSnapshotDataSamples(samples, sample => sample.data);
if (summary == null) throw new Error('expected heap snapshot samples to produce a summary');
return {
summary,
samples,
};
}
function totalRow(markdown: string) {
const row = markdown.split('\n').find(line => line.includes('**Total**'));
if (row === undefined) throw new Error('expected heap snapshot table to contain a Total row');
return row;
}
describe('renderHeapSnapshotTable', () => {
test('leaves a threshold-sized delta uncoloured when it is within observed noise', () => {
const row = totalRow(renderHeapSnapshotTable(
report([1_000_000, 1_100_000, 1_200_000]),
report([1_100_000, 1_200_000, 1_300_000]),
));
expect(row).toContain('$\\text{+100 KB}$');
expect(row).not.toContain('within noise');
expect(row).not.toContain('\\color{orange}');
});
test('colours both delta lines for a clear increase at or above the absolute threshold', () => {
const row = totalRow(renderHeapSnapshotTable(
report([1_000_000, 1_000_000, 1_000_000]),
report([1_200_000, 1_200_000, 1_200_000]),
));
expect(row).toContain('$\\color{orange}{\\text{+200 KB}}$');
expect(row).toContain('$\\color{orange}{\\text{+20\\\\%}}$');
expect(row).not.toContain('increase');
});
test('leaves both delta lines uncoloured below the absolute threshold', () => {
const row = totalRow(renderHeapSnapshotTable(
report([1_000_000, 1_000_000, 1_000_000]),
report([1_050_000, 1_050_000, 1_050_000]),
));
expect(row).toContain('$\\text{+50 KB}$<br>$\\text{+5\\\\%}$');
expect(row.split('|')[4]).not.toContain('\\color{');
});
test('throws when only one snapshot per side reaches the renderer', () => {
expect(() => renderHeapSnapshotTable(
report([1_000_000]),
report([1_200_000]),
)).toThrow('At least two samples per side are required');
});
test('uses two-line formatting only for Total and puts a five-column separator after it', () => {
const table = renderHeapSnapshotTable(
report([1_000_000, 1_000_000, 1_000_000]),
report([1_200_000, 1_200_000, 1_200_000]),
);
const lines = table.split('\n');
const totalIndex = lines.findIndex(line => line.includes('**Total**'));
const categoryRow = lines.find(line => line.includes('**Code**'));
expect(lines.slice(0, 2)).toStrictEqual([
'| Metric | @&nbsp;Base | @&nbsp;Head | Δ | MAD |',
'| --- | ---: | ---: | ---: | ---: |',
]);
expect(table).not.toContain('Result');
expect(totalIndex).toBeGreaterThan(1);
expect(lines[totalIndex].match(/<br>/g)).toHaveLength(3);
expect(lines[totalIndex + 1]).toBe('| | | | | |');
expect(categoryRow).toBeDefined();
expect(categoryRow).not.toContain('<br>');
expect(categoryRow).not.toContain('<details>');
expect(categoryRow).not.toContain('→');
});
});