-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
184 lines (160 loc) · 4.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* eslint no-param-reassign:0 */
/**
* A simple, light-weight NodeJS utility for measuring code execution in high-resolution real times.
*
* @module perfy
* @author Onur Yıldırım <[email protected]>
* @license MIT
* @example
* var perfy = require('perfy');
* perfy.start('loop-test');
* // some heavy stuff here...
* console.log(perfy.end('loop-test').time);
*/
module.exports = (function () {
// See https://nodejs.org/api/process.html#process_process_hrtime
// --------------------------------
// CLASS: PerfyItem
// --------------------------------
function PerfyItem(name) {
this.name = name;
this.reset();
}
PerfyItem.prototype.reset = function () {
this.time = {
start: null,
end: null
};
this.utc = {
start: null,
end: null
};
this.result = null;
};
PerfyItem.prototype.start = function () {
this.reset();
this.time.start = process.hrtime();
this.utc.start = Date.now();
};
PerfyItem.prototype.end = function () {
if (!this.time.start) {
throw new Error('start() should be called first!');
}
this.time.end = process.hrtime(this.time.start);
this.utc.end = Date.now();
var o = {
name: this.name || '',
seconds: this.time.end[0],
nanoseconds: this.time.end[1],
// divide by a million to convert nanoseconds to milliseconds
milliseconds: (this.time.end[1] / 1000000),
startTime: this.utc.start,
endTime: this.utc.end
};
var fullMilliseconds = (o.seconds * 1000) + o.milliseconds;
o.fullMilliseconds = Number(fullMilliseconds.toFixed(3));
o.fullSeconds = o.time = Number((fullMilliseconds / 1000).toFixed(3));
o.fullNanoseconds = (o.seconds * 1000 * 1000000) + o.nanoseconds;
var n = this.name ? this.name + ': ' : '';
o.summary = n + o.time + ' sec.';
this.result = o;
return o;
};
// --------------------------------
// CLASS: perfy
// --------------------------------
// storage for PerfyItem instances
var perfList = {},
perfy = {},
ERR = {
NAME: 'Performance instance name required!',
NOITEM: 'No performance instance with name: ',
CALLBACK: 'Callback is not a function!'
};
perfy.start = function (name, autoDestroy) {
if (!name) { throw new Error(ERR.NAME); }
name = String(name);
autoDestroy = typeof autoDestroy === 'undefined' ? true : autoDestroy;
perfList[name] = new PerfyItem(name);
perfList[name].autoDestroy = autoDestroy;
perfList[name].start();
return perfy;
};
perfy.end = function (name) {
if (!name) { throw new Error(ERR.NAME); }
name = String(name);
var p = perfList[name];
if (!p) { throw new Error(ERR.NOITEM + name); }
// if already ended and has result, return
if (p.result) { return p.result; }
var result = p.end();
if (p.autoDestroy) {
delete perfList[name];
}
return result;
};
perfy.result = function (name) {
if (!name) { throw new Error(ERR.NAME); }
name = String(name);
var p = perfList[name];
if (!p) { return null; }
return p.result;
};
perfy.exists = function (name) {
if (!name) { throw new Error(ERR.NAME); }
return Boolean(perfList[name]);
};
perfy.names = function () {
return Object.keys(perfList);
};
perfy.count = function () {
return perfy.names().length;
};
perfy.destroy = function (name) {
if (!name) { throw new Error(ERR.NAME); }
if (perfList[name]) {
delete perfList[name];
}
return perfy;
};
perfy.destroyAll = function () {
perfList = {};
return perfy;
};
perfy.exec = function (name, fn) {
if (typeof fn !== 'function') {
if (typeof name === 'function') {
fn = name;
name = null;
} else {
throw new Error(ERR.CALLBACK);
}
}
var p;
if (name) {
perfList[name] = new PerfyItem(name);
perfList[name].autoDestroy = false;
p = perfList[name];
} else {
p = new PerfyItem();
}
function done() {
var result = p.end();
if (name && p.autoDestroy) {
delete perfList[name];
}
return result;
}
p.start();
if (fn.length > 0) {
fn(done);
return perfy;
}
fn();
return done();
};
// --------------------------------
// EXPORT
// --------------------------------
return perfy;
}());