From e59cf312c641131a2c411d65727ceb4a9bcb4016 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 26 May 2016 17:10:21 -0700 Subject: [PATCH] prefix. profiler. --- lib/bcoin/env.js | 12 ++++++++++-- lib/bcoin/profiler.js | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index 3e5e2dd4..6b415070 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -209,8 +209,13 @@ Environment.prototype.set = function set(options) { || 'main'; options.prefix = options.prefix - || process.env.BCOIN_PREFIX - || utils.HOME + '/.bcoin/' + options.network; + || process.env.BCOIN_PREFIX; + + if (!options.prefix) { + options.prefix = utils.HOME + '/.bcoin'; + if (options.network !== 'main') + options.prefix += '/' + options.network; + } if (!options.db) options.db = process.env.BCOIN_DB; @@ -281,6 +286,9 @@ Environment.prototype.ensurePrefix = function ensurePrefix() { this._ensured = true; mkdirp(this.prefix); + + if (this.profile) + mkdirp(this.prefix + '/profiler'); }; /** diff --git a/lib/bcoin/profiler.js b/lib/bcoin/profiler.js index f2edb75d..c5a1e0f4 100644 --- a/lib/bcoin/profiler.js +++ b/lib/bcoin/profiler.js @@ -23,6 +23,7 @@ function ensure() { if (bcoin.profile && !bcoin.isBrowser) { v8profiler = require('v8-' + 'profiler'); fs = require('f' + 's'); + bcoin.ensurePrefix(); } } @@ -35,23 +36,25 @@ function ensure() { function Profile(name) { if (v8profiler && bcoin.profile) { - name = 'profile-' + (name ? name + '-' : '') + Profile.uid++; + name = 'profile-' + (name ? name + '-' : '') + Date.now(); bcoin.debug('Starting CPU profile: %s', name); - this.profile = v8profiler.startProfiling(name, true); + v8profiler.startProfiling(name, true); this.name = name; + this.profile = null; + this.finished = false; } } -Profile.uid = 0; - /** * Stop profiling. */ -Profile.prototype.stopProfiling = function stopProfiling() { +Profile.prototype.stop = function stop() { if (!v8profiler) return; + assert(!this.finished); + this.profile = v8profiler.stopProfiling(this.name); }; @@ -63,6 +66,11 @@ Profile.prototype.del = function del() { if (!v8profiler) return; + assert(!this.finished); + + if (!this.profile) + this.stop(); + this.profile['delete'](); }; @@ -79,8 +87,10 @@ Profile.prototype.save = function save(callback) { if (!v8profiler) return callback(); + assert(!this.finished); + if (!this.profile) - this.stopProfiling(); + this.stop(); bcoin.debug('Saving CPU profile: %s', this.name); @@ -88,12 +98,16 @@ Profile.prototype.save = function save(callback) { var file; self.profile['delete'](); - delete self.profile; + self.profile = null; + self.finished = true; if (err) return callback(err); - file = bcoin.prefix + '/' + self.name + '.cpuprofile'; + file = bcoin.prefix + + '/profiler/' + + self.name + + '.cpuprofile'; fs.writeFile(file, result, callback); }); @@ -108,15 +122,13 @@ Profile.prototype.save = function save(callback) { function Snapshot(name) { if (v8profiler && bcoin.profile) { - name = 'snapshot-' + (name ? name + '-' : '') + Snapshot.uid++; + name = 'snapshot-' + (name ? name + '-' : '') + Date.now(); bcoin.debug('Taking heap snapshot: %s', name); this.snapshot = v8profiler.takeSnapshot(name); this.name = name; } } -Snapshot.uid = 0; - /** * Compare two snapshots. * @param {Snapshot} @@ -180,12 +192,15 @@ Snapshot.prototype.save = function save(callback) { var file; self.snapshot['delete'](); - delete self.snapshot; + self.snapshot = null; if (err) return callback(err); - file = bcoin.prefix + '/' + self.name + '.heapsnapshot'; + file = bcoin.prefix + + '/profiler/' + + self.name + + '.heapsnapshot'; fs.writeFile(file, result, callback); });