2 * Command line application to run Lottie-Web perf on a Lottie file in the
3 * browser and then exporting that 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 Lottie JSON file to process.'
21 typeLabel: '{underline file}',
22 description: 'The perf file to write. Defaults to perf.json',
26 description: 'Whether we should run in non-headless mode with GPU.',
31 description: 'The port number to use, defaults to 8081.',
35 name: 'lottie_player',
36 description: 'The path to lottie.min.js, defaults to a local npm install location.',
41 description: 'Which lottie-web backend to use. Options: canvas or svg.',
48 description: 'Print this usage guide.'
54 header: 'Lottie-Web Perf',
55 content: 'Command line application to run Lottie-Web perf.',
63 // Parse and validate flags.
64 const options = commandLineArgs(opts);
66 if (options.backend != 'canvas' && options.backend != 'svg') {
67 console.error('You must supply a lottie-web backend (canvas, svg).');
68 console.log(commandLineUsage(usage));
72 if (!options.output) {
73 options.output = 'perf.json';
78 if (!options.lottie_player) {
79 options.lottie_player = 'node_modules/lottie-web/build/player/lottie.min.js';
83 console.log(commandLineUsage(usage));
88 console.error('You must supply a Lottie JSON filename.');
89 console.log(commandLineUsage(usage));
93 // Start up a web server to serve the three files we need.
94 let lottieJS = fs.readFileSync(options.lottie_player, 'utf8');
95 let lottieJSON = fs.readFileSync(options.input, 'utf8');
97 if (options.backend == 'svg') {
98 console.log('Using lottie-web-perf.html');
99 driverHTML = fs.readFileSync('lottie-web-perf.html', 'utf8');
101 console.log('Using lottie-web-canvas-perf.html');
102 driverHTML = fs.readFileSync('lottie-web-canvas-perf.html', 'utf8');
105 // Find number of frames from the lottie JSON.
106 let lottieJSONContent = JSON.parse(lottieJSON);
107 const totalFrames = lottieJSONContent.op - lottieJSONContent.ip;
108 console.log('Total frames: ' + totalFrames);
110 const app = express();
111 app.get('/', (req, res) => res.send(driverHTML));
112 app.get('/res/lottie.js', (req, res) => res.send(lottieJS));
113 app.get('/res/lottie.json', (req, res) => res.send(lottieJSON));
114 app.listen(options.port, () => console.log('- Local web server started.'))
117 async function wait(ms) {
118 await new Promise(resolve => setTimeout(() => resolve(), ms));
122 const targetURL = "http://localhost:" + options.port + "/#" + totalFrames;
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);
151 console.log("Loading " + targetURL);
154 await page.tracing.start({
155 path: options.output,
157 categories: ["blink", "cc", "gpu"]
160 await page.goto(targetURL, {
162 waitUntil: 'networkidle0'
165 console.log('- Waiting 60s for run to be done.');
166 await page.waitForFunction('window._lottieWebDone === true', {
171 await page.tracing.stop();
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();
179 await browser.close();
180 // Need to call exit() because the web server is still running.