UNPKG

inspector-api

Version:

A simple node module to access V8 inspector + some tools to export and read the data.

54 lines (40 loc) 1.55 kB
'use strict' const utils = require('./utils') class Heap { constructor (session, config, s3Client) { this.s3Client = s3Client this.session = session this.config = config } async enable () { await utils.invokeFunction(this.session, 'HeapProfiler.enable') } async disable () { await utils.invokeFunction(this.session, 'HeapProfiler.disable') } async startSampling () { await utils.invokeFunction(this.session, 'HeapProfiler.startSampling') } async stopSampling () { return utils.invokeStop('HeapProfiler.stopSampling', this.session, 'heapprofiler', 'heapprofile', this.config, this.s3Client) } takeSnapshot () { return new Promise((resolve, reject) => { const res = [] const getChunk = (m) => { res.push(m.params.chunk) } this.session.on('HeapProfiler.addHeapSnapshotChunk', getChunk) this.session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => { this.session.removeListener('HeapProfiler.addHeapSnapshotChunk', getChunk) if (err) return reject(err) const date = new Date() const fileName = `profile_${date.getTime()}.heapsnapshot` utils.writeData(JSON.parse(res.join('')), fileName, this.config, this.s3Client).then((data) => { resolve(data) }).catch(err => reject(err)) }) }) } } module.exports = Heap