Файли
openidea/src/features/debugger/__tests__/memoryRows.test.ts
T
ntbang0901 8d09479a65 feat(debugger): heap snapshots of a debugged JVM (#2649)
Capture a heap dump of the debugged JVM over the attach listener (GC.heap_dump, after checking free disk space and the JDK's rules for suspended VMs) or open an .hprof file, and browse it in an editor tab: classes with shallow and retained sizes, biggest objects, dominators, the path from an object to a GC root, and a comparison of two snapshots. A snapshot captured at a stop adds a Retained Size column to that stop's Memory tab.

New crate ide-heap: HotSpot hprof parser (bounds-checked; unfinished or crafted dumps are refused), object graph, dominator tree (semi-NCA), retained sizes and GC-root paths. Snapshots live in private directories removed with their window; an opened dump is analyzed from a private copy so changes to the user's file can't affect the mapped data.

Co-authored-by: ntbang0901 <[email protected]>
2026-11-11 17:43:07 +08:00

119 рядки
4.2 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
import { describe, expect, it } from "vitest";
import type { JvmClassRow } from "@/platform/ipc";
import { columnsOf, formatBytes, formatDelta, queryFor, visibleRows, type RowQuery } from "../memory/rows";
const row = (name: string, instances: number, bytes: number, deltaInstances: number | null): JvmClassRow => ({
name,
module: null,
instances,
bytes,
deltaInstances,
deltaBytes: deltaInstances == null ? null : deltaInstances * 16,
ambiguous: false,
});
const rows = [
row("java.lang.String", 100, 2400, 0),
row("java.util.HashMap$Node", 50, 1600, 5),
row("com.example.Order", 10, 4000, -3),
row("com.example.Customer", 20, 800, null),
];
const q: RowQuery = { filter: "", sort: "bytes", desc: true, changedOnly: false };
const names = (r: JvmClassRow[]) => r.map((x) => x.name);
describe("visibleRows", () => {
it("filters by substring, case-insensitive", () => {
expect(names(visibleRows(rows, { ...q, filter: "hashmap" }))).toEqual(["java.util.HashMap$Node"]);
});
it("keeps only non-zero instance deltas when changedOnly", () => {
expect(names(visibleRows(rows, { ...q, changedOnly: true }))).toEqual([
"com.example.Order",
"java.util.HashMap$Node",
]);
});
it("sorts by delta with nulls last in both directions", () => {
expect(names(visibleRows(rows, { ...q, sort: "deltaInstances", desc: true }))).toEqual([
"java.util.HashMap$Node",
"java.lang.String",
"com.example.Order",
"com.example.Customer",
]);
expect(names(visibleRows(rows, { ...q, sort: "deltaInstances", desc: false }))).toEqual([
"com.example.Order",
"java.lang.String",
"java.util.HashMap$Node",
"com.example.Customer",
]);
});
it("sorts by name ascending and by instances descending", () => {
expect(names(visibleRows(rows, { ...q, sort: "name", desc: false }))[0]).toBe("com.example.Customer");
expect(names(visibleRows(rows, { ...q, sort: "instances" }))[0]).toBe("java.lang.String");
});
it("sorts by retained with missing values last", () => {
const retained = new Map([
["java.lang.String", 3000],
["com.example.Order", 9000],
["java.util.HashMap$Node", 100],
]);
expect(names(visibleRows(rows, { ...q, sort: "retained", desc: true }, retained))).toEqual([
"com.example.Order",
"java.lang.String",
"java.util.HashMap$Node",
"com.example.Customer",
]);
expect(names(visibleRows(rows, { ...q, sort: "retained", desc: false }, retained))).toEqual([
"java.util.HashMap$Node",
"java.lang.String",
"com.example.Order",
"com.example.Customer",
]);
});
it("does not mutate its input", () => {
const copy = [...rows];
visibleRows(rows, { ...q, sort: "name", desc: false });
expect(rows).toEqual(copy);
});
});
describe("columns", () => {
it("has a Retained Size column only with retained sizes, and keeps debugger tables without sizes", () => {
expect(columnsOf("histogram", true)).toEqual(["name", "instances", "deltaInstances", "bytes", "retained"]);
expect(columnsOf("debugger", true)).toEqual(["name", "instances", "deltaInstances", "retained"]);
expect(columnsOf("histogram", false)).not.toContain("retained");
});
it("falls back to the count when the retained sizes are gone", () => {
expect(queryFor({ ...q, sort: "retained" }, "histogram", false).sort).toBe("instances");
expect(queryFor({ ...q, sort: "retained" }, "debugger", true).sort).toBe("retained");
});
});
describe("formatting", () => {
it("formats bytes", () => {
expect(formatBytes(0)).toBe("0 B");
expect(formatBytes(512)).toBe("512 B");
expect(formatBytes(1536)).toBe("1.5 KB");
expect(formatBytes(16_000_000)).toBe("15.3 MB");
});
it("rolls to the next unit instead of rounding up to 1024", () => {
expect(formatBytes(1023)).toBe("1023 B");
expect(formatBytes(1024)).toBe("1.0 KB");
expect(formatBytes(1_048_575)).toBe("1.0 MB");
});
it("formats deltas", () => {
expect(formatDelta(null)).toBe("");
expect(formatDelta(0)).toBe("0");
expect(formatDelta(3)).toBe("+3");
expect(formatDelta(-2)).toBe("−2");
expect(formatDelta(4312)).toBe(`+${(4312).toLocaleString()}`);
expect(formatDelta(-1500)).toBe(`−${(1500).toLocaleString()}`);
});
});