|
| 1 | +import { GitHash } from '../util/git.js'; |
| 2 | +import { Result } from './result.js'; |
| 3 | +import { ResultsSet } from './results-set.js'; |
| 4 | +import { MetricsStats } from './metrics-stats.js'; |
| 5 | +import { filesize } from "filesize"; |
| 6 | + |
| 7 | +// Compares latest result to previous/baseline results and produces the needed |
| 8 | +// info. |
| 9 | +export class ResultsAnalyzer { |
| 10 | + public static analyze(currentResult: Result, baselineResults: ResultsSet): AnalyzerItem[] { |
| 11 | + const items = new ResultsAnalyzer(currentResult).collect(); |
| 12 | + |
| 13 | + const baseline = baselineResults.find( |
| 14 | + (other) => other.cpuThrottling == currentResult.cpuThrottling && |
| 15 | + other.name == currentResult.name && |
| 16 | + other.networkConditions == currentResult.networkConditions); |
| 17 | + |
| 18 | + if (baseline != undefined) { |
| 19 | + const baseItems = new ResultsAnalyzer(baseline[1]).collect(); |
| 20 | + // update items with baseline results |
| 21 | + for (const base of baseItems) { |
| 22 | + for (const item of items) { |
| 23 | + if (item.metric == base.metric) { |
| 24 | + item.other = base.value; |
| 25 | + item.otherHash = baseline[0]; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return items; |
| 32 | + } |
| 33 | + |
| 34 | + private constructor(private result: Result) { } |
| 35 | + |
| 36 | + private collect(): AnalyzerItem[] { |
| 37 | + const items = new Array<AnalyzerItem>(); |
| 38 | + |
| 39 | + const aStats = new MetricsStats(this.result.aResults); |
| 40 | + const bStats = new MetricsStats(this.result.bResults); |
| 41 | + |
| 42 | + const pushIfDefined = function (metric: AnalyzerItemMetric, unit: AnalyzerItemUnit, valueA?: number, valueB?: number) { |
| 43 | + if (valueA == undefined || valueB == undefined) return; |
| 44 | + |
| 45 | + items.push({ |
| 46 | + metric: metric, |
| 47 | + value: { |
| 48 | + unit: unit, |
| 49 | + asDiff: () => valueB - valueA, |
| 50 | + asRatio: () => valueB / valueA, |
| 51 | + asString: () => { |
| 52 | + const diff = valueB - valueA; |
| 53 | + const prefix = diff >= 0 ? '+' : ''; |
| 54 | + |
| 55 | + switch (unit) { |
| 56 | + case AnalyzerItemUnit.bytes: |
| 57 | + return prefix + filesize(diff); |
| 58 | + case AnalyzerItemUnit.ratio: |
| 59 | + return prefix + (diff * 100).toFixed(2) + ' %'; |
| 60 | + default: |
| 61 | + return prefix + diff.toFixed(2) + ' ' + AnalyzerItemUnit[unit]; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + pushIfDefined(AnalyzerItemMetric.lcp, AnalyzerItemUnit.ms, aStats.lcp, bStats.lcp); |
| 69 | + pushIfDefined(AnalyzerItemMetric.cls, AnalyzerItemUnit.ms, aStats.cls, bStats.cls); |
| 70 | + pushIfDefined(AnalyzerItemMetric.cpu, AnalyzerItemUnit.ratio, aStats.cpu, bStats.cpu); |
| 71 | + pushIfDefined(AnalyzerItemMetric.memoryAvg, AnalyzerItemUnit.bytes, aStats.memoryAvg, bStats.memoryAvg); |
| 72 | + pushIfDefined(AnalyzerItemMetric.memoryMax, AnalyzerItemUnit.bytes, aStats.memoryMax, bStats.memoryMax); |
| 73 | + |
| 74 | + return items.filter((item) => item.value != undefined); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export enum AnalyzerItemUnit { |
| 79 | + ms, |
| 80 | + ratio, // 1.0 == 100 % |
| 81 | + bytes, |
| 82 | +} |
| 83 | + |
| 84 | +export interface AnalyzerItemValue { |
| 85 | + unit: AnalyzerItemUnit; |
| 86 | + asString(): string; |
| 87 | + asDiff(): number; |
| 88 | + asRatio(): number; // 1.0 == 100 % |
| 89 | +} |
| 90 | + |
| 91 | +export enum AnalyzerItemMetric { |
| 92 | + lcp, |
| 93 | + cls, |
| 94 | + cpu, |
| 95 | + memoryAvg, |
| 96 | + memoryMax, |
| 97 | +} |
| 98 | + |
| 99 | +export interface AnalyzerItem { |
| 100 | + metric: AnalyzerItemMetric; |
| 101 | + value: AnalyzerItemValue; |
| 102 | + other?: AnalyzerItemValue; |
| 103 | + otherHash?: GitHash; |
| 104 | +} |
0 commit comments