From e3b4a5aafd960bdf5e730042cb5fecd85f9ed1ab Mon Sep 17 00:00:00 2001 From: Thorsten Lorenz Date: Fri, 13 Mar 2015 19:07:17 -0400 Subject: [PATCH] snapshot: adding snapshot.toJSON convenience method --- test/cpu_cprofiler.js | 12 +++++++++++- v8-profiler.js | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/cpu_cprofiler.js b/test/cpu_cprofiler.js index 1607360..8448a41 100644 --- a/test/cpu_cprofiler.js +++ b/test/cpu_cprofiler.js @@ -1,4 +1,5 @@ const binding = require('../build/Release/profiler'), + profiler = require('../'), expect = require('chai').expect; const NODE_V_10 = /^v0\.10\.\d+$/.test(process.version), @@ -210,6 +211,15 @@ describe('HEAP', function() { } ); }); + + it('Snapshot.toJSON', function(done) { + var snapshot = profiler.takeSnapshot(); + snapshot.toJSON(function callback(err, json) { + expect(!err); + expect(JSON.parse.bind(JSON, json)).to.not.throw(); + done(); + }); + }); }); @@ -218,4 +228,4 @@ describe('HEAP', function() { snapshot.delete(); }); } -}); \ No newline at end of file +}); diff --git a/v8-profiler.js b/v8-profiler.js index 687efc3..81fe4cf 100644 --- a/v8-profiler.js +++ b/v8-profiler.js @@ -58,6 +58,21 @@ Snapshot.prototype.nodeCounts = function() { return objects; }; +Snapshot.prototype.toJSON = function toJSON(cb) { + var chunks = []; + + function onChunk(chunk, len) { + chunks.push(chunk); + } + + function onDone() { + var s = chunks.join(''); + cb(null, s); + } + + this.serialize(onChunk, onDone); +}; + function CpuProfile() {} CpuProfile.prototype.getHeader = function() {