UNPKG

1.06 kBJavaScriptView Raw
1//
2// # Profiler
3//
4// Adds profiling to requests.
5//
6var _ = require('lodash');
7var fs = require('fs');
8
9var Profiler = module.exports = function (url, conf) {
10 this.url = url;
11 this.conf = conf;
12 this.data = {};
13};
14
15Profiler.prototype.start = function (label, data) {
16 this.data[label] = {
17 label: label,
18 data: data || {},
19 start: Date.now()
20 };
21};
22
23Profiler.prototype.stop = function (label, data) {
24 data = data || {};
25 this.data[label].stop = Date.now();
26 this.data[label].total = this.data[label].stop - this.data[label].start;
27 this.data[label].data = _.merge(this.data[label].data, data);
28};
29
30Profiler.prototype.complete = function () {
31 if (!this.conf) return;
32 var conf = this.conf;
33
34 if (conf.redis) {
35
36 }
37 if (conf.file) {
38 var data = {
39 url: this.url,
40 data: this.data,
41 };
42 fs.appendFileSync(conf.file, JSON.stringify(data) + '\n');
43 }
44};
45
46Profiler.prototype.decorateCallback = function (label, fn) {
47 var self = this;
48 return function () {
49 fn.apply(fn, arguments);
50 self.stop(label);
51 };
52};
53