Automating Google Lighthouse

Saturday, March 18th 2023

To run multiple instances of Google Lighthouse NPM, you can use a library like p-limit or p-queue to control the concurrency of running Lighthouse in parallel. Here's an example using p-limit:

const lighthouse = require('lighthouse'); const chromeLauncher = require('chrome-launcher'); const plimit = require('p-limit'); // Set up options for Lighthouse const lighthouseOptions = { output: 'html', logLevel: 'info', onlyCategories: ['performance'], port: undefined }; // Define a function to run Lighthouse on a given URL async function runLighthouse(url) { const chrome = await chromeLauncher.launch({chromeFlags: ['--headless']}); lighthouseOptions.port = chrome.port; const results = await lighthouse(url, lighthouseOptions); await chrome.kill(); return results.report; } // Set up an array of URLs to test const urls = ['https://www.example.com', 'https://www.anotherexample.com', 'https://www.yetanotherexample.com']; // Set up a concurrency limit of 2 instances of Lighthouse const limit = plimit(2); // Run Lighthouse for each URL using p-limit async function runLighthouseForUrls() { const promises = urls.map(url => limit(() => runLighthouse(url))); const results = await Promise.all(promises); console.log(results); } runLighthouseForUrls();

In this example, we set up options for Lighthouse and define a function runLighthouse() that takes a URL and runs Lighthouse on it. We then set up an array of URLs to test and use p-limit to control the concurrency of running Lighthouse in parallel. Finally, we call runLighthouseForUrls() to run Lighthouse for each URL using p-limit.

Note that in this example we set a concurrency limit of 2 instances of Lighthouse, but you can adjust this value to suit your needs.