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

Commit e4773fa

Browse files
jeffbcrossjbdeboer
authored andcommitted
refactor(benchmark): reduce code redundancy for statistics calculations
1 parent 801c0d7 commit e4773fa

File tree

3 files changed

+55
-58
lines changed

3 files changed

+55
-58
lines changed

benchmark/web/bp.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ var bp = window.bp = {
1515
Report: {
1616
timesPerAction: {}
1717
},
18-
Measure: {}
18+
Measure: {
19+
characteristics: ['gcTime','testTime','garbageCount','retainedCount']
20+
}
1921
};
2022

2123
bp.Measure.numMilliseconds = function() {
@@ -151,8 +153,8 @@ bp.Runner.runTimedTest = function (bs) {
151153
testTime: endTime,
152154
gcTime: endGCTime,
153155
beforeHeap: beforeHeap,
154-
garbage: garbage,
155-
retainedMemory: retainedMemory
156+
garbageCount: garbage,
157+
retainedCount: retainedMemory
156158
};
157159
};
158160

@@ -168,12 +170,16 @@ bp.Report.getTimesPerAction = function(name) {
168170
var tpa = bp.Report.timesPerAction[name];
169171
if (!tpa) {
170172
tpa = bp.Report.timesPerAction[name] = {
171-
testTimes: [], // circular buffer
172-
gcTimes: [],
173-
garbageCount: [],
174-
retainedCount: [],
173+
name: name,
175174
nextEntry: 0
176-
}
175+
};
176+
_.each(bp.Measure.characteristics, function(c) {
177+
tpa[c] = {
178+
recent: undefined,
179+
history: [],
180+
avg: {}
181+
};
182+
});
177183
}
178184
return tpa;
179185
};
@@ -188,8 +194,9 @@ bp.Report.rightSizeTimes = function(times) {
188194
};
189195

190196
bp.Report.updateTimes = function(tpa, index, reference, recentTime) {
191-
tpa[reference][index] = recentTime;
192-
tpa[reference] = bp.Report.rightSizeTimes(tpa[reference]);
197+
tpa[reference].recent = recentTime;
198+
tpa[reference].history[index] = recentTime;
199+
tpa[reference].history = bp.Report.rightSizeTimes(tpa[reference].history);
193200
};
194201

195202
bp.Report.calcStats = function() {
@@ -198,33 +205,21 @@ bp.Report.calcStats = function() {
198205
var recentResult = bp.Runner.runState.recentResult[bs.name],
199206
tpa = bp.Report.getTimesPerAction(bs.name);
200207

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);
208+
_.each(bp.Measure.characteristics, function(c) {
209+
bp.Report.updateTimes(tpa, tpa.nextEntry, c, recentResult[c]);
210+
var mean = bp.Statistics.getMean(tpa[c].history);
211+
var stdDev = bp.Statistics.calculateStandardDeviation(tpa[c].history, mean);
212+
tpa[c].avg = {
213+
mean: mean,
214+
stdDev: stdDev,
215+
coefficientOfVariation: bp.Statistics.calculateCoefficientOfVariation(stdDev, mean)
216+
};
217+
});
205218

206219
tpa.nextEntry++;
207220
tpa.nextEntry %= bp.Runner.runState.numSamples;
208221

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)
220-
};
221-
222-
var reportModel = _.clone(tpa);
223-
reportModel.name = bs.name;
224-
reportModel.avg = avg;
225-
reportModel.testTimes = tpa.testTimes;
226-
227-
report += bp.Report.generatePartial(reportModel);
222+
report += bp.Report.generatePartial(tpa);
228223
});
229224
return report;
230225
};

benchmark/web/bp.spec.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ describe('bp', function() {
214214
}, 'done to be called', 200);
215215

216216
runs(function() {
217-
expect(bp.Report.timesPerAction.fakeStep.testTimes.length).toBe(8);
217+
expect(bp.Report.timesPerAction.fakeStep.testTime.history.length).toBe(8);
218218
});
219219
});
220220
});
@@ -378,29 +378,31 @@ describe('bp', function() {
378378
};
379379
bp.Report.timesPerAction = {
380380
fakeStep: {
381-
testTimes: [3,7],
382-
garbageCount: [50,50],
383-
retainedCount: [25,25],
384-
gcTimes: [1,3],
381+
testTime: {
382+
history: [3,7]
383+
},
384+
garbageCount: {
385+
history: [50,50]
386+
},
387+
retainedCount: {
388+
history: [25,25]
389+
},
390+
gcTime: {
391+
recent: 3,
392+
history: [1,3]
393+
},
385394
nextEntry: 2
386395
},
387396
};
388397
});
389398

390399

391-
xit('should call generateReportPartial() with the correct info', function() {
392-
var spy = spyOn(bp, 'generateReportPartial');
393-
bp.Report.calcStats();
394-
expect(spy).toHaveBeenCalledWith('fakeStep', {time: 5, gcTime: 2}, ['3','7','5'], ['1','3','2'], [50,50,200], [25,25,100])
395-
});
396-
397-
398400
it('should set the most recent time for each step to the next entry', function() {
399401
bp.Report.calcStats();
400-
expect(bp.Report.timesPerAction.fakeStep.testTimes[2]).toBe(5);
402+
expect(bp.Report.timesPerAction.fakeStep.testTime.history[2]).toBe(5);
401403
bp.Runner.runState.recentResult.fakeStep.testTime = 25;
402404
bp.Report.calcStats();
403-
expect(bp.Report.timesPerAction.fakeStep.testTimes[3]).toBe(25);
405+
expect(bp.Report.timesPerAction.fakeStep.testTime.history[3]).toBe(25);
404406
});
405407

406408

benchmark/web/tree.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,42 +68,42 @@
6868
<tr class="sampleContainer">
6969
<td><%= name %></td>
7070
<td class="average">
71-
test:<%= avg.testTime.toFixed(2) %>ms
71+
test:<%= testTime.avg.mean.toFixed(2) %>ms
7272
<br>
73-
deviation: <%= avg.testTimesStdDev.toFixed(2) %>
73+
deviation: <%= testTime.avg.stdDev.toFixed(2) %>
7474
<br>
75-
coefficient of variation: <%= Math.round(avg.coefficientOfVariation * 100) %>%
75+
coefficient of variation: <%= Math.round(testTime.avg.coefficientOfVariation * 100) %>%
7676
<br>
77-
gc:<%= avg.gcTime.toFixed(2) %>ms
77+
gc:<%= gcTime.avg.mean.toFixed(2) %>ms
7878
<br>
79-
combined: <%= avg.combinedTime.toFixed(2) %>ms
79+
combined: <%= (testTime.avg.mean + gcTime.avg.mean).toFixed(2) %>ms
8080
<br>
81-
garbage: <%= (avg.garbage / 1e3).toFixed(2) %>KB
81+
garbage: <%= (garbageCount.avg.mean / 1e3).toFixed(2) %>KB
8282
<br>
83-
retained: <%= (avg.retained / 1e3).toFixed(2) %>KB
83+
retained: <%= (retainedCount.avg.mean / 1e3).toFixed(2) %>KB
8484
</td>
8585
<td>
8686
<div class="sampleContainer">
8787
<div class="testTimeCol">
88-
<% _.each(testTimes, function(time) { %>
88+
<% _.each(testTime.history, function(time) { %>
8989
<%= time.toFixed(2) %>
9090
<br>
9191
<% }); %>
9292
</div>
9393
<div class="testTimeCol">
94-
<% _.each(gcTimes, function(time) { %>
94+
<% _.each(gcTime.history, function(time) { %>
9595
<%= time.toFixed(2) %>
9696
<br>
9797
<% }); %>
9898
</div>
9999
<div class="testTimeCol">
100-
<% _.each(garbageCount, function(count) { %>
100+
<% _.each(garbageCount.history, function(count) { %>
101101
<%= (count / 1e3).toFixed(2) %>
102102
<br>
103103
<% }); %>
104104
</div>
105105
<div class="testTimeCol">
106-
<% _.each(retainedCount, function(count) { %>
106+
<% _.each(retainedCount.history, function(count) { %>
107107
<%= (count / 1e3).toFixed(2) %>
108108
<br>
109109
<% }); %>

0 commit comments

Comments
 (0)