mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-15 22:08:03 +00:00
perf(studio): render thumbnails from one loaded page
Every thumbnail opened a page and loaded the whole composition to shoot one frame. The server now keeps one loaded page per preview document and seeks it per frame, reloading only when the project content changes. A page closes 10 s after its last use, and only after the frame still running on it finishes, so an idle Studio keeps no composition running.
This commit is contained in:
@@ -51,6 +51,7 @@ import type { ScreenshotClip } from "@hyperframes/studio-server/screenshot-clip"
|
||||
import type { RenderJob } from "@hyperframes/producer";
|
||||
import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution";
|
||||
import { seekCompositionTimeline } from "../capture/captureCompositionFrame.js";
|
||||
import { createThumbnailPages } from "./thumbnailPages.js";
|
||||
import {
|
||||
assertWebGpuAdapterAvailable,
|
||||
compositionRequiresWebGpu,
|
||||
@@ -214,6 +215,7 @@ async function downloadRemoteGifImageSources(
|
||||
// share a single Chrome process instead of running two independent ones.
|
||||
|
||||
let _thumbnailBrowserLease: import("@hyperframes/engine").BrowserLease | null = null;
|
||||
const thumbnailPages = createThumbnailPages();
|
||||
let _thumbnailBrowserInitializing: Promise<ThumbnailBrowserSession | null> | null = null;
|
||||
let _thumbnailBrowserModes: {
|
||||
requested: BrowserGpuMode;
|
||||
@@ -295,6 +297,7 @@ async function getThumbnailBrowser(
|
||||
}
|
||||
|
||||
async function closeThumbnailBrowser(): Promise<void> {
|
||||
thumbnailPages.closeAll();
|
||||
// A launch kicked off just before this call isn't in _thumbnailBrowserLease
|
||||
// yet; awaiting it here closes a browser that was mid-launch when the stop
|
||||
// signal arrived, instead of leaving it running, unreferenced, after exit.
|
||||
@@ -630,66 +633,66 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
|
||||
const requiresWebGpu = existsSync(sourcePath)
|
||||
? compositionRequiresWebGpu(readFileSync(sourcePath, "utf-8"))
|
||||
: false;
|
||||
let page: import("puppeteer-core").Page | null = null;
|
||||
const closePage = () => void page?.close().catch(() => {});
|
||||
opts.signal.addEventListener("abort", closePage, { once: true });
|
||||
if (opts.signal.aborted) return null;
|
||||
const width = opts.width || 1920;
|
||||
const height = opts.height || 1080;
|
||||
const viewport = { width, height, deviceScaleFactor: thumbnailDeviceScaleFactor(opts) };
|
||||
try {
|
||||
page = await session.browser.newPage();
|
||||
if (opts.signal.aborted) return null;
|
||||
const width = opts.width || 1920;
|
||||
const height = opts.height || 1080;
|
||||
await page.setViewport({
|
||||
width,
|
||||
height,
|
||||
deviceScaleFactor: thumbnailDeviceScaleFactor(opts),
|
||||
});
|
||||
await page.goto(opts.previewUrl, { waitUntil: "domcontentloaded", timeout: 10000 });
|
||||
await assertWebGpuAdapterAvailable(page, requiresWebGpu);
|
||||
await page
|
||||
.waitForFunction(
|
||||
() => {
|
||||
const w = window as Window & {
|
||||
__timelines?: Record<string, unknown>;
|
||||
};
|
||||
return !!(w.__timelines && Object.keys(w.__timelines).length > 0);
|
||||
},
|
||||
{ timeout: 5000 },
|
||||
)
|
||||
.catch(() => {});
|
||||
await seekCompositionTimeline(page, opts.seekTime, {
|
||||
fallbackToBridgeAndTimelines: true,
|
||||
waitForPreferredSeekTargetMs: 500,
|
||||
animationFrameSettle: "double",
|
||||
waitForFontsMs: 500,
|
||||
});
|
||||
const manifestContent = readStudioManualEditManifestContent(opts.project.dir);
|
||||
await applyStudioManualEditsToThumbnailPage(page, manifestContent, opts.compPath);
|
||||
await page.evaluate(() => {
|
||||
void document.fonts?.ready;
|
||||
const body = document.body;
|
||||
if (body && getComputedStyle(body).backgroundColor === "rgba(0, 0, 0, 0)") {
|
||||
body.style.backgroundColor = "#1c2028";
|
||||
}
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
await reapplyStudioManualEditsToThumbnailPage(page);
|
||||
let clip: ScreenshotClip | undefined;
|
||||
if (opts.selector) {
|
||||
clip = await page.evaluate(getElementScreenshotClip, opts.selector, opts.selectorIndex);
|
||||
}
|
||||
const screenshot = (await page.screenshot(
|
||||
opts.format === "png"
|
||||
? {
|
||||
type: "png",
|
||||
...(clip ? { clip } : {}),
|
||||
return await thumbnailPages.withPage(
|
||||
session.browser,
|
||||
opts.previewUrl,
|
||||
createProjectSignature(opts.project.dir),
|
||||
async (page) => {
|
||||
await page.setViewport(viewport);
|
||||
await page.goto(opts.previewUrl, { waitUntil: "domcontentloaded", timeout: 10000 });
|
||||
await assertWebGpuAdapterAvailable(page, requiresWebGpu);
|
||||
await page
|
||||
.waitForFunction(
|
||||
() => {
|
||||
const w = window as Window & {
|
||||
__timelines?: Record<string, unknown>;
|
||||
};
|
||||
return !!(w.__timelines && Object.keys(w.__timelines).length > 0);
|
||||
},
|
||||
{ timeout: 5000 },
|
||||
)
|
||||
.catch(() => {});
|
||||
},
|
||||
async (page) => {
|
||||
if (opts.signal.aborted) return null;
|
||||
await page.setViewport(viewport);
|
||||
await seekCompositionTimeline(page, opts.seekTime, {
|
||||
fallbackToBridgeAndTimelines: true,
|
||||
waitForPreferredSeekTargetMs: 500,
|
||||
animationFrameSettle: "double",
|
||||
waitForFontsMs: 500,
|
||||
});
|
||||
const manifestContent = readStudioManualEditManifestContent(opts.project.dir);
|
||||
await applyStudioManualEditsToThumbnailPage(page, manifestContent, opts.compPath);
|
||||
await page.evaluate(() => {
|
||||
void document.fonts?.ready;
|
||||
const body = document.body;
|
||||
if (body && getComputedStyle(body).backgroundColor === "rgba(0, 0, 0, 0)") {
|
||||
body.style.backgroundColor = "#1c2028";
|
||||
}
|
||||
: {
|
||||
type: "jpeg",
|
||||
quality: 80,
|
||||
...(clip ? { clip } : {}),
|
||||
},
|
||||
)) as Buffer;
|
||||
return screenshot;
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
await reapplyStudioManualEditsToThumbnailPage(page);
|
||||
let clip: ScreenshotClip | undefined;
|
||||
if (opts.selector) {
|
||||
clip = await page.evaluate(
|
||||
getElementScreenshotClip,
|
||||
opts.selector,
|
||||
opts.selectorIndex,
|
||||
);
|
||||
}
|
||||
return (await page.screenshot(
|
||||
opts.format === "png"
|
||||
? { type: "png", ...(clip ? { clip } : {}) }
|
||||
: { type: "jpeg", quality: 80, ...(clip ? { clip } : {}) },
|
||||
)) as Buffer;
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
if (!opts.signal.aborted) {
|
||||
console.warn(
|
||||
@@ -698,9 +701,6 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
|
||||
);
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
opts.signal.removeEventListener("abort", closePage);
|
||||
await page?.close().catch(() => {});
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { Browser, Page } from "puppeteer-core";
|
||||
import { createThumbnailPages } from "./thumbnailPages.js";
|
||||
|
||||
function fakeBrowser() {
|
||||
const pages: Array<{ close: ReturnType<typeof vi.fn> }> = [];
|
||||
const browser = {
|
||||
newPage: vi.fn(async () => {
|
||||
const page = { close: vi.fn(async () => {}) };
|
||||
pages.push(page);
|
||||
return page as unknown as Page;
|
||||
}),
|
||||
} as unknown as Browser;
|
||||
return { browser, pages };
|
||||
}
|
||||
|
||||
describe("createThumbnailPages", () => {
|
||||
it("loads a document once and serves every frame from that page, one at a time", async () => {
|
||||
const { browser } = fakeBrowser();
|
||||
const thumbnails = createThumbnailPages();
|
||||
const load = vi.fn(async () => {});
|
||||
const order: string[] = [];
|
||||
const shot = (name: string) => async () => {
|
||||
order.push(`${name}:start`);
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
order.push(`${name}:end`);
|
||||
return name;
|
||||
};
|
||||
|
||||
const frames = await Promise.all([
|
||||
thumbnails.withPage(browser, "/preview", "v1", load, shot("t0")),
|
||||
thumbnails.withPage(browser, "/preview", "v1", load, shot("t3")),
|
||||
]);
|
||||
|
||||
expect(frames).toEqual(["t0", "t3"]);
|
||||
expect(browser.newPage).toHaveBeenCalledTimes(1);
|
||||
expect(load).toHaveBeenCalledTimes(1);
|
||||
expect(order).toEqual(["t0:start", "t0:end", "t3:start", "t3:end"]);
|
||||
});
|
||||
|
||||
it("reloads when the project content changes and after a failed frame", async () => {
|
||||
const { browser, pages } = fakeBrowser();
|
||||
const thumbnails = createThumbnailPages();
|
||||
const load = vi.fn(async () => {});
|
||||
|
||||
await thumbnails.withPage(browser, "/preview", "v1", load, async () => null);
|
||||
await thumbnails.withPage(browser, "/preview", "v2", load, async () => null);
|
||||
expect(load).toHaveBeenCalledTimes(2);
|
||||
expect(pages[0]?.close).toHaveBeenCalled();
|
||||
|
||||
await expect(
|
||||
thumbnails.withPage(browser, "/preview", "v2", load, async () => {
|
||||
throw new Error("page crashed");
|
||||
}),
|
||||
).rejects.toThrow("page crashed");
|
||||
await thumbnails.withPage(browser, "/preview", "v2", load, async () => null);
|
||||
expect(load).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("closes a page nobody has used for a while, so an idle Studio runs no composition", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const { browser, pages } = fakeBrowser();
|
||||
const thumbnails = createThumbnailPages(2, 10_000);
|
||||
const load = vi.fn(async () => {});
|
||||
|
||||
await thumbnails.withPage(browser, "/preview", "v1", load, async () => null);
|
||||
await vi.advanceTimersByTimeAsync(9_999);
|
||||
expect(pages[0]?.close).not.toHaveBeenCalled();
|
||||
await vi.advanceTimersByTimeAsync(1);
|
||||
expect(pages[0]?.close).toHaveBeenCalled();
|
||||
|
||||
await thumbnails.withPage(browser, "/preview", "v1", load, async () => null);
|
||||
expect(load).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("never closes a page under a frame that is still being taken", async () => {
|
||||
const { browser, pages } = fakeBrowser();
|
||||
const thumbnails = createThumbnailPages(1);
|
||||
const load = vi.fn(async () => {});
|
||||
let finish: () => void = () => {};
|
||||
const slow = thumbnails.withPage(
|
||||
browser,
|
||||
"/a",
|
||||
"v1",
|
||||
load,
|
||||
() => new Promise<void>((r) => (finish = r)),
|
||||
);
|
||||
await vi.waitFor(() => expect(pages).toHaveLength(1));
|
||||
|
||||
await thumbnails.withPage(browser, "/b", "v1", load, async () => null);
|
||||
expect(pages[0]?.close).not.toHaveBeenCalled();
|
||||
finish();
|
||||
await slow;
|
||||
await vi.waitFor(() => expect(pages[0]?.close).toHaveBeenCalled());
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
import type { Browser, Page } from "puppeteer-core";
|
||||
|
||||
interface LoadedPage {
|
||||
browser: Browser;
|
||||
version: string;
|
||||
page: Promise<Page>;
|
||||
queue: Promise<unknown>;
|
||||
idleTimer?: ReturnType<typeof setTimeout>;
|
||||
}
|
||||
|
||||
/** One loaded preview page per document URL: a thumbnail is a seek and a screenshot on it, not a
|
||||
* fresh load of the whole composition. A new `version` (project content) reloads the page, calls on
|
||||
* one page run one at a time so their seeks never interleave, and a page unused for `idleMs` closes
|
||||
* so an idle Studio keeps no composition running. */
|
||||
export function createThumbnailPages(maxPages = 2, idleMs = 10_000) {
|
||||
const pages = new Map<string, LoadedPage>();
|
||||
const drop = (url: string) => {
|
||||
const entry = pages.get(url);
|
||||
if (!entry) return;
|
||||
pages.delete(url);
|
||||
clearTimeout(entry.idleTimer);
|
||||
// After any frame still running on it.
|
||||
void entry.queue
|
||||
.then(() => entry.page)
|
||||
.then((page) => page.close())
|
||||
.catch(() => {});
|
||||
};
|
||||
return {
|
||||
async withPage<T>(
|
||||
browser: Browser,
|
||||
url: string,
|
||||
version: string,
|
||||
load: (page: Page) => Promise<void>,
|
||||
use: (page: Page) => Promise<T>,
|
||||
): Promise<T> {
|
||||
let entry = pages.get(url);
|
||||
if (entry && (entry.browser !== browser || entry.version !== version)) {
|
||||
drop(url);
|
||||
entry = undefined;
|
||||
}
|
||||
if (!entry) {
|
||||
const page = browser.newPage().then(async (created) => {
|
||||
await load(created);
|
||||
return created;
|
||||
});
|
||||
entry = { browser, version, page, queue: Promise.resolve() };
|
||||
pages.set(url, entry);
|
||||
while (pages.size > maxPages) drop(pages.keys().next().value!);
|
||||
}
|
||||
const current = entry;
|
||||
clearTimeout(current.idleTimer);
|
||||
const run = current.queue.then(async () => use(await current.page));
|
||||
current.queue = run.catch(() => {});
|
||||
try {
|
||||
return await run;
|
||||
} catch (error) {
|
||||
if (pages.get(url) === current) drop(url);
|
||||
throw error;
|
||||
} finally {
|
||||
if (pages.get(url) === current) {
|
||||
clearTimeout(current.idleTimer);
|
||||
current.idleTimer = setTimeout(() => {
|
||||
if (pages.get(url) === current) drop(url);
|
||||
}, idleMs);
|
||||
current.idleTimer.unref?.();
|
||||
}
|
||||
}
|
||||
},
|
||||
closeAll(): void {
|
||||
for (const url of [...pages.keys()]) drop(url);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user