npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

puppeteer-performance-gatherer

v1.0.0

Published

Gather all kinds of resources like lighthouse audits and chrome devtools tracing while running puppeteer

Downloads

4

Readme

Puppeteer Performance Gatherer

Installation

npm i puppeteer-performance-gatherer --save

Usage

Basic

const puppeteer = require("puppeteer");
const Gatherer = require("puppeteer-performance-gatherer");

let browser = await puppeteer.launch(opts);
let page = await browser.newPage();

// initialize the gatherer with the puppeteer page
let gatherer = new Gatherer(page, {url: "www.example.com", name: "example"});

// run lighthouse on the given url and capture tracing of the given page
await gatherer.start({"lighthouse": true, "tracing": true});

// do stuff that is captured by the tracing
await page.goto("www.example.com");

// stop tracing
await gatherer.stop();

A gatherer always needs a puppeteer page, the url and name are optional:

  • url: The endpoint that lighthouse will use to run an audit. If this is null, lighthouse will use page.url();
  • name: The tracing output file name

gatherer.start() also accepts lighthouse and tracing options:

// port and output are always overriden by the gatherer
let lighthouseOptions = {
    disableStorageReset: true,
    disableDeviceEmulation: true,
    emulatedFormFactor: 'desktop'
}

// name and dir are custom settings that have precedence over path
// The default dir is traces
let tracingOptions = {
    path: "traces/trace.json",
    name: "trace",
    dir: "traces",
    screenshots: false,
    categories: [],
}

await gatherer.start({lighthouse: lighthouseOptions, tracing: tracingOptions});

You don't need to use gatherer.start(). You can run lighthouse and tracing manually:

await gatherer.runLighthouse(options, config);

await gatherer.captureTracing(options);
await gatherer.stopTracing();

Lighthouse

A gatherer can only have one lighthouse result that can be accessed by using gatherer.lighthouse

Using the gatherer.lighthouseAudits and gatherer.lighthouseCategories methods, you can easily get the lighthouse audit results, perfect for testing:

const accessibilityScore = gatherer.lighthouseCategories.get('accessibility');
expect(accessibilityScore).to.be.at.least(90);

const performanceScore = gatherer.lighthouseCategories.get('performance');
expect(performanceScore).to.be.at.least(75);

const contrastCheck = gatherer.lighthouseAudits.get('color-contrast');
expect(contrastCheck).to.equal('Pass');

const pageSpeedScore = gatherer.lighthouseAudits.get('speed-index');
expect(pageSpeedScore).to.be.at.least(75);

Performance

The gatherer can also extract performance entries from the page (https://developer.mozilla.org/en-US/docs/Web/API/PerformanceEntry)

Some examples:

const resourceTimings = await gatherer.extractResourceTimings();

const [navigationTiming] = await gatherer.extractNavigationTimings();
console.log(navigationTiming["domInteractive"]);

const paintTimings = await gatherer.extractPerformanceTimingsByType("paint");

const [measure] = await gatherer.extractPerformanceTimingsByName("name", "measure");
console.log(measure.duration);

const performanceTimeline = await gatherer.extractPerformanceTimings();

Emulation

const { NETWORK_PRESETS } = require("puppeteer-performance-gatherer/utils/emulation");

// gatherer.emulate(network_preset, cpuThrottleRate)
await gatherer.emulate(NETWORK_PRESETS.Regular2G, 2);

await gatherer.emulate({
    'offline': false,
    'downloadThroughput': 30 * 1024 * 1024 / 8,
    'uploadThroughput': 15 * 1024 * 1024 / 8,
    'latency': 2
}, 0);

Asynchronous performance testing

Imagine that we have a website with a button that asynchronously loads some data, we can get the loadtime by doing the following:

let button = await page.$('#button');

await button.click().then(async () => await gatherer.waitUntilDomSilent(".content", 1000, "exampleID"));

const [measure] = await gatherer.extractPerformanceTimingsByName("exampleID", "measure");
console.log(measure.duration);

How does gatherer.waitUntilDomSilent(selector, timeout, measureID) work?

The gatherer will use a mutationobserver to observe the given selector for changes. If the given selector doesn't change for the duration of the timeout, we consider the selector loaded. The gatherer will create a performance entry with the given measureID so you can retrieve the time needed to load the data. In the example this will be the time needed to fetch the new data and populate the DOM node for the given selector.

Final notes

I've made this package during my internship at Info Support to be able to test performance of asynchronous applications using puppeteer, lighthouse and mocha.