11import { filesize } from 'filesize' ;
22
33import { GitHash } from '../util/git.js' ;
4- import { MetricsStats } from './metrics-stats.js' ;
4+ import { AnalyticsFunction , MetricsStats , NumberProvider } from './metrics-stats.js' ;
55import { Result } from './result.js' ;
66import { ResultsSet } from './results-set.js' ;
77
@@ -24,7 +24,7 @@ export class ResultsAnalyzer {
2424 for ( const base of baseItems ) {
2525 for ( const item of items ) {
2626 if ( item . metric == base . metric ) {
27- item . other = base . value ;
27+ item . others = base . values ;
2828 otherHash = baseline [ 0 ] ;
2929 }
3030 }
@@ -40,21 +40,26 @@ export class ResultsAnalyzer {
4040 private _collect ( ) : AnalyzerItem [ ] {
4141 const items = new Array < AnalyzerItem > ( ) ;
4242
43- const aStats = new MetricsStats ( this . _result . aResults ) ;
44- const bStats = new MetricsStats ( this . _result . bResults ) ;
43+ const scenarioResults = this . _result . scenarioResults ;
4544
46- const pushIfDefined = function ( metric : AnalyzerItemMetric , unit : AnalyzerItemUnit , valueA ?: number , valueB ?: number ) : void {
47- if ( valueA == undefined || valueB == undefined ) return ;
48- items . push ( { metric : metric , value : new AnalyzerItemNumberValue ( unit , valueA , valueB ) } )
45+ const pushIfDefined = function ( metric : AnalyzerItemMetric , unit : AnalyzerItemUnit , source : NumberProvider , fn : AnalyticsFunction ) : void {
46+ const values = scenarioResults . map ( items => fn ( items , source ) ) ;
47+ // only push if at least one value is defined
48+ if ( values . findIndex ( v => v != undefined ) >= 0 ) {
49+ items . push ( {
50+ metric : metric ,
51+ values : new AnalyzerItemNumberValues ( unit , values )
52+ } ) ;
53+ }
4954 }
5055
51- pushIfDefined ( AnalyzerItemMetric . lcp , AnalyzerItemUnit . ms , aStats . mean ( MetricsStats . lcp ) , bStats . mean ( MetricsStats . lcp ) ) ;
52- pushIfDefined ( AnalyzerItemMetric . cls , AnalyzerItemUnit . ms , aStats . mean ( MetricsStats . cls ) , bStats . mean ( MetricsStats . cls ) ) ;
53- pushIfDefined ( AnalyzerItemMetric . cpu , AnalyzerItemUnit . ratio , aStats . mean ( MetricsStats . cpu ) , bStats . mean ( MetricsStats . cpu ) ) ;
54- pushIfDefined ( AnalyzerItemMetric . memoryAvg , AnalyzerItemUnit . bytes , aStats . mean ( MetricsStats . memoryMean ) , bStats . mean ( MetricsStats . memoryMean ) ) ;
55- pushIfDefined ( AnalyzerItemMetric . memoryMax , AnalyzerItemUnit . bytes , aStats . max ( MetricsStats . memoryMax ) , bStats . max ( MetricsStats . memoryMax ) ) ;
56+ pushIfDefined ( AnalyzerItemMetric . lcp , AnalyzerItemUnit . ms , MetricsStats . lcp , MetricsStats . mean ) ;
57+ pushIfDefined ( AnalyzerItemMetric . cls , AnalyzerItemUnit . ms , MetricsStats . cls , MetricsStats . mean ) ;
58+ pushIfDefined ( AnalyzerItemMetric . cpu , AnalyzerItemUnit . ratio , MetricsStats . cpu , MetricsStats . mean ) ;
59+ pushIfDefined ( AnalyzerItemMetric . memoryAvg , AnalyzerItemUnit . bytes , MetricsStats . memoryMean , MetricsStats . mean ) ;
60+ pushIfDefined ( AnalyzerItemMetric . memoryMax , AnalyzerItemUnit . bytes , MetricsStats . memoryMax , MetricsStats . max ) ;
5661
57- return items . filter ( ( item ) => item . value != undefined ) ;
62+ return items ;
5863 }
5964}
6065
@@ -64,35 +69,42 @@ export enum AnalyzerItemUnit {
6469 bytes ,
6570}
6671
67- export interface AnalyzerItemValue {
68- readonly a : string ;
69- readonly b : string ;
70- readonly diff : string ;
71- readonly percent : string ;
72+ export interface AnalyzerItemValues {
73+ value ( index : number ) : string ;
74+ diff ( aIndex : number , bIndex : number ) : string ;
75+ percent ( aIndex : number , bIndex : number ) : string ;
7276}
7377
74- class AnalyzerItemNumberValue implements AnalyzerItemValue {
75- constructor ( private _unit : AnalyzerItemUnit , private _a : number , private _b : number ) { }
78+ const AnalyzerItemValueNotAvailable = 'n/a' ;
79+
80+ class AnalyzerItemNumberValues implements AnalyzerItemValues {
81+ constructor ( private _unit : AnalyzerItemUnit , private _values : ( number | undefined ) [ ] ) { }
7682
77- public get a ( ) : string {
78- return this . _withUnit ( this . _a ) ;
83+ private _has ( index : number ) : boolean {
84+ return index >= 0 && index < this . _values . length && this . _values [ index ] != undefined ;
7985 }
8086
81- public get b ( ) : string {
82- return this . _withUnit ( this . _b ) ;
87+ private _get ( index : number ) : number {
88+ return this . _values [ index ] ! ;
8389 }
8490
85- public get diff ( ) : string {
86- const diff = this . _b - this . _a ;
91+ public value ( index : number ) : string {
92+ if ( ! this . _has ( index ) ) return AnalyzerItemValueNotAvailable ;
93+ return this . _withUnit ( this . _get ( index ) ) ;
94+ }
95+
96+ public diff ( aIndex : number , bIndex : number ) : string {
97+ if ( ! this . _has ( aIndex ) || ! this . _has ( bIndex ) ) return AnalyzerItemValueNotAvailable ;
98+ const diff = this . _get ( bIndex ) - this . _get ( aIndex ) ;
8799 const str = this . _withUnit ( diff , true ) ;
88100 return diff > 0 ? `+${ str } ` : str ;
89101 }
90102
91- public get percent ( ) : string {
92- if ( this . _a == 0 ) return 'n/a' ;
93- const diff = this . _b / this . _a * 100 - 100 ;
94- const str = `${ diff . toFixed ( 2 ) } %` ;
95- return diff > 0 ? `+${ str } ` : str ;
103+ public percent ( aIndex : number , bIndex : number ) : string {
104+ if ( ! this . _has ( aIndex ) || ! this . _has ( bIndex ) || this . _get ( aIndex ) == 0.0 ) return AnalyzerItemValueNotAvailable ;
105+ const percent = this . _get ( bIndex ) / this . _get ( aIndex ) * 100 - 100 ;
106+ const str = `${ percent . toFixed ( 2 ) } %` ;
107+ return percent > 0 ? `+${ str } ` : str ;
96108 }
97109
98110 private _withUnit ( value : number , isDiff : boolean = false ) : string {
@@ -119,10 +131,10 @@ export interface AnalyzerItem {
119131 metric : AnalyzerItemMetric ;
120132
121133 // Current (latest) result.
122- value : AnalyzerItemValue ;
134+ values : AnalyzerItemValues ;
123135
124136 // Previous or baseline results, depending on the context.
125- other ?: AnalyzerItemValue ;
137+ others ?: AnalyzerItemValues ;
126138}
127139
128140export interface Analysis {
0 commit comments