forked from steinslin/note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.js
88 lines (79 loc) · 2.02 KB
/
monitor.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
const logUpdate = require('log-update')
const asciichart = require('asciichart')
const chalk = require('chalk')
const Measured = require('measured')
const timer = new Measured.Timer()
const history = new Array(120)
history.fill(0)
const monitor = obj => {
return new Proxy(obj, {
get(target, propKey) {
const origMethod = target[propKey]
if (!origMethod) return
return (...args) => {
const stopwatch = timer.start()
const result = origMethod.apply(this, args)
return result.then(out => {
const n = stopwatch.end()
history.shift()
history.push(n)
return out
})
}
}
})
}
const service = {
callService() {
return new Promise(resolve =>
setTimeout(resolve, Math.random() * 50 + 50))
}
}
const monitoredService = monitor(service)
setInterval(() => {
monitoredService.callService()
.then(() => {
const fields = ['min', 'max', 'sum', 'variance',
'mean', 'count', 'median']
const histogram = timer.toJSON().histogram
const lines = [
'',
...fields.map(field =>
chalk.cyan(field) + ': ' +
(histogram[field] || 0).toFixed(2))
]
logUpdate(asciichart.plot(history, { height: 10 })
+ lines.join('\n'))
})
.catch(err => console.error(err))
}, 100)
function Animal() {
this.type = 123
}
function Cat() {
Animal.call(this)
this.name = 'tom'
}
const valueOf = Array.prototype.valueOf
const toString = Array.prototype.toString
const oValueof = Object.prototype.valueOf
const oToString = Object.prototype.toString
Array.prototype.valueOf = function () {
console.log('array valueOf')
return valueOf.call(this)
}
Array.prototype.toString = function () {
console.log('array toString')
return toString.call(this)
}
Object.prototype.valueOf = function () {
console.log('object valueOf')
return oValueof.call(this)
}
Object.prototype.toString = function () {
console.log('object toString')
return oToString.call(this)
}
[] + []
{} + {}
{} + []