blob: a669bbc1ed238c1f766307b973f4dd87e6748973 [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 {
25 name: 'port',
26 description: 'The port number to use, defaults to 8081.',
27 type: Number,
28 },
29 {
30 name: 'lottie_player',
31 description: 'The path to lottie.min.js, defaults to a local npm install location.',
32 type: String,
33 },
34 {
35 name: 'help',
36 alias: 'h',
37 type: Boolean,
38 description: 'Print this usage guide.'
39 },
40];
41
42const usage = [
43 {
44 header: 'Lottie-Web Perf',
45 content: 'Command line application to run Lottie-Web perf.',
46 },
47 {
48 header: 'Options',
49 optionList: opts,
50 },
51];
52
53// Parse and validate flags.
54const options = commandLineArgs(opts);
55
56if (!options.output) {
57 options.output = 'perf.json';
58}
59if (!options.port) {
60 options.port = 8081;
61}
Ravi Mistry0233b1e2019-06-19 14:47:59 -040062if (!options.lottie_player) {
63 options.lottie_player = 'node_modules/lottie-web/build/player/lottie.min.js';
64}
Ravi Mistry6ac37952019-06-19 11:26:00 -040065
66if (options.help) {
67 console.log(commandLineUsage(usage));
68 process.exit(0);
69}
70
71if (!options.input) {
72 console.error('You must supply a Lottie JSON filename.');
73 console.log(commandLineUsage(usage));
74 process.exit(1);
75}
76
77// Start up a web server to serve the three files we need.
78let lottieJS = fs.readFileSync(options.lottie_player, 'utf8');
79let driverHTML = fs.readFileSync('lottie-web-perf.html', 'utf8');
80let lottieJSON = fs.readFileSync(options.input, 'utf8');
81
82const app = express();
83app.get('/', (req, res) => res.send(driverHTML));
84app.get('/res/lottie.js', (req, res) => res.send(lottieJS));
85app.get('/res/lottie.json', (req, res) => res.send(lottieJSON));
86app.listen(options.port, () => console.log('- Local web server started.'))
87
88// Utility function.
89async function wait(ms) {
90 await new Promise(resolve => setTimeout(() => resolve(), ms));
91 return ms;
92}
93
94const targetURL = "http://localhost:" + options.port + "/";
95
96// Drive chrome to load the web page from the server we have running.
97async function driveBrowser() {
Ravi Mistryb2ca0062019-06-20 15:56:24 +000098 console.log('- Launching chrome for ' + options.input);
Ravi Mistry83262392019-06-26 13:41:24 +000099 let browser;
100 let page;
101 try {
102 browser = await puppeteer.launch(
103 {headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox']});
104 page = await browser.newPage();
105 } catch (e) {
106 console.log('Could not open the browser.', e);
107 process.exit(1);
108 }
109
Ravi Mistry6ac37952019-06-19 11:26:00 -0400110 console.log("Loading " + targetURL);
111 try {
112 await page.goto(targetURL, {
113 timeout: 20000,
114 waitUntil: 'networkidle0'
115 });
116 console.log('- Waiting 20s for run to be done.');
117 await page.waitForFunction('window._lottieWebDone === true', {
118 timeout: 20000,
119 });
120 } catch(e) {
121 console.log('Timed out while loading or drawing. Either the JSON file was ' +
122 'too big or hit a bug in the player.', e);
123 await browser.close();
124 process.exit(0);
125 }
126
127 // Write results.
128 var extractResults = function() {
129 return {
130 'frame_avg_us': window._avgFrameTimeUs,
131 'frame_max_us': window._maxFrameTimeUs,
132 'frame_min_us': window._minFrameTimeUs,
133 };
134 }
135 var data = await page.evaluate(extractResults);
136 console.log(data)
137 fs.writeFileSync(options.output, JSON.stringify(data), 'utf-8');
138
139 await browser.close();
140 // Need to call exit() because the web server is still running.
141 process.exit(0);
142}
143
144driveBrowser();