|
| 1 | +import { Metrics, MetricsCollector } from '../../src/collector.js'; |
| 2 | +import { MetricsStats, NumberProvider } from '../../src/results/metrics-stats.js'; |
| 3 | +import { JankTestScenario } from '../../src/scenarios.js'; |
| 4 | +import { printStats } from '../../src/util/console.js'; |
| 5 | +import { latestResultFile } from './env.js'; |
| 6 | + |
| 7 | +function checkStdDev(results: Metrics[], name: string, provider: NumberProvider, max: number): boolean { |
| 8 | + const value = MetricsStats.stddev(results, provider); |
| 9 | + if (value == undefined) { |
| 10 | + console.warn(`✗ | Discarding results because StandardDeviation(${name}) is undefined`); |
| 11 | + return false; |
| 12 | + } else if (value > max) { |
| 13 | + console.warn(`✗ | Discarding results because StandardDeviation(${name}) is larger than ${max}. Actual value: ${value}`); |
| 14 | + return false; |
| 15 | + } else { |
| 16 | + console.log(`✓ | StandardDeviation(${name}) is ${value} (<= ${max})`) |
| 17 | + } |
| 18 | + return true; |
| 19 | +} |
| 20 | + |
| 21 | +const collector = new MetricsCollector({ headless: true, cpuThrottling: 2 }); |
| 22 | +const result = await collector.execute({ |
| 23 | + name: 'jank', |
| 24 | + scenarios: [ |
| 25 | + new JankTestScenario('index.html'), |
| 26 | + new JankTestScenario('with-sentry.html'), |
| 27 | + new JankTestScenario('with-replay.html'), |
| 28 | + ], |
| 29 | + runs: 10, |
| 30 | + tries: 10, |
| 31 | + async shouldAccept(results: Metrics[]): Promise<boolean> { |
| 32 | + await printStats(results); |
| 33 | + |
| 34 | + if (!checkStdDev(results, 'lcp', MetricsStats.lcp, 50) |
| 35 | + || !checkStdDev(results, 'cls', MetricsStats.cls, 0.1) |
| 36 | + || !checkStdDev(results, 'cpu', MetricsStats.cpu, 1) |
| 37 | + || !checkStdDev(results, 'memory-mean', MetricsStats.memoryMean, 1000 * 1024) |
| 38 | + || !checkStdDev(results, 'memory-max', MetricsStats.memoryMax, 1000 * 1024)) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + const cpuUsage = MetricsStats.mean(results, MetricsStats.cpu)!; |
| 43 | + if (cpuUsage > 0.85) { |
| 44 | + // Note: complexity on the "JankTest" is defined by the `minimum = ...,` setting in app.js - specifying the number of animated elements. |
| 45 | + console.warn(`✗ | Discarding results because CPU usage is too high and may be inaccurate: ${(cpuUsage * 100).toFixed(2)} %.`, |
| 46 | + 'Consider simplifying the scenario or changing the CPU throttling factor.'); |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + return true; |
| 51 | + }, |
| 52 | +}); |
| 53 | + |
| 54 | +result.writeToFile(latestResultFile); |
0 commit comments