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';
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');
};
/**

View File

@ -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);
});