Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit a98663d

Browse files
committed
feat(benchmark): add ability to adjust sample quantity
Move creation of HTML elements into tree.html where possible. Improve styling with bootstrap lib plus basic structuring of content.
1 parent d1e745e commit a98663d

File tree

4 files changed

+198
-121
lines changed

4 files changed

+198
-121
lines changed

benchmark/web/bootstrap.min.css

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmark/web/bp.js

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
var bp = window.bp = {};
22
bp.steps = window.benchmarkSteps = [];
3-
bp.runState = {};
3+
bp.runState = {
4+
numSamples: 10,
5+
recentTimePerStep: {},
6+
timesPerAction: {}
7+
};
48

5-
bp.setRunState = function (samples, iterations, ignoreCount) {
6-
bp.runState.numSamples = samples;
9+
bp.setIterations = function (iterations) {
710
bp.runState.iterations = iterations;
8-
bp.runState.ignoreCount = ignoreCount;
9-
bp.runState.recentTimePerStep = {};
10-
bp.runState.timesPerAction = {};
1111
};
1212

13-
bp.resetRunState = function() {
14-
bp.runState = {
15-
numSamples: 0,
16-
iterations: 0,
17-
ignoreCount: 0,
18-
recentTimePerStep: {},
19-
timesPerAction: {}
20-
};
13+
bp.resetIterations = function() {
14+
bp.runState.iterations = 0;
2115
};
2216

2317
bp.interpolateHtml = function(string, list) {
@@ -42,17 +36,17 @@ bp.numMilliseconds = function() {
4236
bp.loopBenchmark = function () {
4337
if (bp.runState.iterations <= -1) {
4438
//Time to stop looping
45-
bp.setRunState(10, 0);
39+
bp.setIterations(0);
4640
bp.loopBtn.innerText = 'Loop';
4741
return;
4842
}
49-
bp.setRunState(10, -1);
43+
bp.setIterations(-1);
5044
bp.loopBtn.innerText = 'Pause';
5145
bp.runAllTests();
5246
};
5347

5448
bp.onceBenchmark = function() {
55-
bp.setRunState(10, 1);
49+
bp.setIterations(1);
5650
bp.onceBtn.innerText = '...';
5751
bp.runAllTests(function() {
5852
bp.onceBtn.innerText = 'Once';
@@ -61,13 +55,27 @@ bp.onceBenchmark = function() {
6155

6256
bp.twentyFiveBenchmark = function() {
6357
var twentyFiveBtn = bp.twentyFiveBtn;
64-
bp.setRunState(20, 25);
58+
bp.setIterations(25);
6559
twentyFiveBtn.innerText = 'Looping...';
6660
bp.runAllTests(function() {
6761
twentyFiveBtn.innerText = 'Loop 25x';
6862
}, 5);
6963
};
7064

65+
bp.addSampleRange = function() {
66+
bp.sampleRange = bp.container().querySelector('#sampleRange');
67+
bp.sampleRange.value = Math.max(bp.runState.numSamples, 1);
68+
bp.sampleRange.addEventListener('input', bp.onSampleRangeChanged);
69+
bp.sampleRangeValue = bp.container().querySelector('#sampleRangeValue');
70+
bp.sampleRangeValue.innerText = bp.runState.numSamples;
71+
};
72+
73+
bp.onSampleRangeChanged = function (evt) {
74+
var value = evt.target.value;
75+
bp.runState.numSamples = parseInt(value, 10);
76+
bp.sampleRangeValue.innerText = value;
77+
};
78+
7179
bp.runTimedTest = function (bs) {
7280
if (typeof window.gc === 'function') {
7381
window.gc();
@@ -90,31 +98,23 @@ bp.runAllTests = function (done) {
9098
}
9199
else {
92100
bp.writeReport(bp.report);
93-
bp.resetRunState();
101+
bp.resetIterations();
94102
done && done();
95103
}
96104
}
97105

98-
bp.padName = function (name) {
99-
return name.length < 10 ?
100-
(' ' + name).slice(-10).replace(/ /g, '&nbsp;') :
101-
name;
102-
};
103-
104106
bp.generateReportPartial = function(name, avg, times) {
105107
return bp.interpolateHtml(
106-
'<div>%0: avg-%1:<b>%2ms</b> [%3]ms</div>',
108+
'<tr><td>%0</td><td class="average">%1ms</td><td>[%2]ms</td></tr>',
107109
[
108-
bp.padName(name),
109-
bp.runState.numSamples,
110+
name,
110111
('' + avg).substr(0,6),
111112
times.join(', ')
112113
]);
113114
};
114115

115116
bp.getAverage = function (times, runState) {
116-
var avg = 0, ignoreCount = (runState && runState.ignoreCount) || 0;
117-
times = times.slice(ignoreCount);
117+
var avg = 0;
118118
times.forEach(function(x) { avg += x; });
119119
return avg / times.length;
120120
};
@@ -126,7 +126,7 @@ bp.writeReport = function(reportContent) {
126126
bp.getTimesPerAction = function(name) {
127127
var tpa = bp.runState.timesPerAction[name];
128128
if (!tpa) {
129-
tpa = bp.runState.timesPerAction[name] = {
129+
tpa = bp.runState.timesPerAction[name] = {
130130
times: [], // circular buffer
131131
fmtTimes: [],
132132
nextEntry: 0
@@ -135,15 +135,27 @@ bp.getTimesPerAction = function(name) {
135135
return tpa;
136136
};
137137

138+
bp.rightSizeTimes = function(times) {
139+
var delta = times.length - bp.runState.numSamples;
140+
if (delta > 0) {
141+
return times.slice(delta);
142+
}
143+
144+
return times;
145+
};
146+
138147
bp.calcStats = function() {
139148
var report = '';
140149
bp.steps.forEach(function(bs) {
141150
var stepName = bs.name,
142151
timeForStep = bp.runState.recentTimePerStep[stepName],
143152
tpa = bp.getTimesPerAction(stepName),
144153
avg;
154+
145155
tpa.fmtTimes[tpa.nextEntry] = ('' + timeForStep).substr(0,6);
156+
tpa.fmtTimes = bp.rightSizeTimes(tpa.fmtTimes);
146157
tpa.times[tpa.nextEntry++] = timeForStep;
158+
tpa.times = bp.rightSizeTimes(tpa.times);
147159
tpa.nextEntry %= bp.runState.numSamples;
148160
avg = bp.getAverage(tpa.times, bp.runState);
149161

@@ -159,49 +171,37 @@ bp.container = function() {
159171
return bp._container;
160172
}
161173

162-
bp.addButton = function(reference, text, handler) {
174+
bp.addButton = function(reference, handler) {
163175
var container = bp.container();
164-
bp[reference] = document.createElement('button');
165-
bp[reference].innerText = text;
166-
176+
bp[reference] = container.querySelector('button.' + reference);
167177
bp[reference].addEventListener('click', handler);
168-
169-
container.appendChild(bp[reference]);
170178
}
171179

172180
bp.addLinks = function() {
173181
// Add links to everything
174-
var linkDiv = document.createElement('div');
175-
linkDiv.style['margin-bottom'] = '1.5em';
176-
var linkHtml = [
177-
'<style>',
178-
'.bpLink { background: lightblue; padding: 1em }',
179-
'</style>',
180-
'<span class=bpLink>Benchmark Versions: </span>'
181-
].join('\n');
182+
var linkDiv = bp.container().querySelector('.versionContent');
183+
var linkHtml = '';
182184

183185
[
184186
// Add new benchmark suites here
185187
['tree.html', 'TreeComponent']
186188
].forEach((function (link) {
187-
linkHtml += bp.interpolateHtml('<a class=bpLink href=%0>%1</a>', link);
189+
linkHtml += bp.interpolateHtml('<a href=%0>%1</a>', link);
188190
}));
189191

190192
linkDiv.innerHTML = linkHtml;
191-
bp.container().appendChild(linkDiv);
192193
};
193194

194195
bp.addInfo = function() {
195-
bp.infoDiv = document.createElement('div');
196-
bp.infoDiv.style['font-family'] = 'monospace';
197-
bp.container().appendChild(bp.infoDiv);
196+
bp.infoDiv = bp.container().querySelector('tbody.info');
198197
};
199198

200199
bp.onDOMContentLoaded = function() {
201200
bp.addLinks();
202-
bp.addButton('loopBtn', 'Loop', bp.loopBenchmark);
203-
bp.addButton('onceBtn', 'Once', bp.onceBenchmark);
204-
bp.addButton('twentyFiveBtn', 'Loop 25x', bp.twentyFiveBenchmark);
201+
bp.addButton('loopBtn', bp.loopBenchmark);
202+
bp.addButton('onceBtn', bp.onceBenchmark);
203+
bp.addButton('twentyFiveBtn', bp.twentyFiveBenchmark);
204+
bp.addSampleRange();
205205
bp.addInfo();
206206
};
207207

0 commit comments

Comments
 (0)