2019-06-18 12:16:15 +00:00
|
|
|
/**
|
|
|
|
* Command line application to run Skottie-WASM perf on a Lottie file in the
|
|
|
|
* browser and then exporting the result.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const puppeteer = require('puppeteer');
|
|
|
|
const express = require('express');
|
|
|
|
const fs = require('fs');
|
|
|
|
const commandLineArgs = require('command-line-args');
|
|
|
|
const commandLineUsage= require('command-line-usage');
|
|
|
|
const fetch = require('node-fetch');
|
|
|
|
|
|
|
|
const opts = [
|
|
|
|
{
|
|
|
|
name: 'canvaskit_js',
|
|
|
|
typeLabel: '{underline file}',
|
|
|
|
description: 'The path to canvaskit.js.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'canvaskit_wasm',
|
|
|
|
typeLabel: '{underline file}',
|
|
|
|
description: 'The path to canvaskit.wasm.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'input',
|
|
|
|
typeLabel: '{underline file}',
|
|
|
|
description: 'The Lottie JSON file to process.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'output',
|
|
|
|
typeLabel: '{underline file}',
|
|
|
|
description: 'The perf file to write. Defaults to perf.json',
|
|
|
|
},
|
2019-07-12 17:16:40 +00:00
|
|
|
{
|
|
|
|
name: 'use_gpu',
|
|
|
|
description: 'Whether we should run in non-headless mode with GPU.',
|
|
|
|
type: Boolean,
|
|
|
|
},
|
2019-06-18 12:16:15 +00:00
|
|
|
{
|
|
|
|
name: 'port',
|
|
|
|
description: 'The port number to use, defaults to 8081.',
|
|
|
|
type: Number,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'help',
|
|
|
|
alias: 'h',
|
|
|
|
type: Boolean,
|
|
|
|
description: 'Print this usage guide.'
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const usage = [
|
|
|
|
{
|
|
|
|
header: 'Skottie WASM Perf',
|
|
|
|
content: "Command line application to run Skottie-WASM perf."
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: 'Options',
|
|
|
|
optionList: opts,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Parse and validate flags.
|
|
|
|
const options = commandLineArgs(opts);
|
|
|
|
|
|
|
|
if (!options.output) {
|
|
|
|
options.output = 'perf.json';
|
|
|
|
}
|
|
|
|
if (!options.port) {
|
|
|
|
options.port = 8081;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.help) {
|
|
|
|
console.log(commandLineUsage(usage));
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.canvaskit_js) {
|
|
|
|
console.error('You must supply path to canvaskit.js.');
|
|
|
|
console.log(commandLineUsage(usage));
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.canvaskit_wasm) {
|
|
|
|
console.error('You must supply path to canvaskit.wasm.');
|
|
|
|
console.log(commandLineUsage(usage));
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.input) {
|
|
|
|
console.error('You must supply a Lottie JSON filename.');
|
|
|
|
console.log(commandLineUsage(usage));
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start up a web server to serve the three files we need.
|
|
|
|
let canvasKitJS = fs.readFileSync(options.canvaskit_js, 'utf8');
|
|
|
|
let canvasKitWASM = fs.readFileSync(options.canvaskit_wasm, 'binary');
|
|
|
|
let driverHTML = fs.readFileSync('skottie-wasm-perf.html', 'utf8');
|
|
|
|
let lottieJSON = fs.readFileSync(options.input, 'utf8');
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.get('/', (req, res) => res.send(driverHTML));
|
|
|
|
app.get('/res/canvaskit.wasm', function(req, res) {
|
|
|
|
res.type('application/wasm');
|
|
|
|
res.send(new Buffer(canvasKitWASM, 'binary'));
|
|
|
|
});
|
|
|
|
app.get('/res/canvaskit.js', (req, res) => res.send(canvasKitJS));
|
|
|
|
app.get('/res/lottie.json', (req, res) => res.send(lottieJSON));
|
|
|
|
app.listen(options.port, () => console.log('- Local web server started.'))
|
|
|
|
|
|
|
|
// Utility function.
|
|
|
|
async function wait(ms) {
|
|
|
|
await new Promise(resolve => setTimeout(() => resolve(), ms));
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2019-12-03 17:56:40 +00:00
|
|
|
let hash = "#cpu";
|
|
|
|
if (options.use_gpu) {
|
|
|
|
hash = "#gpu";
|
|
|
|
}
|
|
|
|
const targetURL = `http://localhost:${options.port}/${hash}`;
|
2019-07-12 13:33:06 +00:00
|
|
|
const viewPort = {width: 1000, height: 1000};
|
2019-06-18 12:16:15 +00:00
|
|
|
|
|
|
|
// Drive chrome to load the web page from the server we have running.
|
|
|
|
async function driveBrowser() {
|
2019-06-26 13:41:24 +00:00
|
|
|
console.log('- Launching chrome for ' + options.input);
|
|
|
|
let browser;
|
|
|
|
let page;
|
2019-07-12 17:16:40 +00:00
|
|
|
const headless = !options.use_gpu;
|
|
|
|
let browser_args = [
|
|
|
|
'--no-sandbox',
|
|
|
|
'--disable-setuid-sandbox',
|
|
|
|
'--window-size=' + viewPort.width + ',' + viewPort.height,
|
|
|
|
];
|
|
|
|
if (options.use_gpu) {
|
|
|
|
browser_args.push('--ignore-gpu-blacklist');
|
2019-09-06 18:13:57 +00:00
|
|
|
browser_args.push('--ignore-gpu-blocklist');
|
2019-07-12 17:16:40 +00:00
|
|
|
browser_args.push('--enable-gpu-rasterization');
|
|
|
|
}
|
|
|
|
console.log("Running with headless: " + headless + " args: " + browser_args);
|
2019-06-26 13:41:24 +00:00
|
|
|
try {
|
2019-07-12 17:16:40 +00:00
|
|
|
browser = await puppeteer.launch({headless: headless, args: browser_args});
|
2019-06-26 13:41:24 +00:00
|
|
|
page = await browser.newPage();
|
2019-07-12 13:33:06 +00:00
|
|
|
await page.setViewport(viewPort);
|
2019-06-26 13:41:24 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log('Could not open the browser.', e);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2019-06-18 12:16:15 +00:00
|
|
|
console.log("Loading " + targetURL);
|
|
|
|
try {
|
2019-07-08 18:49:48 +00:00
|
|
|
// Start trace.
|
|
|
|
await page.tracing.start({
|
|
|
|
path: options.output,
|
|
|
|
screenshots: false,
|
2019-07-12 01:18:43 +00:00
|
|
|
categories: ["blink", "cc", "gpu"]
|
2019-07-08 18:49:48 +00:00
|
|
|
});
|
|
|
|
|
2019-06-18 12:16:15 +00:00
|
|
|
await page.goto(targetURL, {
|
2019-07-11 17:48:41 +00:00
|
|
|
timeout: 60000,
|
2019-06-18 12:16:15 +00:00
|
|
|
waitUntil: 'networkidle0'
|
|
|
|
});
|
2019-07-08 18:49:48 +00:00
|
|
|
|
2020-09-03 14:02:10 +00:00
|
|
|
console.log('Waiting 90s for run to be done');
|
2019-12-03 17:56:40 +00:00
|
|
|
await page.waitForFunction(`(window._skottieDone === true) || window._error`, {
|
2020-09-03 14:02:10 +00:00
|
|
|
timeout: 90000,
|
2019-06-18 12:16:15 +00:00
|
|
|
});
|
2019-07-08 18:49:48 +00:00
|
|
|
|
2019-12-03 17:56:40 +00:00
|
|
|
const err = await page.evaluate('window._error');
|
|
|
|
if (err) {
|
|
|
|
console.log(`ERROR: ${err}`)
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:49:48 +00:00
|
|
|
// Stop Trace.
|
|
|
|
await page.tracing.stop();
|
2019-06-18 12:16:15 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log('Timed out while loading or drawing. Either the JSON file was ' +
|
|
|
|
'too big or hit a bug.', e);
|
|
|
|
await browser.close();
|
2019-07-11 17:48:41 +00:00
|
|
|
process.exit(1);
|
2019-06-18 12:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await browser.close();
|
|
|
|
// Need to call exit() because the web server is still running.
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
driveBrowser();
|