-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (66 loc) · 2.12 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
//The module code will go here
module.exports = {
_usedLogger: console.log,
logger: console.log,
disableLogging(){
this._usedLogger = this.logger;
this.logger = undefined;
},
enableLogging()
{
this.logger = this._usedLogger;
},
measureCall(funcToMeasure, ...args) {
let funcName = funcToMeasure.name;
if (funcToMeasure[Symbol.toStringTag] == "AsyncFunction") {
//we are delaing with an async function
let start = Date.now();
let promise = funcToMeasure(...args);
promise.then((result) => {
let stop = Date.now();
this.report(funcName, (stop - start), Date.now());
return result;
});
return promise;
}
else {
let start = Date.now();
var result = funcToMeasure(...args);
let stop = Date.now();
if (result instanceof Promise) {
let promise = result;
promise.then((result) => {
let stop = Date.now();
this.report(funcName, (stop - start), Date.now());
return result;
});
return promise;
}
else {
this.report(funcName, (stop - start), Date.now());
return result;
}
}
},
report(funcName, callDuration, calledWhen) {
if (arguments.length == 0) {
this.results.forEach(({name, duration, when}) => {
this.reportItem(name, duration, when, this.logger);
});
}
else {
let item = { name: funcName, duration: callDuration, when: calledWhen };
this.results.push(item);
let {name, duration, when} = item;
this.reportItem(name, duration, when, this.logger);
}
},
reportItem(name, duration, when, logger)
{
if (logger) {
let funcName = "Function '" + name + "'";
logger(when + ": " + funcName + " took: " + duration + "ms");
}
},
results: []
}