simonw/shot-scraper

Experimental feature: heap snapshots

simonw opened this issue · 1 comments

simonw commented

Here's the prototype:

@cli.command()
@click.argument("url")
@click.option(
    "-a",
    "--auth",
    type=click.File("r"),
    help="Path to JSON authentication context file",
)
def snapshot(url, auth):
    "Return a heap snapshot of the specified URL"
    url = url_or_file_path(url, _check_and_absolutize)
    with sync_playwright() as p:
        context, browser_obj = _browser_context(
            p,
            auth,
            # browser=browser,
            # user_agent=user_agent,
            # reduced_motion=reduced_motion,
        )
        page = context.new_page()
        client = page.context.new_cdp_session(page)

        chunks = []

        def store_chunk(chunk):
            chunks.append(chunk["chunk"])

        client.on("HeapProfiler.addHeapSnapshotChunk", store_chunk)

        page.goto(url)

        client.send("HeapProfiler.takeHeapSnapshot", {})

        combined = "".join(chunks)
        data = json.loads(combined)
        click.echo(json.dumps(data, indent=2))

@simonw I've ported the original project to Python & Playwright https://github.com/byt3bl33d3r/playwright-heap-snapshot

Feel free to steal