mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
perf(engine): check still frames on several pages at once so text films start sooner (#7997)
* perf(engine): check still frames on several pages at once so text films start sooner * fix(engine): parallel still-frame check keeps its full time budget * test(engine): give the Bun download check the minute its spawn already allows
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
planStaticVerification,
|
||||
verifyStaticFramesSafe,
|
||||
type CaptureSession,
|
||||
} from "./frameCapture.js";
|
||||
|
||||
const fps = 30;
|
||||
type FakePage = { frame: number; evaluate: (fn: unknown, time: number) => Promise<void> };
|
||||
|
||||
function fakePage(): FakePage {
|
||||
const page: FakePage = {
|
||||
frame: 0,
|
||||
evaluate: async (_fn, time) => {
|
||||
page.frame = Math.round(time * fps);
|
||||
},
|
||||
};
|
||||
return page;
|
||||
}
|
||||
|
||||
function framesForRuns(runs: Array<[number, number]>): Set<number> {
|
||||
const frames = new Set<number>();
|
||||
for (const [a, b] of runs) for (let f = a; f <= b; f++) frames.add(f);
|
||||
return frames;
|
||||
}
|
||||
|
||||
/** Screenshots of a page show `contentAt(frame)`; `inFlight.max` records how many ran at once. */
|
||||
function capturing(contentAt: (frame: number) => string) {
|
||||
const inFlight = { now: 0, max: 0 };
|
||||
const capture = async (target: unknown) => {
|
||||
inFlight.now++;
|
||||
inFlight.max = Math.max(inFlight.max, inFlight.now);
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
inFlight.now--;
|
||||
return Buffer.from(contentAt((target as FakePage).frame));
|
||||
};
|
||||
return { capture, inFlight };
|
||||
}
|
||||
|
||||
function verify(
|
||||
pages: FakePage[],
|
||||
frames: Set<number>,
|
||||
capture: (target: unknown) => Promise<Buffer>,
|
||||
now?: () => number,
|
||||
) {
|
||||
return verifyStaticFramesSafe(
|
||||
{ options: {} } as unknown as CaptureSession,
|
||||
pages as unknown as Parameters<typeof verifyStaticFramesSafe>[1],
|
||||
frames,
|
||||
fps,
|
||||
1,
|
||||
{ capture: capture as never, ...(now ? { now } : {}) },
|
||||
);
|
||||
}
|
||||
|
||||
const runs: Array<[number, number]> = [
|
||||
[1, 40],
|
||||
[50, 90],
|
||||
[100, 140],
|
||||
[150, 190],
|
||||
];
|
||||
|
||||
describe("verifyStaticFramesSafe on several pages", () => {
|
||||
it("verifies runs on every page at once", async () => {
|
||||
const { capture, inFlight } = capturing(() => "still");
|
||||
|
||||
const result = await verify([fakePage(), fakePage()], framesForRuns(runs), capture);
|
||||
|
||||
expect(result.outcome).toBe("verified");
|
||||
expect(result.verifiedFrames).toEqual(framesForRuns(runs));
|
||||
expect(inFlight.max).toBe(2);
|
||||
});
|
||||
|
||||
it("a mismatch on one page clears the runs another page verified", async () => {
|
||||
const { capture } = capturing((frame) => (frame >= 150 ? "moved" : "still"));
|
||||
|
||||
const result = await verify([fakePage(), fakePage()], framesForRuns(runs), capture);
|
||||
|
||||
expect(result.outcome).toBe("mismatch");
|
||||
expect(result.verifiedFrames.size).toBe(0);
|
||||
});
|
||||
|
||||
it("gives the verdict one page gives", async () => {
|
||||
const one = await verify([fakePage()], framesForRuns(runs), capturing(() => "still").capture);
|
||||
const three = await verify(
|
||||
[fakePage(), fakePage(), fakePage()],
|
||||
framesForRuns(runs),
|
||||
capturing(() => "still").capture,
|
||||
);
|
||||
|
||||
expect(three.outcome).toBe(one.outcome);
|
||||
expect(three.verifiedFrames).toEqual(one.verifiedFrames);
|
||||
expect(three.stats.completedRuns).toBe(one.stats.completedRuns);
|
||||
});
|
||||
|
||||
it("scales its time budget from the first shot when pages capture together", async () => {
|
||||
const frames = framesForRuns(runs);
|
||||
const shotMs = Math.ceil(20_000 / planStaticVerification(frames, 1).plannedScreenshots);
|
||||
let clock = 0;
|
||||
const { capture } = capturing(() => "still");
|
||||
const slowCapture = async (target: unknown) => {
|
||||
clock += shotMs;
|
||||
return capture(target);
|
||||
};
|
||||
|
||||
const result = await verify([fakePage(), fakePage()], frames, slowCapture, () => clock);
|
||||
|
||||
expect(result.outcome).toBe("verified");
|
||||
expect(result.verifiedFrames).toEqual(frames);
|
||||
});
|
||||
});
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
type ConsoleMessage,
|
||||
} from "puppeteer-core";
|
||||
import { existsSync, mkdirSync, writeFileSync } from "fs";
|
||||
import { availableParallelism } from "os";
|
||||
import { join } from "path";
|
||||
import {
|
||||
COMPOSITION_SOURCE_URL,
|
||||
@@ -4071,6 +4072,7 @@ const STATIC_VERIFY_REFERENCE_STRIDE = 24;
|
||||
const STATIC_VERIFY_MAX_MS = 15_000;
|
||||
const STATIC_VERIFY_HEADROOM = 1.5;
|
||||
const STATIC_VERIFY_CEILING_MS = 60_000;
|
||||
const STATIC_VERIFY_MAX_PAGES = 4;
|
||||
// Two captures (anchor + comparison) are the minimum proof for a run. Four hundred
|
||||
// therefore permits 200 fully verified runs while bounding pathological schedules.
|
||||
const STATIC_VERIFY_MIN_SCREENSHOT_CAP = 400;
|
||||
@@ -4305,7 +4307,7 @@ export async function createStaticVerificationPage(session: CaptureSession): Pro
|
||||
*/
|
||||
export async function verifyStaticFramesSafe(
|
||||
session: CaptureSession,
|
||||
page: Page,
|
||||
page: Page | readonly Page[],
|
||||
staticFrames: Set<number>,
|
||||
fps: number,
|
||||
sampleCount: number,
|
||||
@@ -4341,10 +4343,11 @@ export async function verifyStaticFramesSafe(
|
||||
stats,
|
||||
});
|
||||
if (plan.runs.length === 0) return finish("unprofitable");
|
||||
const seekToFrame = async (frameIdx: number): Promise<void> => {
|
||||
const pages = Array.isArray(page) ? page : [page];
|
||||
const seekToFrame = async (target: Page, frameIdx: number): Promise<void> => {
|
||||
stats.seeks++;
|
||||
const t = quantizeTimeToFrame(frameIdx / fps, fps);
|
||||
await page.evaluate((tt: number) => {
|
||||
await target.evaluate((tt: number) => {
|
||||
const hf = (
|
||||
window as unknown as {
|
||||
__hf?: { seek?: (t: number, options?: { suppressEvents?: boolean }) => void };
|
||||
@@ -4360,6 +4363,7 @@ export async function verifyStaticFramesSafe(
|
||||
);
|
||||
let firstShotMs = 0;
|
||||
const seekCapture = async (
|
||||
target: Page,
|
||||
frameIdx: number,
|
||||
): Promise<Buffer | "time_budget" | "count_budget"> => {
|
||||
const spent = now() - startedAt;
|
||||
@@ -4373,46 +4377,75 @@ export async function verifyStaticFramesSafe(
|
||||
if (spent >= budget) return "time_budget";
|
||||
if (stats.screenshots >= hardCap) return "count_budget";
|
||||
const shotStart = now();
|
||||
await seekToFrame(frameIdx);
|
||||
stats.screenshots++;
|
||||
const buf = await capture(page, session.options);
|
||||
await seekToFrame(target, frameIdx);
|
||||
const first = stats.screenshots++ === 0;
|
||||
const buf = await capture(target, session.options);
|
||||
session.options.onStaticDedupActivity?.();
|
||||
if (stats.screenshots === 1) firstShotMs = now() - shotStart;
|
||||
if (first) firstShotMs = now() - shotStart;
|
||||
return buf;
|
||||
};
|
||||
// Verify profitable runs in deterministic savings order. A run is added to the armed
|
||||
// set only after its anchor and every planner-selected comparison match. Budget exits
|
||||
// retain completed runs; mismatch or infrastructure failure clears all verified frames.
|
||||
try {
|
||||
for (const run of plan.runs) {
|
||||
const anchorBuf = await seekCapture(run.anchor);
|
||||
if (typeof anchorBuf === "string") return finish(anchorBuf, run.a);
|
||||
// Each page takes the next profitable run in savings order. A run is added to the armed set
|
||||
// only after its anchor and every planner-selected comparison match. Budget exits retain
|
||||
// completed runs; a mismatch or infrastructure failure on any page clears all verified frames.
|
||||
let next = 0;
|
||||
let budgetExit: StaticVerificationResult | undefined;
|
||||
let mismatchAt: number | undefined;
|
||||
let failed = false;
|
||||
const stopped = () => budgetExit !== undefined || mismatchAt !== undefined || failed;
|
||||
const verifyOn = async (target: Page): Promise<void> => {
|
||||
while (!stopped() && next < plan.runs.length) {
|
||||
const run = plan.runs[next++]!;
|
||||
const anchorBuf = await seekCapture(target, run.anchor);
|
||||
if (typeof anchorBuf === "string") {
|
||||
budgetExit ??= finish(anchorBuf, run.a);
|
||||
return;
|
||||
}
|
||||
stats.completedAnchors++;
|
||||
for (const f of run.comparisons) {
|
||||
const cur = await seekCapture(f);
|
||||
if (typeof cur === "string") return finish(cur, f);
|
||||
const cur = await seekCapture(target, f);
|
||||
if (typeof cur === "string") {
|
||||
budgetExit ??= finish(cur, f);
|
||||
return;
|
||||
}
|
||||
stats.completedComparisons++;
|
||||
stats.byteComparisons++;
|
||||
if (!anchorBuf.equals(cur)) {
|
||||
verifiedFrames.clear();
|
||||
stats.verifiedFrames = 0;
|
||||
stats.unverifiedFrames = plan.predictedFrames;
|
||||
return finish("mismatch", f);
|
||||
mismatchAt ??= f;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (mismatchAt !== undefined || failed) return;
|
||||
stats.completedRuns++;
|
||||
for (let frame = run.a; frame <= run.b; frame++) verifiedFrames.add(frame);
|
||||
stats.verifiedFrames = verifiedFrames.size;
|
||||
stats.unverifiedFrames = plan.predictedFrames - verifiedFrames.size;
|
||||
}
|
||||
return finish("verified");
|
||||
} catch {
|
||||
};
|
||||
const clearVerified = () => {
|
||||
verifiedFrames.clear();
|
||||
stats.verifiedFrames = 0;
|
||||
stats.unverifiedFrames = plan.predictedFrames;
|
||||
return finish("infrastructure");
|
||||
};
|
||||
try {
|
||||
const settled = await Promise.allSettled(
|
||||
pages.map((target) =>
|
||||
verifyOn(target).catch((error: unknown) => {
|
||||
failed = true;
|
||||
throw error;
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (settled.some((lane) => lane.status === "rejected")) {
|
||||
clearVerified();
|
||||
return finish("infrastructure");
|
||||
}
|
||||
if (mismatchAt !== undefined) {
|
||||
clearVerified();
|
||||
return finish("mismatch", mismatchAt);
|
||||
}
|
||||
return budgetExit ?? finish("verified");
|
||||
} finally {
|
||||
await seekToFrame(0).catch(() => {});
|
||||
await Promise.all(pages.map((target) => seekToFrame(target, 0).catch(() => {})));
|
||||
stats.elapsedMs = Math.max(0, now() - startedAt);
|
||||
}
|
||||
}
|
||||
@@ -4733,6 +4766,13 @@ async function verifyOrShareStaticFrames(
|
||||
}
|
||||
}
|
||||
|
||||
/** Pages that verify static runs side by side; seeks and screenshots mostly wait on Chrome, not the CPU. */
|
||||
function staticVerificationPageCount(): number {
|
||||
const forced = Number(process.env.HF_STATIC_DEDUP_VERIFY_PAGES);
|
||||
if (Number.isInteger(forced) && forced >= 1) return forced;
|
||||
return Math.min(STATIC_VERIFY_MAX_PAGES, Math.max(1, availableParallelism()));
|
||||
}
|
||||
|
||||
async function verifyAndArmStaticDedup(
|
||||
session: CaptureSession,
|
||||
stats: Awaited<ReturnType<typeof computeStaticFrameSet>>,
|
||||
@@ -4741,13 +4781,19 @@ async function verifyAndArmStaticDedup(
|
||||
logInitPhase: (phase: string) => void,
|
||||
onVerdict: (verdict: StaticVerificationResult) => void,
|
||||
): Promise<void> {
|
||||
let verificationPage: Page | undefined;
|
||||
let verificationPages: Page[] = [];
|
||||
let verdict: StaticVerificationResult;
|
||||
try {
|
||||
verificationPage = await createStaticVerificationPage(session);
|
||||
const opened = await Promise.allSettled(
|
||||
Array.from({ length: staticVerificationPageCount() }, () =>
|
||||
createStaticVerificationPage(session),
|
||||
),
|
||||
);
|
||||
verificationPages = opened.flatMap((p) => (p.status === "fulfilled" ? [p.value] : []));
|
||||
if (verificationPages.length === 0) throw new Error("no verification page opened");
|
||||
verdict = await verifyStaticFramesSafe(
|
||||
session,
|
||||
verificationPage,
|
||||
verificationPages,
|
||||
stats.staticFrameSet,
|
||||
fps,
|
||||
samples,
|
||||
@@ -4757,7 +4803,7 @@ async function verifyAndArmStaticDedup(
|
||||
logInitPhase("static-frame dedup: disabled (verification infrastructure failure)");
|
||||
return;
|
||||
} finally {
|
||||
await verificationPage?.close().catch(() => {});
|
||||
await Promise.all(verificationPages.map((p) => p.close().catch(() => {})));
|
||||
}
|
||||
session.staticDedupVerification = verdict;
|
||||
onVerdict(verdict);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import { fetchPublicHttpsText } from "./urlDownloader.js";
|
||||
|
||||
const FIXTURE = fileURLToPath(new URL("./__fixtures__/downloadPrivateAnswer.mjs", import.meta.url));
|
||||
const BUN_START_MS = 60_000;
|
||||
const PROXY_VARS = ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "NODE_USE_ENV_PROXY"];
|
||||
|
||||
function envWithoutProxy(): NodeJS.ProcessEnv {
|
||||
@@ -33,15 +34,19 @@ describe("a name that resolves to a private address is refused on every runtime"
|
||||
}
|
||||
});
|
||||
|
||||
it("under Bun, which the Cloud Run image runs", () => {
|
||||
const run = spawnSync("bun", [FIXTURE], {
|
||||
encoding: "utf8",
|
||||
env: envWithoutProxy(),
|
||||
timeout: 60_000,
|
||||
});
|
||||
expect(run.status, run.stderr).toBe(0);
|
||||
const result = JSON.parse(run.stdout.trim().split("\n").at(-1) ?? "{}");
|
||||
expect(result.outcome).toBe("http_rejected");
|
||||
expect(result.lookups).toBeGreaterThan(0);
|
||||
});
|
||||
it(
|
||||
"under Bun, which the Cloud Run image runs",
|
||||
() => {
|
||||
const run = spawnSync("bun", [FIXTURE], {
|
||||
encoding: "utf8",
|
||||
env: envWithoutProxy(),
|
||||
timeout: BUN_START_MS,
|
||||
});
|
||||
expect(run.status, run.stderr).toBe(0);
|
||||
const result = JSON.parse(run.stdout.trim().split("\n").at(-1) ?? "{}");
|
||||
expect(result.outcome).toBe("http_rejected");
|
||||
expect(result.lookups).toBeGreaterThan(0);
|
||||
},
|
||||
BUN_START_MS,
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user