Skip to content

Commit

Permalink
fix off-by-one error causing Infinity in 100% EIL
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Aug 2, 2016
1 parent 606e74b commit 2737165
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lighthouse-core/lib/traces/tracing-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class TraceProcessor {
// Loop over durations, calculating a CDF value for each until it is above
// the target percentile.
const percentileTime = percentile * totalTime;
while (cdfTime < percentileTime && durationIndex < durations.length) {
while (cdfTime < percentileTime && durationIndex < durations.length - 1) {
completedTime += duration;
remainingCount -= (duration < 0 ? -1 : 1);

Expand Down
12 changes: 12 additions & 0 deletions lighthouse-core/test/lib/traces/tracing-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,17 @@ describe('TracingProcessor lib', () => {
[16, 16, 16, 31, 46, 55, 56]);
assert.deepEqual(results, expected);
});

it('does not divide by zero when duration sum is less than whole', () => {
// Durations chosen such that, due to floating point error:
// const idleTime = totalTime - (duration1 + duration2);
// (idleTime + duration1 + duration2) < totalTime
const duration1 = 67 / 107;
const duration2 = 67 / 53;
const totalTime = 10;
const results = TracingProcessor._riskPercentiles([duration1, duration2], totalTime, [1], 0);
const expected = createRiskPercentiles([1], [16 + duration2]);
assert.deepEqual(results, expected);
});
});
});

0 comments on commit 2737165

Please sign in to comment.