diff --git a/lighthouse-core/scripts/benchmark-plus-extras.js b/lighthouse-core/scripts/benchmark-plus-extras.js new file mode 100644 index 0000000000..b72bb48193 --- /dev/null +++ b/lighthouse-core/scripts/benchmark-plus-extras.js @@ -0,0 +1,95 @@ +/** + * @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +/* global document */ + +/** + * @fileoverview This script computes the BenchmarkIndex and a few other related browser benchmarks. + */ + +const puppeteer = require('puppeteer'); +const {computeBenchmarkIndex} = require('../lib/page-functions.js'); + +/** @param {import('puppeteer').Page} page */ +async function runOctane(page) { + /** @param {import('puppeteer').ConsoleMessage} message */ + const pageLogger = message => process.stdout.write(` ${message.text()}\n`); + + process.stdout.write(`Running Octane...\n`); + await page.goto('https://chromium.github.io/octane/', {waitUntil: 'networkidle0'}); + await page.waitFor('#run-octane'); + await page.waitFor(5000); + page.on('console', pageLogger); + await page.click('#run-octane'); + await page.waitForFunction(() => { + const banner = document.querySelector('#main-banner'); + return /Octane Score: \d+/.test(banner && banner.textContent || ''); + }, {timeout: 300e3}); + + const score = await page.evaluate(() => { + const banner = document.querySelector('#main-banner'); + if (!banner || !banner.textContent) return 0; + const [_, score] = banner.textContent.match(/Octane Score: (\d+)/) || []; + return Number(score); + }); + process.stdout.write(` Octane: ${score}\n`); + + page.off('console', pageLogger); +} + +/** @param {import('puppeteer').Page} page */ +async function runSpeedometer(page) { + process.stdout.write(`Running Speedometer...\n`); + await page.goto('https://browserbench.org/Speedometer2.0/', {waitUntil: 'networkidle0'}); + await page.waitFor('#home button'); + await page.waitFor(5000); + await page.click('#home button'); + + const loggerInterval = setInterval(async () => { + const progress = await page.evaluate(() => { + const infoEl = document.querySelector('#running #info'); + return infoEl && infoEl.textContent || 'Unknown'; + }); + process.stdout.write(` Progress: ${progress}\n`); + }, 10000); + + await page.waitForSelector('#summarized-results.selected', {timeout: 600e3}); + clearInterval(loggerInterval); + + const score = await page.evaluate(() => { + const result = document.querySelector('#result-number'); + if (!result || !result.textContent) return 0; + return Number(result.textContent); + }); + process.stdout.write(` Speedometer: ${score}\n`); +} + +async function main() { + process.stdout.write(`Launching Chrome...\n`); + const browser = await puppeteer.launch({ + headless: true, + executablePath: process.env.CHROME_PATH, + }); + + const page = await browser.newPage(); + await page.goto('about:blank'); + + process.stdout.write(`Running BenchmarkIndex...\n`); + for (let i = 0; i < 10; i++) { + const BenchmarkIndex = await page.evaluate(computeBenchmarkIndex); + process.stdout.write(` ${i + 1}: BenchmarkIndex=${BenchmarkIndex}\n`); + } + + await runOctane(page); + await runSpeedometer(page); + await browser.close(); +} + +main().catch(err => { + process.stderr.write(err.stack); + process.exit(1); +}); diff --git a/package.json b/package.json index eb39613942..1c15faced8 100644 --- a/package.json +++ b/package.json @@ -97,6 +97,7 @@ "@types/lodash.isequal": "^4.5.2", "@types/lodash.set": "^4.3.6", "@types/node": "*", + "@types/puppeteer": "1.19.x", "@types/raven": "^2.5.1", "@types/resize-observer-browser": "^0.1.1", "@types/rimraf": "^2.0.2", diff --git a/yarn.lock b/yarn.lock index 9ea1cb9dce..d73405b060 100644 --- a/yarn.lock +++ b/yarn.lock @@ -663,6 +663,13 @@ resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA== +"@types/puppeteer@1.19.x": + version "1.19.1" + resolved "https://registry.yarnpkg.com/@types/puppeteer/-/puppeteer-1.19.1.tgz#942ca62288953a0f5fbbc25c103b5f2ba28b60ab" + integrity sha512-ReWZvoEfMiJIA3AG+eM+nCx5GKrU2ANVYY5TC0nbpeiTCtnJbcqnmBbR8TkXMBTvLBYcuTOAELbTcuX73siDNQ== + dependencies: + "@types/node" "*" + "@types/raven@^2.5.1": version "2.5.1" resolved "https://registry.yarnpkg.com/@types/raven/-/raven-2.5.1.tgz#62ef0a59e29691945e1f295b62ed199619cbd9b6"