bench.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function () {
  2. var BenchView = function (benchmark) {
  3. this.benchmark = benchmark
  4. var benchmarkTemplate = $('#benchmark-template').text()
  5. this.html = $(Mustache.to_html(benchmarkTemplate, benchmark))
  6. this.benchmark.on('start', this.started.bind(this))
  7. this.benchmark.on('complete', this.completed.bind(this))
  8. this.benchmark.on('error', this.errored.bind(this))
  9. this.html.find('button').bind('click', function () {
  10. benchmark.run({async: true})
  11. })
  12. }
  13. BenchView.prototype.started = function () {
  14. this.html
  15. .addClass('is-running')
  16. .find('.status')
  17. .html('Running…')
  18. }
  19. BenchView.prototype.completed = function () {
  20. this.html
  21. .removeClass('is-running')
  22. .find('.status')
  23. .text('Done')
  24. .end()
  25. .find('.ops-per-sec')
  26. .text(Benchmark.formatNumber(this.benchmark.hz.toFixed(2)))
  27. }
  28. BenchView.prototype.errored = function () {
  29. this.html
  30. .removeClass('is-running')
  31. .addClass('is-errored')
  32. .find('.status')
  33. .text('Error')
  34. throw(this.benchmark.error)
  35. }
  36. benchmarks = []
  37. var bench = function (name, testFn, options) {
  38. var benchmark = new Benchmark(name, testFn, options)
  39. benchmarks.push(benchmark)
  40. }
  41. bench.runAll = function () {
  42. for (var i = 1; i < benchmarks.length; i++) {
  43. var benchmark = benchmarks[i],
  44. prev = benchmarks[i - 1]
  45. prev.on('complete', function () {
  46. console.log(benchmark)
  47. benchmark.run({async: true})
  48. })
  49. }
  50. benchmarks[0].run({async: true})
  51. }
  52. $(document).ready(function () {
  53. var benchmarksContainer = $('#benchmarks tbody')
  54. benchmarks.forEach(function (benchmark) {
  55. var benchView = new BenchView (benchmark)
  56. benchmarksContainer.append(benchView.html)
  57. })
  58. $('button.run').click(function () {
  59. //bench.runAll()
  60. })
  61. })
  62. window.bench = bench
  63. })()