UNPKG

playwright-performance-reporter

Version:

Measure and publish performance metrics from browser dev-tools when running playwright

51 lines (50 loc) 1.62 kB
import { nativeChromiumPlugins, } from '../plugins/index.js'; import { enhanceGarbageCollectionPlugin, } from '../../../helpers/index.js'; export class AllPerformanceMetrics { options; name = 'allPerformanceMetrics'; plugins = [ nativeChromiumPlugins.performanceDomainPlugin, ]; constructor(options) { this.options = options; enhanceGarbageCollectionPlugin(nativeChromiumPlugins.heapGarbageCollectorPlugin, this, this.options); } /** * @inheritdoc */ async onStart(accumulator, developmentTools) { await this.common(accumulator, developmentTools); } /** * @inheritdoc */ async onSampling(accumulator, developmentTools) { await this.common(accumulator, developmentTools); } /** * @inheritdoc */ async onStop(accumulator, developmentTools) { await this.common(accumulator, developmentTools); } /** * Common function for onStart and onStop hook */ async common(accumulator, client) { return new Promise(resolve => { client.send('Performance.getMetrics', (error, cdpResponse) => { if (!cdpResponse || Boolean(error)) { resolve(false); return; } if (cdpResponse.metrics.length === 0) { resolve(false); return; } Object.assign(accumulator, Object.fromEntries(cdpResponse.metrics.map(metric => [metric.name, metric.value]))); resolve(true); }); }); } }