UNPKG

playwright-performance-reporter

Version:

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

53 lines (52 loc) 1.72 kB
import { nativeChromiumPlugins, } from '../plugins/index.js'; import { enhanceGarbageCollectionPlugin, } from '../../../helpers/index.js'; export class TotalJsHeapSize { options; name = 'totalJsHeapSize'; chromiumCompatibleName = 'JSHeapTotalSize'; 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; } const foundMetric = cdpResponse.metrics.find(metricResponse => metricResponse.name === this.chromiumCompatibleName); if (!foundMetric) { resolve(false); return; } Object.assign(accumulator, { [this.name]: foundMetric.value }); resolve(true); }); }); } }