Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions packages/studio-server/src/helpers/projectSignature.memory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { setFlagsFromString } from "node:v8";
import { runInNewContext } from "node:vm";
import { resolve } from "node:path";
import { describe, expect, it, vi } from "vitest";

// A project whose frames/ folder holds FRAMES files, served from memory so the walk itself is all that is measured.
const FRAMES = 50_000;
const PROJECT = resolve("/hf-memory-project");
const FRAMES_DIR = resolve(PROJECT, "frames");
const NAMES = Array.from({ length: FRAMES }, (_, i) => `frame-${String(i).padStart(6, "0")}.png`);

setFlagsFromString("--expose-gc");
const gc = runInNewContext("gc") as () => void;
const probe = { calls: 0, base: 0, peak: 0, inFlight: 0, mostInFlight: 0 };
function sample(): void {
probe.calls += 1;
if (probe.calls % 5_000 !== 1) return;
gc();
const used = process.memoryUsage().heapUsed;
if (probe.calls === 1) probe.base = used;
probe.peak = Math.max(probe.peak, used);
}

const kind = (path: string) =>
path === PROJECT || path === FRAMES_DIR ? "dir" : path.startsWith(FRAMES_DIR) ? "frame" : "index";
const fakeStat = (path: string) => {
const dir = kind(path) === "dir";
return {
isDirectory: () => dir,
isFile: () => !dir,
isSymbolicLink: () => false,
size: dir ? 0 : 10,
mtimeMs: 1,
ctimeMs: 1,
};
};
const inProject = (path: unknown) => String(path).startsWith(PROJECT);
const missing = () => Object.assign(new Error("ENOENT"), { code: "ENOENT" });
const listing = (path: string) =>
path === PROJECT ? ["frames", "index.html"] : path === FRAMES_DIR ? NAMES.slice() : null;

vi.mock("node:fs", async (actual) => {
const fs = await actual<typeof import("node:fs")>();
const faked = {
...fs,
readdirSync: (path: string, ...rest: never[]) =>
inProject(path) ? (listing(path) ?? []) : fs.readdirSync(path, ...rest),
lstatSync: (path: string, ...rest: never[]) => {
if (!inProject(path)) return fs.lstatSync(path, ...rest);
if (path.includes(".hyperframes")) throw missing();
sample();
return fakeStat(path);
},
readFileSync: (path: string, ...rest: never[]) =>
inProject(path) ? Buffer.from("<html></html>") : fs.readFileSync(path, ...rest),
};
return { ...faked, default: faked };
});

vi.mock("node:fs/promises", async (actual) => {
const fs = await actual<typeof import("node:fs/promises")>();
const faked = {
...fs,
readdir: async (path: string, ...rest: never[]) =>
inProject(path) ? (listing(path) ?? []) : fs.readdir(path, ...rest),
lstat: (path: string, ...rest: never[]) => {
if (!inProject(path)) return fs.lstat(path, ...rest);
if (path.includes(".hyperframes")) return Promise.reject(missing());
sample();
probe.inFlight += 1;
probe.mostInFlight = Math.max(probe.mostInFlight, probe.inFlight);
return new Promise((done) =>
setImmediate(() => {
probe.inFlight -= 1;
done(fakeStat(path));
}),
);
},
readFile: async (path: string, ...rest: never[]) =>
inProject(path) ? Buffer.from("<html></html>") : fs.readFile(path, ...rest),
};
return { ...faked, default: faked };
});

const { createProjectSignature, createProjectSignatureAsync } =
await import("./projectSignature.js");

const grewPerFile = () => (probe.peak - probe.base) / FRAMES;
const reset = () =>
Object.assign(probe, { calls: 0, base: 0, peak: 0, inFlight: 0, mostInFlight: 0 });

// Before, both walks kept one record per file until the end, and the awaited one stat'ed a whole folder at once: a
// project an agent filled with frames took hundreds of MB to sign. Memory now follows folder depth, not file count.
describe("signing a project with a large folder", () => {
it("the synchronous walk keeps no record per file", () => {
reset();
expect(createProjectSignature(PROJECT)).toMatch(/^[0-9a-f]{24}$/);
expect(grewPerFile()).toBeLessThan(40);
});

it("the awaited walk keeps no record per file and stats a bounded number of entries at once", async () => {
reset();
expect(await createProjectSignatureAsync(PROJECT)).toBe(createProjectSignature(PROJECT));
expect(probe.mostInFlight).toBeLessThanOrEqual(64);
reset();
await createProjectSignatureAsync(PROJECT);
expect(grewPerFile()).toBeLessThan(40);
});
});
Loading
Loading