|
| 1 | +window.benchmarkSteps = []; |
| 2 | + |
| 3 | +window.addEventListener('DOMContentLoaded', function() { |
| 4 | + var container = document.querySelector('#benchmarkContainer'); |
| 5 | + |
| 6 | + // Add links to everything |
| 7 | + var linkDiv = document.createElement('div'); |
| 8 | + linkDiv.style['margin-bottom'] = "1.5em"; |
| 9 | + var linkHtml = [ |
| 10 | + '<style>', |
| 11 | + '.bpLink { background: lightblue; padding: 1em }', |
| 12 | + '</style>', |
| 13 | + '<span class=bpLink>Benchmark Versions: </span>' |
| 14 | + ].join('\n'); |
| 15 | + |
| 16 | + [ |
| 17 | + // Add new benchmark suites here |
| 18 | + ['tree.html', 'TreeComponent'] |
| 19 | + ].forEach((function (link) { |
| 20 | + linkHtml += [ |
| 21 | + '<a class=bpLink href=', |
| 22 | + link[0], |
| 23 | + '>', |
| 24 | + link[1], |
| 25 | + '</a>' |
| 26 | + ].join(''); |
| 27 | + })); |
| 28 | + |
| 29 | + linkDiv.innerHTML = linkHtml; |
| 30 | + container.appendChild(linkDiv); |
| 31 | + |
| 32 | + |
| 33 | + // Benchmark runner |
| 34 | + var btn = document.createElement('button'); |
| 35 | + btn.innerText = "Loop"; |
| 36 | + var running = false; |
| 37 | + btn.addEventListener('click', loopBenchmark); |
| 38 | + |
| 39 | + container.appendChild(btn); |
| 40 | + |
| 41 | + function loopBenchmark() { |
| 42 | + if (running) { |
| 43 | + btn.innerText = "Loop"; |
| 44 | + running = false; |
| 45 | + } else { |
| 46 | + window.requestAnimationFrame(function() { |
| 47 | + btn.innerText = "Pause"; |
| 48 | + running = true; |
| 49 | + var loopB = function() { |
| 50 | + if (running) { |
| 51 | + window.requestAnimationFrame(function() { |
| 52 | + if (running) runBenchmarkSteps(loopB); |
| 53 | + }); |
| 54 | + } |
| 55 | + }; |
| 56 | + loopB(); |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + var onceBtn = document.createElement('button'); |
| 63 | + onceBtn.innerText = "Once"; |
| 64 | + onceBtn.addEventListener('click', function() { |
| 65 | + window.requestAnimationFrame(function() { |
| 66 | + onceBtn.innerText = "..."; |
| 67 | + window.requestAnimationFrame(function() { |
| 68 | + runBenchmarkSteps(function() { |
| 69 | + onceBtn.innerText = "Once"; |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + container.appendChild(onceBtn); |
| 75 | + |
| 76 | + var infoDiv = document.createElement('div'); |
| 77 | + infoDiv.style['font-family'] = 'monospace'; |
| 78 | + container.appendChild(infoDiv); |
| 79 | + |
| 80 | + |
| 81 | + var numMilliseconds; |
| 82 | + var performance = window.performance; |
| 83 | + if (performance != null && typeof performance.now == "function") { |
| 84 | + numMilliseconds = function numMillisecondsWPN() { |
| 85 | + return performance.now(); |
| 86 | + } |
| 87 | + } else if (performance != null && typeof performance.webkitNow == "function") { |
| 88 | + numMilliseconds = function numMillisecondsWebkit() { |
| 89 | + return performance.webkitNow(); |
| 90 | + } |
| 91 | + } else { |
| 92 | + console.log('using Date.now'); |
| 93 | + numMilliseconds = function numMillisecondsDateNow() { |
| 94 | + return Date.now(); |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + function runBenchmarkSteps(done) { |
| 99 | + // Run all the steps; |
| 100 | + var times = {}; |
| 101 | + window.benchmarkSteps.forEach(function(bs) { |
| 102 | + var startTime = numMilliseconds(); |
| 103 | + bs.fn(); |
| 104 | + times[bs.name] = numMilliseconds() - startTime; |
| 105 | + }); |
| 106 | + calcStats(times); |
| 107 | + |
| 108 | + done(); |
| 109 | + } |
| 110 | + |
| 111 | + var timesPerAction = {}; |
| 112 | + |
| 113 | + var NUM_SAMPLES = 10; |
| 114 | + function calcStats(times) { |
| 115 | + var iH = ''; |
| 116 | + window.benchmarkSteps.forEach(function(bs) { |
| 117 | + var tpa = timesPerAction[bs.name]; |
| 118 | + if (!tpa) { |
| 119 | + tpa = timesPerAction[bs.name] = { |
| 120 | + times: [], // circular buffer |
| 121 | + fmtTimes: [], |
| 122 | + nextEntry: 0 |
| 123 | + } |
| 124 | + } |
| 125 | + tpa.fmtTimes[tpa.nextEntry] = ('' + times[bs.name]).substr(0,6); |
| 126 | + tpa.times[tpa.nextEntry++] = times[bs.name]; |
| 127 | + tpa.nextEntry %= NUM_SAMPLES; |
| 128 | + var avg = 0; |
| 129 | + tpa.times.forEach(function(x) { avg += x; }); |
| 130 | + avg /= Math.min(NUM_SAMPLES, tpa.times.length); |
| 131 | + avg = ('' + avg).substr(0,6); |
| 132 | + iH += '<div>' + (' ' + bs.name).slice(-10).replace(/ /g, ' ') + ': avg-' + NUM_SAMPLES + ':<b>' + avg + 'ms</b> [' + tpa.fmtTimes.join(', ') + ']ms</div>'; |
| 133 | + }); |
| 134 | + infoDiv.innerHTML = iH; |
| 135 | + } |
| 136 | +}); |
0 commit comments