UNPKG

playwright-performance-reporter

Version:

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

81 lines (80 loc) 2.63 kB
import { nativeChromiumPlugins, } from '../plugins/index.js'; import { enhanceGarbageCollectionPlugin, } from '../../../helpers/index.js'; export class HeapObjectsTracking { options; name = 'heapObjectsTracking'; plugins = [ nativeChromiumPlugins.heapProfilerDomainPlugin, ]; /** * Options to call `HeapProfiler.startTrackingHeapObjects` which are mandatory to collect * * @private */ startTrackingHeapObjectsOptions = { trackAllocations: true, }; constructor(options) { this.options = options; enhanceGarbageCollectionPlugin(nativeChromiumPlugins.heapGarbageCollectorPlugin, this, this.options); } /** * @inheritdoc */ async onStart(accumulator, developmentTools) { return new Promise(async (resolve, reject) => { try { await developmentTools.HeapProfiler.startTrackingHeapObjects(this.startTrackingHeapObjectsOptions); } catch { reject(new Error('HeapProfiler.startTrackingHeapObjects command failed')); } resolve(); }); } /** * @inheritdoc */ async onSampling(accumulator, developmentTools) { } /** * @inheritdoc */ async onStop(accumulator, developmentTools) { return new Promise(async (resolve, reject) => { const subscriptions = []; const chunks = []; try { subscriptions.push(this.listenForNextChunk(developmentTools, chunks)); await this.stopTrackingHeapObjects(developmentTools); accumulator.heapObjectsTracking = chunks.join(''); } catch { } finally { for (const subscription of subscriptions) { subscription(); } } resolve(); }); } /** * Wrapper for `HeapProfiler.addHeapSnapshotChunk` and report response continuously */ listenForNextChunk(client, chunkAggregation) { const unsubscribe = client.HeapProfiler.addHeapSnapshotChunk(cdpResponse => { chunkAggregation.push(cdpResponse.chunk); }); return unsubscribe; } async stopTrackingHeapObjects(developmentTools) { return new Promise(async (resolve, reject) => { try { await developmentTools.HeapProfiler.stopTrackingHeapObjects(); } catch { reject(new Error('HeapProfiler.stopSampling command failed')); } resolve(); }); } }