benchmark.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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);
  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 (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;
  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"]);
  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. })