1
0

benchmark.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var BenchClass = function () {
  2. this.suites = [];
  3. this.THREE = window.THREE;
  4. window.THREE = undefined;
  5. Benchmark.options.maxTime = 1.0;
  6. return this;
  7. };
  8. BenchClass.prototype.isTHREELoaded = function () {
  9. return _.isObject( this.THREE ); // eslint-disable-line no-undef
  10. };
  11. BenchClass.prototype.newSuite = function ( name ) {
  12. var s = new Benchmark.Suite( name );
  13. this.suites.push( s );
  14. return s;
  15. };
  16. BenchClass.prototype.display = function () {
  17. for ( var x of this.suites ) {
  18. var s = new SuiteUI( x );
  19. s.render();
  20. }
  21. };
  22. BenchClass.prototype.warning = function ( message ) {
  23. console.error( message );
  24. };
  25. var SuiteUI = function ( suite ) {
  26. this.suite = suite;
  27. this.isRunning = false;
  28. return this;
  29. };
  30. SuiteUI.prototype.render = function () {
  31. var n = document.importNode( this.suiteTemplate, true );
  32. this.elem = n.querySelector( 'article' );
  33. this.results = n.querySelector( '.results' );
  34. this.title = n.querySelector( 'h2' );
  35. this.runButton = n.querySelector( 'h3' );
  36. this.title.innerText = this.suite.name;
  37. this.runButton.onclick = this.run.bind( this );
  38. this.section.appendChild( n );
  39. };
  40. SuiteUI.prototype.run = function () {
  41. this.runButton.click = _.noop; // eslint-disable-line no-undef
  42. this.runButton.innerText = 'Running...';
  43. this.suite.on( 'complete', this.complete.bind( this ) );
  44. this.suite.run( {
  45. async: true
  46. } );
  47. };
  48. SuiteUI.prototype.complete = function () {
  49. this.runButton.style.display = 'none';
  50. this.results.style.display = 'block';
  51. var f = _.orderBy( this.suite, [ 'hz' ], [ 'desc' ] ); // eslint-disable-line no-undef
  52. for ( var i = 0; i < f.length; i ++ ) {
  53. var x = f[ i ];
  54. var n = document.importNode( this.suiteTestTemplate, true );
  55. n.querySelector( '.name' ).innerText = x.name;
  56. n.querySelector( '.ops' ).innerText = x.hz.toFixed();
  57. n.querySelector( '.desv' ).innerText = x.stats.rme.toFixed( 2 );
  58. this.results.appendChild( n );
  59. }
  60. };
  61. var Bench = new BenchClass();
  62. window.addEventListener( 'load', function () {
  63. SuiteUI.prototype.suiteTemplate = document.querySelector( '#suite' ).content;
  64. SuiteUI.prototype.suiteTestTemplate = document.querySelector( '#suite-test' ).content;
  65. SuiteUI.prototype.section = document.querySelector( 'section' );
  66. Bench.display();
  67. } );