skottie-wasm-perf.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Command line application to run Skottie-WASM perf on a Lottie file in the
  3. * browser and then exporting the result.
  4. *
  5. */
  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');
  12. const opts = [
  13. {
  14. name: 'canvaskit_js',
  15. typeLabel: '{underline file}',
  16. description: 'The path to canvaskit.js.'
  17. },
  18. {
  19. name: 'canvaskit_wasm',
  20. typeLabel: '{underline file}',
  21. description: 'The path to canvaskit.wasm.'
  22. },
  23. {
  24. name: 'input',
  25. typeLabel: '{underline file}',
  26. description: 'The Lottie JSON file to process.'
  27. },
  28. {
  29. name: 'output',
  30. typeLabel: '{underline file}',
  31. description: 'The perf file to write. Defaults to perf.json',
  32. },
  33. {
  34. name: 'use_gpu',
  35. description: 'Whether we should run in non-headless mode with GPU.',
  36. type: Boolean,
  37. },
  38. {
  39. name: 'port',
  40. description: 'The port number to use, defaults to 8081.',
  41. type: Number,
  42. },
  43. {
  44. name: 'help',
  45. alias: 'h',
  46. type: Boolean,
  47. description: 'Print this usage guide.'
  48. },
  49. ];
  50. const usage = [
  51. {
  52. header: 'Skottie WASM Perf',
  53. content: "Command line application to run Skottie-WASM perf."
  54. },
  55. {
  56. header: 'Options',
  57. optionList: opts,
  58. },
  59. ];
  60. // Parse and validate flags.
  61. const options = commandLineArgs(opts);
  62. if (!options.output) {
  63. options.output = 'perf.json';
  64. }
  65. if (!options.port) {
  66. options.port = 8081;
  67. }
  68. if (options.help) {
  69. console.log(commandLineUsage(usage));
  70. process.exit(0);
  71. }
  72. if (!options.canvaskit_js) {
  73. console.error('You must supply path to canvaskit.js.');
  74. console.log(commandLineUsage(usage));
  75. process.exit(1);
  76. }
  77. if (!options.canvaskit_wasm) {
  78. console.error('You must supply path to canvaskit.wasm.');
  79. console.log(commandLineUsage(usage));
  80. process.exit(1);
  81. }
  82. if (!options.input) {
  83. console.error('You must supply a Lottie JSON filename.');
  84. console.log(commandLineUsage(usage));
  85. process.exit(1);
  86. }
  87. // Start up a web server to serve the three files we need.
  88. let canvasKitJS = fs.readFileSync(options.canvaskit_js, 'utf8');
  89. let canvasKitWASM = fs.readFileSync(options.canvaskit_wasm, 'binary');
  90. let driverHTML = fs.readFileSync('skottie-wasm-perf.html', 'utf8');
  91. let lottieJSON = fs.readFileSync(options.input, 'utf8');
  92. const app = express();
  93. app.get('/', (req, res) => res.send(driverHTML));
  94. app.get('/res/canvaskit.wasm', function(req, res) {
  95. res.type('application/wasm');
  96. res.send(new Buffer(canvasKitWASM, 'binary'));
  97. });
  98. app.get('/res/canvaskit.js', (req, res) => res.send(canvasKitJS));
  99. app.get('/res/lottie.json', (req, res) => res.send(lottieJSON));
  100. app.listen(options.port, () => console.log('- Local web server started.'))
  101. // Utility function.
  102. async function wait(ms) {
  103. await new Promise(resolve => setTimeout(() => resolve(), ms));
  104. return ms;
  105. }
  106. const targetURL = "http://localhost:" + options.port + "/";
  107. const viewPort = {width: 1000, height: 1000};
  108. // Drive chrome to load the web page from the server we have running.
  109. async function driveBrowser() {
  110. console.log('- Launching chrome for ' + options.input);
  111. let browser;
  112. let page;
  113. const headless = !options.use_gpu;
  114. let browser_args = [
  115. '--no-sandbox',
  116. '--disable-setuid-sandbox',
  117. '--window-size=' + viewPort.width + ',' + viewPort.height,
  118. ];
  119. if (options.use_gpu) {
  120. browser_args.push('--ignore-gpu-blacklist');
  121. browser_args.push('--enable-gpu-rasterization');
  122. }
  123. console.log("Running with headless: " + headless + " args: " + browser_args);
  124. try {
  125. browser = await puppeteer.launch({headless: headless, args: browser_args});
  126. page = await browser.newPage();
  127. await page.setViewport(viewPort);
  128. } catch (e) {
  129. console.log('Could not open the browser.', e);
  130. process.exit(1);
  131. }
  132. console.log("Loading " + targetURL);
  133. try {
  134. // Start trace.
  135. await page.tracing.start({
  136. path: options.output,
  137. screenshots: false,
  138. categories: ["blink", "cc", "gpu"]
  139. });
  140. await page.goto(targetURL, {
  141. timeout: 60000,
  142. waitUntil: 'networkidle0'
  143. });
  144. console.log('Waiting 60s for run to be done');
  145. await page.waitForFunction('window._skottieDone === true', {
  146. timeout: 60000,
  147. });
  148. // Stop Trace.
  149. await page.tracing.stop();
  150. } catch(e) {
  151. console.log('Timed out while loading or drawing. Either the JSON file was ' +
  152. 'too big or hit a bug.', e);
  153. await browser.close();
  154. process.exit(1);
  155. }
  156. await browser.close();
  157. // Need to call exit() because the web server is still running.
  158. process.exit(0);
  159. }
  160. driveBrowser();