2 * Command line application to run Skottie-WASM perf on a Lottie file in the
3 * browser and then exporting the result.
6 const puppeteer = require('puppeteer');
7 const express = require('express');
8 const fs = require('fs');
9 const commandLineArgs = require('command-line-args');
10 const commandLineUsage= require('command-line-usage');
11 const fetch = require('node-fetch');
16 typeLabel: '{underline file}',
17 description: 'The path to canvaskit.js.'
20 name: 'canvaskit_wasm',
21 typeLabel: '{underline file}',
22 description: 'The path to canvaskit.wasm.'
26 typeLabel: '{underline file}',
27 description: 'The Lottie JSON file to process.'
31 typeLabel: '{underline file}',
32 description: 'The perf file to write. Defaults to perf.json',
36 description: 'Whether we should run in non-headless mode with GPU.',
41 description: 'The port number to use, defaults to 8081.',
48 description: 'Print this usage guide.'
54 header: 'Skottie WASM Perf',
55 content: "Command line application to run Skottie-WASM perf."
63 // Parse and validate flags.
64 const options = commandLineArgs(opts);
66 if (!options.output) {
67 options.output = 'perf.json';
74 console.log(commandLineUsage(usage));
78 if (!options.canvaskit_js) {
79 console.error('You must supply path to canvaskit.js.');
80 console.log(commandLineUsage(usage));
84 if (!options.canvaskit_wasm) {
85 console.error('You must supply path to canvaskit.wasm.');
86 console.log(commandLineUsage(usage));
91 console.error('You must supply a Lottie JSON filename.');
92 console.log(commandLineUsage(usage));
96 // Start up a web server to serve the three files we need.
97 let canvasKitJS = fs.readFileSync(options.canvaskit_js, 'utf8');
98 let canvasKitWASM = fs.readFileSync(options.canvaskit_wasm, 'binary');
99 let driverHTML = fs.readFileSync('skottie-wasm-perf.html', 'utf8');
100 let lottieJSON = fs.readFileSync(options.input, 'utf8');
102 const app = express();
103 app.get('/', (req, res) => res.send(driverHTML));
104 app.get('/res/canvaskit.wasm', function(req, res) {
105 res.type('application/wasm');
106 res.send(new Buffer(canvasKitWASM, 'binary'));
108 app.get('/res/canvaskit.js', (req, res) => res.send(canvasKitJS));
109 app.get('/res/lottie.json', (req, res) => res.send(lottieJSON));
110 app.listen(options.port, () => console.log('- Local web server started.'))
113 async function wait(ms) {
114 await new Promise(resolve => setTimeout(() => resolve(), ms));
119 if (options.use_gpu) {
122 const targetURL = `http://localhost:${options.port}/${hash}`;
123 const viewPort = {width: 1000, height: 1000};
125 // Drive chrome to load the web page from the server we have running.
126 async function driveBrowser() {
127 console.log('- Launching chrome for ' + options.input);
130 const headless = !options.use_gpu;
133 '--disable-setuid-sandbox',
134 '--window-size=' + viewPort.width + ',' + viewPort.height,
136 if (options.use_gpu) {
137 browser_args.push('--ignore-gpu-blacklist');
138 browser_args.push('--ignore-gpu-blocklist');
139 browser_args.push('--enable-gpu-rasterization');
141 console.log("Running with headless: " + headless + " args: " + browser_args);
143 browser = await puppeteer.launch({headless: headless, args: browser_args});
144 page = await browser.newPage();
145 await page.setViewport(viewPort);
147 console.log('Could not open the browser.', e);
150 console.log("Loading " + targetURL);
153 await page.tracing.start({
154 path: options.output,
156 categories: ["blink", "cc", "gpu"]
159 await page.goto(targetURL, {
161 waitUntil: 'networkidle0'
164 console.log('Waiting 90s for run to be done');
165 await page.waitForFunction(`(window._skottieDone === true) || window._error`, {
169 const err = await page.evaluate('window._error');
171 console.log(`ERROR: ${err}`)
176 await page.tracing.stop();
178 console.log('Timed out while loading or drawing. Either the JSON file was ' +
179 'too big or hit a bug.', e);
180 await browser.close();
184 await browser.close();
185 // Need to call exit() because the web server is still running.