From 642e7e5c461aa48533f24626cd616cfbefea9fb3 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Fri, 17 Apr 2015 18:15:22 -0700 Subject: [PATCH] fix(benchpress): only print the CV when it is meaningful When the mean is 0, the coefficient of variation is calculated to be NaN, which is not meaningful, so instead of printing "+-NaN%", just don't print the CV at all. Closes #908 Closes #1444 --- .../benchpress/src/reporter/console_reporter.js | 6 +++--- .../test/reporter/console_reporter_spec.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/modules/benchpress/src/reporter/console_reporter.js b/modules/benchpress/src/reporter/console_reporter.js index 7682a6d4bc..3663075eb0 100644 --- a/modules/benchpress/src/reporter/console_reporter.js +++ b/modules/benchpress/src/reporter/console_reporter.js @@ -83,10 +83,10 @@ export class ConsoleReporter extends Reporter { var sample = ListWrapper.map(validSample, (measureValues) => measureValues.values[metricName]); var mean = Statistic.calculateMean(sample); var cv = Statistic.calculateCoefficientOfVariation(sample, mean); - var formattedCv = NumberWrapper.isNaN(cv) ? 'NaN' : Math.floor(cv); + var formattedMean = ConsoleReporter._formatNum(mean) // Note: Don't use the unicode character for +- as it might cause - // hickups consoles... - return `${ConsoleReporter._formatNum(mean)}+-${formattedCv}%`; + // hickups for consoles... + return NumberWrapper.isNaN(cv) ? formattedMean : `${formattedMean}+-${Math.floor(cv)}%`; }) ); return PromiseWrapper.resolve(null); diff --git a/modules/benchpress/test/reporter/console_reporter_spec.js b/modules/benchpress/test/reporter/console_reporter_spec.js index a76a7cb262..e4b1fe0f96 100644 --- a/modules/benchpress/test/reporter/console_reporter_spec.js +++ b/modules/benchpress/test/reporter/console_reporter_spec.js @@ -96,6 +96,22 @@ export function main() { ]); }); + it('should print the coefficient of variation only when it is meaningful', () => { + createReporter({ + columnWidth: 8, + metrics: { 'a': '', 'b': '' } + }); + log = []; + reporter.reportSample([], [ + mv(0, 0, { 'a': 3, 'b': 0 }), + mv(1, 1, { 'a': 5, 'b': 0 }) + ]); + expect(log).toEqual([ + '======== | ========', + '4.00+-25% | 0.00' + ]); + }); + }); }