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

Commit 801c0d7

Browse files
jeffbcrossjbdeboer
authored andcommitted
refactor(benchmark): reduce complexity, and move formatting of values into template
1 parent 447b8a6 commit 801c0d7

File tree

3 files changed

+91
-169
lines changed

3 files changed

+91
-169
lines changed

benchmark/web/bp.js

Lines changed: 39 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ var bp = window.bp = {
88
runState: {
99
iterations: 0,
1010
numSamples: 20,
11-
recentTimePerStep: {},
12-
recentGCTimePerStep: {},
13-
recentGarbagePerStep: {},
14-
recentRetainedMemoryPerStep: {}
11+
recentResult: {}
1512
}
1613
},
1714
Document: {},
@@ -106,10 +103,7 @@ bp.Runner.runAllTests = function (done) {
106103
if (bp.Runner.runState.iterations--) {
107104
bp.steps.forEach(function(bs) {
108105
var testResults = bp.Runner.runTimedTest(bs);
109-
bp.Runner.runState.recentTimePerStep[bs.name] = testResults.time;
110-
bp.Runner.runState.recentGCTimePerStep[bs.name] = testResults.gcTime;
111-
bp.Runner.runState.recentGarbagePerStep[bs.name] = testResults.garbage;
112-
bp.Runner.runState.recentRetainedMemoryPerStep[bs.name] = testResults.retainedDelta;
106+
bp.Runner.runState.recentResult[bs.name] = testResults;
113107
});
114108
bp.Report.markup = bp.Report.calcStats();
115109
bp.Document.writeReport(bp.Report.markup);
@@ -129,7 +123,7 @@ bp.Runner.runTimedTest = function (bs) {
129123
endTime,
130124
startGCTime,
131125
endGCTime,
132-
retainedDelta,
126+
retainedMemory,
133127
garbage,
134128
beforeHeap,
135129
afterHeap,
@@ -152,34 +146,18 @@ bp.Runner.runTimedTest = function (bs) {
152146

153147
finalHeap = performance.memory.usedJSHeapSize;
154148
garbage = Math.abs(finalHeap - afterHeap);
155-
retainedDelta = finalHeap - beforeHeap;
149+
retainedMemory = finalHeap - beforeHeap;
156150
return {
157-
time: endTime,
151+
testTime: endTime,
158152
gcTime: endGCTime,
159153
beforeHeap: beforeHeap,
160154
garbage: garbage,
161-
retainedDelta: retainedDelta
155+
retainedMemory: retainedMemory
162156
};
163157
};
164158

165-
bp.Report.generateReportModel = function (rawModel) {
166-
rawModel.avg = {
167-
time: ('' + rawModel.avg.time).substr(0,6),
168-
gcTime: ('' + rawModel.avg.gcTime).substr(0,6),
169-
garbage: ('' + rawModel.avg.garbage).substr(0,6),
170-
retained: ('' + rawModel.avg.retained).substr(0,6),
171-
combinedTime: ('' + (rawModel.avg.time + rawModel.avg.gcTime)).substr(0,6)
172-
};
173-
rawModel.times = rawModel.times.join('<br>'),
174-
rawModel.gcTimes = rawModel.gcTimes.join('<br>'),
175-
rawModel.garbageTimes = rawModel.garbageTimes.join('<br>'),
176-
rawModel.retainedTimes = rawModel.retainedTimes.join('<br>')
177-
rawModel.timesConfidenceInterval = (rawModel.timesConfidenceInterval || 0).toFixed(2);
178-
return rawModel;
179-
};
180-
181159
bp.Report.generatePartial = function(model) {
182-
return bp.infoTemplate(model);
160+
return bp.Document.infoTemplate(model);
183161
};
184162

185163
bp.Document.writeReport = function(reportContent) {
@@ -190,14 +168,10 @@ bp.Report.getTimesPerAction = function(name) {
190168
var tpa = bp.Report.timesPerAction[name];
191169
if (!tpa) {
192170
tpa = bp.Report.timesPerAction[name] = {
193-
times: [], // circular buffer
194-
fmtTimes: [],
171+
testTimes: [], // circular buffer
195172
gcTimes: [],
196-
fmtGcTimes: [],
197-
garbageTimes: [],
198-
fmtGarbageTimes: [],
199-
retainedTimes: [],
200-
fmtRetainedTimes: [],
173+
garbageCount: [],
174+
retainedCount: [],
201175
nextEntry: 0
202176
}
203177
}
@@ -214,58 +188,42 @@ bp.Report.rightSizeTimes = function(times) {
214188
};
215189

216190
bp.Report.updateTimes = function(tpa, index, reference, recentTime) {
217-
var fmtKey = 'fmt' + reference.charAt(0).toUpperCase() + reference.slice(1);
218191
tpa[reference][index] = recentTime;
219192
tpa[reference] = bp.Report.rightSizeTimes(tpa[reference]);
220-
tpa[fmtKey][index] = recentTime.toString().substr(0, 6);
221-
tpa[fmtKey] = bp.Report.rightSizeTimes(tpa[fmtKey]);
222193
};
223194

224195
bp.Report.calcStats = function() {
225196
var report = '';
226197
bp.steps.forEach(function(bs) {
227-
var stepName = bs.name,
228-
timeForStep = bp.Runner.runState.recentTimePerStep[stepName],
229-
gcTimeForStep = bp.Runner.runState.recentGCTimePerStep[stepName],
230-
garbageTimeForStep = bp.Runner.runState.recentGarbagePerStep[stepName],
231-
retainedTimeForStep = bp.Runner.runState.recentRetainedMemoryPerStep[stepName],
232-
tpa = bp.Report.getTimesPerAction(stepName),
233-
reportModel,
234-
avg,
235-
timesConfidenceInterval,
236-
timesStandardDeviation;
237-
238-
bp.Report.updateTimes(tpa, tpa.nextEntry, 'gcTimes', gcTimeForStep);
239-
bp.Report.updateTimes(tpa, tpa.nextEntry, 'garbageTimes', garbageTimeForStep / 1e3);
240-
bp.Report.updateTimes(tpa, tpa.nextEntry, 'retainedTimes', retainedTimeForStep / 1e3);
241-
bp.Report.updateTimes(tpa, tpa.nextEntry, 'times', timeForStep);
198+
var recentResult = bp.Runner.runState.recentResult[bs.name],
199+
tpa = bp.Report.getTimesPerAction(bs.name);
200+
201+
bp.Report.updateTimes(tpa, tpa.nextEntry, 'gcTimes', recentResult.gcTime);
202+
bp.Report.updateTimes(tpa, tpa.nextEntry, 'garbageCount', recentResult.garbage / 1e3);
203+
bp.Report.updateTimes(tpa, tpa.nextEntry, 'retainedCount', recentResult.retainedMemory / 1e3);
204+
bp.Report.updateTimes(tpa, tpa.nextEntry, 'testTimes', recentResult.testTime);
242205

243206
tpa.nextEntry++;
244207
tpa.nextEntry %= bp.Runner.runState.numSamples;
245-
avg = {
246-
gcTime: bp.Statistics.getMean(tpa.gcTimes),
247-
time: bp.Statistics.getMean(tpa.times),
248-
garbage: bp.Statistics.getMean(tpa.garbageTimes),
249-
retained: bp.Statistics.getMean(tpa.retainedTimes)
208+
209+
var meanTestTime = bp.Statistics.getMean(tpa.testTimes);
210+
var testTimesStdDev = bp.Statistics.calculateStandardDeviation(tpa.testTimes, meanTestTime);
211+
var avgGCTime = bp.Statistics.getMean(tpa.gcTimes);
212+
var avg = {
213+
gcTime: avgGCTime,
214+
testTime: meanTestTime,
215+
combinedTime: meanTestTime + avgGCTime,
216+
garbage: bp.Statistics.getMean(tpa.garbageCount),
217+
retained: bp.Statistics.getMean(tpa.retainedCount),
218+
testTimesStdDev: testTimesStdDev,
219+
coefficientOfVariation: bp.Statistics.calculateCoefficientOfVariation(testTimesStdDev, meanTestTime)
250220
};
251221

252-
timesStandardDeviation = bp.Statistics.calculateStandardDeviation(tpa.times, avg.time);
253-
timesConfidenceInterval = bp.Statistics.calculateConfidenceInterval(
254-
timesStandardDeviation,
255-
tpa.times.length
256-
);
257-
258-
reportModel = bp.Report.generateReportModel({
259-
name: stepName,
260-
avg: avg,
261-
times: tpa.fmtTimes,
262-
timesStandardDeviation: timesStandardDeviation,
263-
coefficientOfVariation: bp.Statistics.calculateCoefficientOfVariation(timesStandardDeviation, avg.time),
264-
timesRelativeMarginOfError: bp.Statistics.calculateRelativeMarginOfError(timesConfidenceInterval, avg.time),
265-
gcTimes: tpa.fmtGcTimes,
266-
garbageTimes: tpa.fmtGarbageTimes,
267-
retainedTimes: tpa.fmtRetainedTimes
268-
});
222+
var reportModel = _.clone(tpa);
223+
reportModel.name = bs.name;
224+
reportModel.avg = avg;
225+
reportModel.testTimes = tpa.testTimes;
226+
269227
report += bp.Report.generatePartial(reportModel);
270228
});
271229
return report;
@@ -291,10 +249,10 @@ bp.Document.onSampleInputChanged = function (evt) {
291249
};
292250

293251
bp.Document.container = function() {
294-
if (!bp._container) {
295-
bp._container = document.querySelector('#benchmarkContainer');
252+
if (!bp.Document._container) {
253+
bp.Document._container = document.querySelector('#benchmarkContainer');
296254
}
297-
return bp._container;
255+
return bp.Document._container;
298256
}
299257

300258
bp.Document.addButton = function(reference, handler) {
@@ -325,11 +283,12 @@ bp.Document.addLinks = function() {
325283
bp.Document.addInfo = function() {
326284
bp.Document.infoDiv = bp.Document.container().querySelector('tbody.info');
327285
if (bp.Document.infoDiv) {
328-
bp.infoTemplate = _.template(bp.Document.container().querySelector('#infoTemplate').innerHTML);
286+
bp.Document.infoTemplate = _.template(bp.Document.container().querySelector('#infoTemplate').innerHTML);
329287
}
330288
};
331289

332290
bp.Document.onDOMContentLoaded = function() {
291+
if (!bp.Document.container()) return;
333292
bp.Document.addLinks();
334293
bp.Document.addButton('loopBtn', bp.Runner.loopBenchmark);
335294
bp.Document.addButton('onceBtn', bp.Runner.onceBenchmark);

benchmark/web/bp.spec.js

Lines changed: 29 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ describe('bp', function() {
1111
};
1212

1313
beforeEach(function() {
14-
bp._container = document.createElement('div');
14+
bp.Document._container = document.createElement('div');
15+
bp.Document.infoTemplate = function(model) {
16+
return JSON.stringify(model);
17+
}
1518
bp.Runner.runState = {
1619
iterations: 0,
1720
numSamples: 20,
18-
recentTimePerStep: {},
19-
recentGCTimePerStep: {},
20-
recentGarbagePerStep: {},
21-
recentRetainedMemoryPerStep: {}
21+
recentResult: {}
2222
};
2323

2424
bp.Report.timesPerAction = {};
@@ -67,17 +67,8 @@ describe('bp', function() {
6767

6868
describe('.Document', function() {
6969
describe('.container()', function() {
70-
it('should return bp._container if set', function() {
71-
bp._container = 'fooelement';
72-
expect(bp.Document.container()).toBe('fooelement');
73-
});
74-
75-
76-
it('should query the document for #benchmarkContainer if no _container', function() {
77-
var spy = spyOn(document, 'querySelector');
78-
bp._container = null;
79-
bp.Document.container();
80-
expect(spy).toHaveBeenCalled();
70+
it('should return bp.Document._container if set', function() {
71+
expect(bp.Document.container() instanceof HTMLElement).toBe(true);
8172
});
8273
});
8374

@@ -138,21 +129,23 @@ describe('bp', function() {
138129
bp.Runner.runState = {
139130
numSamples: 99,
140131
iterations: 100,
141-
recentTimePerStep: {
142-
fakeStep: 2
132+
recentResult: {
133+
fakeStep: {
134+
testTime: 2
135+
}
143136
}
144137
}
145138
bp.Report.timesPerAction = {
146139
fakeStep: {
147-
times: [5]
140+
testTimes: [5]
148141
}
149142
};
150143

151144
bp.Runner.resetIterations();
152145
expect(bp.Runner.runState.numSamples).toBe(99);
153146
expect(bp.Runner.runState.iterations).toBe(0);
154-
expect(bp.Report.timesPerAction).toEqual({fakeStep: {times: [5]}});
155-
expect(bp.Runner.runState.recentTimePerStep).toEqual({fakeStep: 2});
147+
expect(bp.Report.timesPerAction).toEqual({fakeStep: {testTimes: [5]}});
148+
expect(bp.Runner.runState.recentResult['fakeStep'].testTime).toEqual(2);
156149
});
157150
});
158151

@@ -168,7 +161,7 @@ describe('bp', function() {
168161

169162
it('should return the time required to run the test', function() {
170163
var times = {};
171-
expect(typeof bp.Runner.runTimedTest(mockStep, times).time).toBe('number');
164+
expect(typeof bp.Runner.runTimedTest(mockStep, times).testTime).toBe('number');
172165
});
173166
});
174167

@@ -221,7 +214,7 @@ describe('bp', function() {
221214
}, 'done to be called', 200);
222215

223216
runs(function() {
224-
expect(bp.Report.timesPerAction.fakeStep.times.length).toBe(8);
217+
expect(bp.Report.timesPerAction.fakeStep.testTimes.length).toBe(8);
225218
});
226219
});
227220
});
@@ -374,28 +367,20 @@ describe('bp', function() {
374367
bp.Runner.runState = {
375368
numSamples: 5,
376369
iterations: 5,
377-
recentTimePerStep: {
378-
fakeStep: 5
379-
},
380-
recentGCTimePerStep: {
381-
fakeStep: 2
382-
},
383-
recentGarbagePerStep: {
384-
fakeStep: 200
385-
},
386-
recentRetainedMemoryPerStep: {
387-
fakeStep: 100
370+
recentResult: {
371+
fakeStep: {
372+
testTime: 5,
373+
gcTime: 2,
374+
recentGarbagePerStep: 200,
375+
recentRetainedMemoryPerStep: 100
376+
}
388377
}
389378
};
390379
bp.Report.timesPerAction = {
391380
fakeStep: {
392-
times: [3,7],
393-
fmtTimes: ['3', '7'],
394-
fmtGcTimes: ['1','3'],
395-
garbageTimes: [50,50],
396-
fmtGarbageTimes: ['50','50'],
397-
retainedTimes: [25,25],
398-
fmtRetainedTimes: ['25','25'],
381+
testTimes: [3,7],
382+
garbageCount: [50,50],
383+
retainedCount: [25,25],
399384
gcTimes: [1,3],
400385
nextEntry: 2
401386
},
@@ -412,10 +397,10 @@ describe('bp', function() {
412397

413398
it('should set the most recent time for each step to the next entry', function() {
414399
bp.Report.calcStats();
415-
expect(bp.Report.timesPerAction.fakeStep.times[2]).toBe(5);
416-
bp.Runner.runState.recentTimePerStep.fakeStep = 25;
400+
expect(bp.Report.timesPerAction.fakeStep.testTimes[2]).toBe(5);
401+
bp.Runner.runState.recentResult.fakeStep.testTime = 25;
417402
bp.Report.calcStats();
418-
expect(bp.Report.timesPerAction.fakeStep.times[3]).toBe(25);
403+
expect(bp.Report.timesPerAction.fakeStep.testTimes[3]).toBe(25);
419404
});
420405

421406

@@ -438,39 +423,5 @@ describe('bp', function() {
438423
expect(bp.Report.rightSizeTimes([0,1,2,3,4,5])).toEqual([0,1,2,3,4,5]);
439424
});
440425
});
441-
442-
443-
describe('.generateReportModel()', function() {
444-
it('should return properly formatted data', function() {
445-
expect(bp.Report.generateReportModel({
446-
name: 'Some Step',
447-
avg: {
448-
time: 1.234567,
449-
gcTime: 2.345678,
450-
garbage: 6.5,
451-
retained: 7.5
452-
},
453-
times: ['1','2'],
454-
gcTimes: ['4','5'],
455-
garbageTimes: ['6','7'],
456-
retainedTimes: ['7','8'],
457-
timesConfidenceInterval: 0.5555
458-
})).toEqual({
459-
name : 'Some Step',
460-
avg : {
461-
time : '1.2345',
462-
gcTime : '2.3456',
463-
garbage : '6.5',
464-
retained : '7.5',
465-
combinedTime : '3.5802'
466-
},
467-
times : '1<br>2',
468-
gcTimes : '4<br>5',
469-
garbageTimes : '6<br>7',
470-
retainedTimes : '7<br>8',
471-
timesConfidenceInterval: '0.56'
472-
});
473-
});
474-
});
475426
});
476427
});

0 commit comments

Comments
 (0)