perf-test.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* eslint-disable no-console */
  2. /**
  3. * Performance Tests
  4. *
  5. * This file measures the performance of renderToString using a number of strings
  6. * from ss_data.yaml.
  7. *
  8. * TODO:
  9. * - allow users to specify a different key or keys from ss_data.yaml
  10. * - allow users to specify a different string or strings
  11. * - provide a way to test the performance against different branches
  12. */
  13. const Benchmark = require('benchmark');
  14. const yaml = require('js-yaml');
  15. const fs = require('fs');
  16. const path = require('path');
  17. const filename = path.resolve(__dirname, 'screenshotter/ss_data.yaml');
  18. const data = yaml.load(fs.readFileSync(filename, 'utf-8'));
  19. console.log('compiling katex...');
  20. require('@babel/register');
  21. const katex = require('../katex').default;
  22. console.log('');
  23. // Benchmark is a performancing testing library. It allows you to define a
  24. // suite of tests. After adding tests to the suite with the .add() method they
  25. // can be run by calling the .run() method. See https://benchmarkjs.com.
  26. const suite = new Benchmark.Suite;
  27. const testsToRun = [
  28. "AccentsText",
  29. "ArrayMode",
  30. "GroupMacros",
  31. "MathBb",
  32. "SqrtRoot",
  33. "StretchyAccent",
  34. "Units",
  35. ];
  36. for (const key of testsToRun) {
  37. const value = data[key];
  38. if (typeof value === "string") {
  39. suite.add(key, () => katex.renderToString(value));
  40. } else {
  41. const options = {
  42. macros: value.macros,
  43. };
  44. suite.add(key, () => katex.renderToString(value.tex, options));
  45. }
  46. }
  47. // Print out the ops/sec for each test
  48. suite.on('cycle', function(event) {
  49. console.log(String(event.target));
  50. });
  51. const config = {};
  52. suite.run(config);