-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer-monitor.js
154 lines (120 loc) · 2.88 KB
/
observer-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
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
import { MongoInternals } from "meteor/mongo";
import { EJSON } from "meteor/ejson";
import { MeteorPerf } from './index';
const _observeChanges = MongoInternals.Connection.prototype._observeChanges;
MongoInternals.Connection.prototype._observeChanges = observeChanges
export const StatDict = new Map();
async function observeChanges (...args) {
const [cursorDescription,,callbacks] = args;
const stat = getStat(cursorDescription);
const originals = {
added: callbacks.added,
changed: callbacks.changed,
removed: callbacks.removed,
}
callbacks.added = function (...args) {
stat.incrementAdded();
originals.added.call(this, ...args);
}
callbacks.changed = function (...args) {
stat.incrementChanged();
originals.changed.call(this, ...args);
}
callbacks.removed = function (...args) {
stat.incrementRemoved();
originals.removed.call(this, ...args);
}
const handle = await _observeChanges.call(this, ...args);
stat.incrementStarted()
const originalStop = handle.stop;
handle.stop = function () {
stat.incrementStopped();
return originalStop.call(this);
}
return handle;
}
function getStat(cursorDescription) {
const key = getKey(cursorDescription);
if (!StatDict.has(key)) {
StatDict.set(key, new NamespaceStat(cursorDescription));
}
return StatDict.get(key)
}
class NamespaceStat {
/**
* Documents
*/
added = 0;
changed = 0;
removed = 0;
/**
* Documents When Lagging
*/
laggingAdded = 0;
laggingChanged = 0;
laggingRemoved = 0;
/**
* Observer Handles
*/
started = 0;
stopped = 0;
/**
* Score
*/
score = 0;
constructor(cursorDescription) {
this.key = getKey(cursorDescription);
}
incrementAdded() {
if (MeteorPerf.EventLoopMonitor.lagging) {
this.laggingAdded++;
this.score += 10;
} else {
this.added++;
this.score++;
}
}
incrementChanged() {
if (MeteorPerf.EventLoopMonitor.lagging) {
this.laggingChanged++;
this.score += 10;
} else {
this.changed++;
this.score++;
}
}
incrementRemoved() {
if (MeteorPerf.EventLoopMonitor.lagging) {
this.laggingRemoved++;
this.score += 10;
} else {
this.removed++;
this.score++;
}
}
incrementStarted() {
this.started++;
this.score += 100;
}
incrementStopped() {
this.stopped++;
this.score += 10;
}
getStats() {
return {
key: this.key,
added: this.added,
changed: this.changed,
removed: this.removed,
started: this.started,
stopped: this.stopped,
lagging_added: this.laggingAdded,
lagging_changed: this.laggingChanged,
lagging_removed: this.laggingRemoved,
score: this.score,
}
}
}
function getKey(cursorDescription) {
return `${cursorDescription.collectionName}::${EJSON.stringify(cursorDescription.selector)}`;
}