prefix. profiler.

This commit is contained in:
Christopher Jeffrey 2016-05-26 17:10:21 -07:00
parent 2612d2cc5c
commit e59cf312c6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 38 additions and 15 deletions

View File

@ -209,8 +209,13 @@ Environment.prototype.set = function set(options) {
|| 'main'; || 'main';
options.prefix = options.prefix options.prefix = options.prefix
|| process.env.BCOIN_PREFIX || process.env.BCOIN_PREFIX;
|| utils.HOME + '/.bcoin/' + options.network;
if (!options.prefix) {
options.prefix = utils.HOME + '/.bcoin';
if (options.network !== 'main')
options.prefix += '/' + options.network;
}
if (!options.db) if (!options.db)
options.db = process.env.BCOIN_DB; options.db = process.env.BCOIN_DB;
@ -281,6 +286,9 @@ Environment.prototype.ensurePrefix = function ensurePrefix() {
this._ensured = true; this._ensured = true;
mkdirp(this.prefix); mkdirp(this.prefix);
if (this.profile)
mkdirp(this.prefix + '/profiler');
}; };
/** /**

View File

@ -23,6 +23,7 @@ function ensure() {
if (bcoin.profile && !bcoin.isBrowser) { if (bcoin.profile && !bcoin.isBrowser) {
v8profiler = require('v8-' + 'profiler'); v8profiler = require('v8-' + 'profiler');
fs = require('f' + 's'); fs = require('f' + 's');
bcoin.ensurePrefix();
} }
} }
@ -35,23 +36,25 @@ function ensure() {
function Profile(name) { function Profile(name) {
if (v8profiler && bcoin.profile) { if (v8profiler && bcoin.profile) {
name = 'profile-' + (name ? name + '-' : '') + Profile.uid++; name = 'profile-' + (name ? name + '-' : '') + Date.now();
bcoin.debug('Starting CPU profile: %s', name); bcoin.debug('Starting CPU profile: %s', name);
this.profile = v8profiler.startProfiling(name, true); v8profiler.startProfiling(name, true);
this.name = name; this.name = name;
this.profile = null;
this.finished = false;
} }
} }
Profile.uid = 0;
/** /**
* Stop profiling. * Stop profiling.
*/ */
Profile.prototype.stopProfiling = function stopProfiling() { Profile.prototype.stop = function stop() {
if (!v8profiler) if (!v8profiler)
return; return;
assert(!this.finished);
this.profile = v8profiler.stopProfiling(this.name); this.profile = v8profiler.stopProfiling(this.name);
}; };
@ -63,6 +66,11 @@ Profile.prototype.del = function del() {
if (!v8profiler) if (!v8profiler)
return; return;
assert(!this.finished);
if (!this.profile)
this.stop();
this.profile['delete'](); this.profile['delete']();
}; };
@ -79,8 +87,10 @@ Profile.prototype.save = function save(callback) {
if (!v8profiler) if (!v8profiler)
return callback(); return callback();
assert(!this.finished);
if (!this.profile) if (!this.profile)
this.stopProfiling(); this.stop();
bcoin.debug('Saving CPU profile: %s', this.name); bcoin.debug('Saving CPU profile: %s', this.name);
@ -88,12 +98,16 @@ Profile.prototype.save = function save(callback) {
var file; var file;
self.profile['delete'](); self.profile['delete']();
delete self.profile; self.profile = null;
self.finished = true;
if (err) if (err)
return callback(err); return callback(err);
file = bcoin.prefix + '/' + self.name + '.cpuprofile'; file = bcoin.prefix
+ '/profiler/'
+ self.name
+ '.cpuprofile';
fs.writeFile(file, result, callback); fs.writeFile(file, result, callback);
}); });
@ -108,15 +122,13 @@ Profile.prototype.save = function save(callback) {
function Snapshot(name) { function Snapshot(name) {
if (v8profiler && bcoin.profile) { if (v8profiler && bcoin.profile) {
name = 'snapshot-' + (name ? name + '-' : '') + Snapshot.uid++; name = 'snapshot-' + (name ? name + '-' : '') + Date.now();
bcoin.debug('Taking heap snapshot: %s', name); bcoin.debug('Taking heap snapshot: %s', name);
this.snapshot = v8profiler.takeSnapshot(name); this.snapshot = v8profiler.takeSnapshot(name);
this.name = name; this.name = name;
} }
} }
Snapshot.uid = 0;
/** /**
* Compare two snapshots. * Compare two snapshots.
* @param {Snapshot} * @param {Snapshot}
@ -180,12 +192,15 @@ Snapshot.prototype.save = function save(callback) {
var file; var file;
self.snapshot['delete'](); self.snapshot['delete']();
delete self.snapshot; self.snapshot = null;
if (err) if (err)
return callback(err); return callback(err);
file = bcoin.prefix + '/' + self.name + '.heapsnapshot'; file = bcoin.prefix
+ '/profiler/'
+ self.name
+ '.heapsnapshot';
fs.writeFile(file, result, callback); fs.writeFile(file, result, callback);
}); });