2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-09-15 00:47:01 +08:00
|
|
|
var Profiler = require('step-profiler');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy to encapsulate node-step-profiler module so there is no need to check if there is an instance
|
|
|
|
*/
|
2019-10-22 01:07:24 +08:00
|
|
|
function ProfilerProxy (opts) {
|
2015-09-15 00:47:01 +08:00
|
|
|
this.profile = !!opts.profile;
|
|
|
|
|
|
|
|
this.profiler = null;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (opts.profile) {
|
|
|
|
this.profiler = new Profiler({ statsd_client: opts.statsd_client });
|
2015-09-15 00:47:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ProfilerProxy.prototype.done = function (what) {
|
2015-09-15 00:47:01 +08:00
|
|
|
if (this.profile) {
|
|
|
|
this.profiler.done(what);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ProfilerProxy.prototype.end = function () {
|
2015-09-15 00:47:01 +08:00
|
|
|
if (this.profile) {
|
|
|
|
this.profiler.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ProfilerProxy.prototype.start = function (what) {
|
2015-09-15 00:47:01 +08:00
|
|
|
if (this.profile) {
|
|
|
|
this.profiler.start(what);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ProfilerProxy.prototype.add = function (what) {
|
2015-09-15 00:47:01 +08:00
|
|
|
if (this.profile) {
|
|
|
|
this.profiler.add(what || {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ProfilerProxy.prototype.sendStats = function () {
|
2015-09-15 00:47:01 +08:00
|
|
|
if (this.profile) {
|
|
|
|
this.profiler.sendStats();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ProfilerProxy.prototype.toString = function () {
|
|
|
|
return this.profile ? this.profiler.toString() : '';
|
2015-09-15 00:47:01 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ProfilerProxy.prototype.toJSONString = function () {
|
|
|
|
return this.profile ? this.profiler.toJSONString() : '{}';
|
2015-09-15 00:47:01 +08:00
|
|
|
};
|
|
|
|
|
2020-06-02 23:09:06 +08:00
|
|
|
ProfilerProxy.prototype.toJSON = function () {
|
|
|
|
return this.profile ? JSON.parse(this.profiler.toJSONString()) : {};
|
|
|
|
};
|
|
|
|
|
2015-09-15 00:47:01 +08:00
|
|
|
module.exports = ProfilerProxy;
|