blob: 3321cf69cf86c787fc5e9bbcd43e43608785cdcf [file] [log] [blame]
Ravi Mistry6ac37952019-06-19 11:26:00 -04001/**
2 * Command line application to run Lottie-Web perf on a Lottie file in the
3 * browser and then exporting that result.
4 *
5 */
6const puppeteer = require('puppeteer');
7const express = require('express');
8const fs = require('fs');
9const commandLineArgs = require('command-line-args');
10const commandLineUsage= require('command-line-usage');
11const fetch = require('node-fetch');
12
13const opts = [
14 {
15 name: 'input',
16 typeLabel: '{underline file}',
17 description: 'The Lottie JSON file to process.'
18 },
19 {
20 name: 'output',
21 typeLabel: '{underline file}',
22 description: 'The perf file to write. Defaults to perf.json',
23 },
24 {
Ravi Mistry4c5edc22019-07-12 17:16:40 +000025 name: 'use_gpu',
26 description: 'Whether we should run in non-headless mode with GPU.',
27 type: Boolean,
28 },
29 {
Ravi Mistry6ac37952019-06-19 11:26:00 -040030 name: 'port',
31 description: 'The port number to use, defaults to 8081.',
32 type: Number,
33 },
34 {
35 name: 'lottie_player',
36 description: 'The path to lottie.min.js, defaults to a local npm install location.',
37 type: String,
38 },
39 {
Ravi Mistryd6cb7ae2019-07-24 21:36:41 +000040 name: 'backend',
41 description: 'Which lottie-web backend to use. Options: canvas or svg.',
42 type: String,
43 },
44 {
Ravi Mistry6ac37952019-06-19 11:26:00 -040045 name: 'help',
46 alias: 'h',
47 type: Boolean,
48 description: 'Print this usage guide.'
49 },
50];
51
52const usage = [
53 {
54 header: 'Lottie-Web Perf',
55 content: 'Command line application to run Lottie-Web perf.',
56 },
57 {
58 header: 'Options',
59 optionList: opts,
60 },
61];
62
63// Parse and validate flags.
64const options = commandLineArgs(opts);
65
Ravi Mistryd6cb7ae2019-07-24 21:36:41 +000066if (options.backend != 'canvas' && options.backend != 'svg') {
67 console.error('You must supply a lottie-web backend (canvas, svg).');
68 console.log(commandLineUsage(usage));
69 process.exit(1);
70}
71
Ravi Mistry6ac37952019-06-19 11:26:00 -040072if (!options.output) {
73 options.output = 'perf.json';
74}
75if (!options.port) {
76 options.port = 8081;
77}
Ravi Mistry0233b1e2019-06-19 14:47:59 -040078if (!options.lottie_player) {
79 options.lottie_player = 'node_modules/lottie-web/build/player/lottie.min.js';
80}
Ravi Mistry6ac37952019-06-19 11:26:00 -040081
82if (options.help) {
83 console.log(commandLineUsage(usage));
84 process.exit(0);
85}
86
87if (!options.input) {
88 console.error('You must supply a Lottie JSON filename.');
89 console.log(commandLineUsage(usage));
90 process.exit(1);
91}
92
93// Start up a web server to serve the three files we need.
94let lottieJS = fs.readFileSync(options.lottie_player, 'utf8');
Ravi Mistry6ac37952019-06-19 11:26:00 -040095let lottieJSON = fs.readFileSync(options.input, 'utf8');
Ravi Mistryd6cb7ae2019-07-24 21:36:41 +000096let driverHTML;
97if (options.backend == 'svg') {
98 console.log('Using lottie-web-perf.html');
99 driverHTML = fs.readFileSync('lottie-web-perf.html', 'utf8');
100} else {
101 console.log('Using lottie-web-canvas-perf.html');
102 driverHTML = fs.readFileSync('lottie-web-canvas-perf.html', 'utf8');
103}
Ravi Mistry6ac37952019-06-19 11:26:00 -0400104
Ravi Mistryb2be4102019-07-08 18:48:12 +0000105// Find number of frames from the lottie JSON.
106let lottieJSONContent = JSON.parse(lottieJSON);
107const totalFrames = lottieJSONContent.op - lottieJSONContent.ip;
108console.log('Total frames: ' + totalFrames);
109
Ravi Mistry6ac37952019-06-19 11:26:00 -0400110const app = express();
111app.get('/', (req, res) => res.send(driverHTML));
112app.get('/res/lottie.js', (req, res) => res.send(lottieJS));
113app.get('/res/lottie.json', (req, res) => res.send(lottieJSON));
114app.listen(options.port, () => console.log('- Local web server started.'))
115
116// Utility function.
117async function wait(ms) {
118 await new Promise(resolve => setTimeout(() => resolve(), ms));
119 return ms;
120}
121
Ravi Mistryb2be4102019-07-08 18:48:12 +0000122const targetURL = "http://localhost:" + options.port + "/#" + totalFrames;
Florin Malitaaa040682019-07-12 09:33:06 -0400123const viewPort = {width: 1000, height: 1000};
Ravi Mistry6ac37952019-06-19 11:26:00 -0400124
125// Drive chrome to load the web page from the server we have running.
126async function driveBrowser() {
Ravi Mistryb2ca0062019-06-20 15:56:24 +0000127 console.log('- Launching chrome for ' + options.input);
Ravi Mistry83262392019-06-26 13:41:24 +0000128 let browser;
129 let page;
Ravi Mistry4c5edc22019-07-12 17:16:40 +0000130 const headless = !options.use_gpu;
131 let browser_args = [
132 '--no-sandbox',
133 '--disable-setuid-sandbox',
134 '--window-size=' + viewPort.width + ',' + viewPort.height,
135 ];
136 if (options.use_gpu) {
137 browser_args.push('--ignore-gpu-blacklist');
Chris Blume08b68db2019-09-06 11:13:57 -0700138 browser_args.push('--ignore-gpu-blocklist');
Ravi Mistry4c5edc22019-07-12 17:16:40 +0000139 browser_args.push('--enable-gpu-rasterization');
140 }
141 console.log("Running with headless: " + headless + " args: " + browser_args);
Ravi Mistry83262392019-06-26 13:41:24 +0000142 try {
Ravi Mistry4c5edc22019-07-12 17:16:40 +0000143 browser = await puppeteer.launch({headless: headless, args: browser_args});
Ravi Mistry83262392019-06-26 13:41:24 +0000144 page = await browser.newPage();
Florin Malitaaa040682019-07-12 09:33:06 -0400145 await page.setViewport(viewPort);
Ravi Mistry83262392019-06-26 13:41:24 +0000146 } catch (e) {
147 console.log('Could not open the browser.', e);
148 process.exit(1);
149 }
150
Ravi Mistry6ac37952019-06-19 11:26:00 -0400151 console.log("Loading " + targetURL);
152 try {
Ravi Mistryb2be4102019-07-08 18:48:12 +0000153 // Start trace.
154 await page.tracing.start({
155 path: options.output,
156 screenshots: false,
Florin Malita85900262019-07-11 21:18:43 -0400157 categories: ["blink", "cc", "gpu"]
Ravi Mistryb2be4102019-07-08 18:48:12 +0000158 });
159
Ravi Mistry6ac37952019-06-19 11:26:00 -0400160 await page.goto(targetURL, {
Ravi Mistry37a4bed2019-07-11 17:48:41 +0000161 timeout: 60000,
Ravi Mistry6ac37952019-06-19 11:26:00 -0400162 waitUntil: 'networkidle0'
163 });
Ravi Mistryb2be4102019-07-08 18:48:12 +0000164
Ravi Mistry37a4bed2019-07-11 17:48:41 +0000165 console.log('- Waiting 60s for run to be done.');
Ravi Mistry6ac37952019-06-19 11:26:00 -0400166 await page.waitForFunction('window._lottieWebDone === true', {
Ravi Mistry37a4bed2019-07-11 17:48:41 +0000167 timeout: 60000,
Ravi Mistry6ac37952019-06-19 11:26:00 -0400168 });
Ravi Mistryb2be4102019-07-08 18:48:12 +0000169
170 // Stop trace.
171 await page.tracing.stop();
Ravi Mistry6ac37952019-06-19 11:26:00 -0400172 } catch(e) {
173 console.log('Timed out while loading or drawing. Either the JSON file was ' +
174 'too big or hit a bug in the player.', e);
175 await browser.close();
Ravi Mistry37a4bed2019-07-11 17:48:41 +0000176 process.exit(1);
Ravi Mistry6ac37952019-06-19 11:26:00 -0400177 }
178
Ravi Mistry6ac37952019-06-19 11:26:00 -0400179 await browser.close();
180 // Need to call exit() because the web server is still running.
181 process.exit(0);
182}
183
184driveBrowser();