cli.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env node
  2. let defaults =
  3. {input: 'input.br', output: 'output.txt', test_iters: 0, test_repeat: 100};
  4. /* Parse command line arguments. */
  5. let argv =
  6. require('yargs')
  7. .usage('Usage: $0 -i file -o file')
  8. .option(
  9. 'input',
  10. {alias: 'i', default: defaults.input, describe: 'compressed file'})
  11. .option('output', {
  12. alias: 'o',
  13. default: defaults.output,
  14. describe: 'decompressed file'
  15. })
  16. .option('test_iters', {
  17. default: defaults.test_iters,
  18. describe: '# of times to run performance test'
  19. })
  20. .option('test_repeat', {
  21. default: defaults.test_repeat,
  22. describe: '# of times to decompress file in performance test'
  23. })
  24. .argv;
  25. /* Read input. */
  26. const fs = require('fs');
  27. data = fs.readFileSync(argv.input);
  28. if (!Buffer.isBuffer(data)) throw 'not a buffer';
  29. const bytes = new Uint8Array(data);
  30. /* Load and map brotli decoder module. */
  31. global.window = {};
  32. require('./decode.js')
  33. const brotliDecode = window['BrotliDecode'];
  34. /* Load "performance" module. */
  35. const {PerformanceObserver, performance} = require('perf_hooks');
  36. /* Performance test. */
  37. for (let i = 0; i < argv.test_iters; ++i) {
  38. const a = performance.now();
  39. let result;
  40. for (let j = 0; j < argv.test_repeat; ++j) {
  41. result = brotliDecode(bytes);
  42. }
  43. const b = performance.now();
  44. const total_length = argv.test_repeat * result.length / (1024 * 1024);
  45. const total_time = (b - a) / 1000;
  46. console.log(
  47. total_length + 'MB / ' + total_time +
  48. 's = ' + (total_length / total_time) + 'MB/s');
  49. }
  50. /* Decode and write output file. */
  51. fs.writeFileSync(argv.output, new Buffer(brotliDecode(bytes)));