Files
hyperframes/packages/engine/src/services/availableMemory.test.ts
T
Miguel Ángel c50918277a perf(engine): fast capture stays on for frames a shadow, blur or clip effect does not touch (#6318)
* perf(engine): fast capture stays on for frames a shadow, blur or clip effect does not touch

A single drop-shadow, blur, backdrop-filter or blend element, or one clip-path or mask
tween, sent the whole render to screenshot capture, 2-3x slower than drawElement. The two
init gates now name the frames the effect can show on. Only those frames capture by
screenshot, in a second browser opened on the first of them; every other frame keeps
drawElement.

* fix(engine): scan effects in a throwaway session; hand risk frames over only where captured

The effect scan seeked the render's own page to the end, leaving its timeline state ahead of
the render. It now runs in a throwaway session with the render's own seek. A hidden effect
element with a visible descendant counts as shown. Per-frame capture is opt-in per caller
(deFrameFallback), so disk, segmented, chunk and parallel-worker sessions keep the
whole-render fallback, and a route that cannot hand frames over drops drawElement up front.

* fix(engine): a timeline callback keeps the whole-render fallback; trim comment share

A tl.call or onComplete can add an effect's class to any element mid-render, so the elements
found at the start are not the only ones it can reach. Such a timeline is unbounded and keeps
the whole-render screenshot fallback.

* style(engine): shorten the live-frame scan comment

* perf(engine): after a timeline callback, not before, every frame may show the effect

A callback can add an effect's class to any element, so only the frames from the first
callback on lose their element bounds. Frames before it keep the scanned bounds, so a late
callback no longer sends the whole render to screenshot capture.

* perf(engine): open the second screenshot browser only when memory is available for it

Per-frame capture needs a second browser (479 MB measured at 1080p). When the render would
leave less than 1.5 GB available after it, the render keeps the whole-render screenshot
fallback instead. getAvailableMemoryMb moves from the CLI to the engine's system-memory
module, which already owns the host's memory readings; os.freemem() reads 367 MB on this
49 GB Mac, so the guard uses free, inactive, speculative and purgeable pages.

* fix(engine): budget the whole 1 GB per-frame capture adds, not just the second browser

The render's process tree goes from 1002 to 2001 MB at 1080p: the screenshot browser (479 MB)
plus a drawElement browser that is itself about 500 MB larger than a screenshot one. The guard
budgeted 500 MB. It now budgets the 1 GB measured, scaled by output pixels.
2026-10-25 19:15:18 +00:00

114 lines
4.1 KiB
TypeScript

// fallow-ignore-file code-duplication
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
beforeEach(() => {
vi.resetModules();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("getAvailableMemoryMb", () => {
it("parses vm_stat on macOS to compute available memory", async () => {
vi.doMock("node:os", async () => ({
...(await vi.importActual<typeof import("node:os")>("node:os")),
platform: () => "darwin",
freemem: () => 100 * 1024 * 1024,
}));
vi.doMock("node:child_process", async () => ({
...(await vi.importActual<typeof import("node:child_process")>("node:child_process")),
execSync: (_cmd: string) =>
[
"Mach Virtual Memory Statistics: (page size of 16384 bytes)",
"Pages free: 5000.",
"Pages active: 200000.",
"Pages inactive: 100000.",
"Pages speculative: 2000.",
"Pages throttled: 0.",
"Pages wired down: 150000.",
"Pages purgeable: 3000.",
].join("\n"),
}));
const { getAvailableMemoryMb } = await import("./systemMemory.js");
const result = getAvailableMemoryMb();
// (5000 + 100000 + 3000 + 2000) * 16384 = 110000 * 16384 = 1,802,240,000 bytes
// 1,802,240,000 / (1024 * 1024) = ~1718 MB
expect(result).toBe(Math.trunc((110000 * 16384) / (1024 * 1024)));
});
it("falls back to freemem on macOS when vm_stat fails", async () => {
vi.doMock("node:os", async () => ({
...(await vi.importActual<typeof import("node:os")>("node:os")),
platform: () => "darwin",
freemem: () => 4 * 1024 * 1024 * 1024,
}));
vi.doMock("node:child_process", async () => ({
...(await vi.importActual<typeof import("node:child_process")>("node:child_process")),
execSync: () => {
throw new Error("vm_stat not found");
},
}));
const { getAvailableMemoryMb } = await import("./systemMemory.js");
expect(getAvailableMemoryMb()).toBe(4096);
});
it("reads MemAvailable from /proc/meminfo on Linux", async () => {
vi.doMock("node:os", async () => ({
...(await vi.importActual<typeof import("node:os")>("node:os")),
platform: () => "linux",
freemem: () => 100 * 1024 * 1024,
}));
vi.doMock("node:fs", async () => ({
...(await vi.importActual<typeof import("node:fs")>("node:fs")),
readFileSync: (path: string, _enc: string) => {
if (path === "/proc/meminfo") {
return [
"MemTotal: 16384000 kB",
"MemFree: 512000 kB",
"MemAvailable: 8192000 kB",
"Buffers: 256000 kB",
].join("\n");
}
throw new Error(`unexpected path: ${path}`);
},
}));
const { getAvailableMemoryMb } = await import("./systemMemory.js");
// 8192000 kB / 1024 = 8000 MB
expect(getAvailableMemoryMb()).toBe(8000);
});
it("falls back to freemem on Linux when /proc/meminfo is unreadable", async () => {
vi.doMock("node:os", async () => ({
...(await vi.importActual<typeof import("node:os")>("node:os")),
platform: () => "linux",
freemem: () => 2 * 1024 * 1024 * 1024,
}));
vi.doMock("node:fs", async () => ({
...(await vi.importActual<typeof import("node:fs")>("node:fs")),
readFileSync: (path: string) => {
if (path === "/proc/meminfo") throw new Error("ENOENT");
throw new Error(`unexpected path: ${path}`);
},
}));
const { getAvailableMemoryMb } = await import("./systemMemory.js");
expect(getAvailableMemoryMb()).toBe(2048);
});
it("falls back to freemem on unsupported platforms", async () => {
vi.doMock("node:os", async () => ({
...(await vi.importActual<typeof import("node:os")>("node:os")),
platform: () => "win32",
freemem: () => 6 * 1024 * 1024 * 1024,
}));
const { getAvailableMemoryMb } = await import("./systemMemory.js");
expect(getAvailableMemoryMb()).toBe(6144);
});
});