UNPKG

3.13 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2012 Dmitri Melikyan
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to permit
9 * persons to whom the Software is furnished to do so, subject to the
10 * following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25var nt;
26
27var infoBuffer;
28var metricsBuffer = [];
29var samplesBuffer = [];
30
31exports.init = function() {
32 nt = global.nodetime;
33
34 nt.on('info', function(info) {
35 if(!nt.headless)
36 infoBuffer = info;
37 });
38
39 nt.on('metric', function(metric) {
40 if(!nt.headless)
41 metricsBuffer.push(metric);
42 });
43
44 nt.on('sample', function(sample) {
45 if(!nt.headless && nt.sessionId)
46 samplesBuffer.push(sample);
47 });
48
49 setInterval(function() {
50 try {
51 sendInfo();
52 sendMetrics();
53 sendSamples();
54 }
55 catch(e) {
56 nt.error(e);
57 }
58 }, 1000);
59
60
61 // empty buffer if no sessionId for more than 30 sec
62 setInterval(function() {
63 try {
64 if(!nt.sessionId)
65 metricsBuffer = [];
66 }
67 catch(e) {
68 nt.error(e);
69 }
70 }, 30000);
71};
72
73
74var sendInfo = function() {
75 if(!nt.sessionId || !infoBuffer) return;
76
77 nt.agent.send({cmd: 'updateData', args: infoBuffer});
78 infoBuffer = undefined;
79};
80
81
82var sendMetrics = function() {
83 if(!nt.sessionId || metricsBuffer.length == 0) return;
84
85 metricsBuffer.forEach(function(metric) {
86 nt.agent.send({cmd: 'updateData', args: metric});
87 });
88
89 metricsBuffer = [];
90};
91
92
93var sendSamples = function() {
94 if(!nt.sessionId || samplesBuffer.length == 0) return;
95
96
97 // send slowest macro samples
98 var macroOps = samplesBuffer.filter(function(sample) {
99 return sample.isMacro;
100 });
101
102 var macroOps = macroOps.sort(function(a, b) {
103 return b._ms - a._ms;
104 });
105
106 for(var i = 0; i < (macroOps.length < 10 ? macroOps.length : 10); i++) {
107 nt.agent.send({cmd: 'updateData', args: macroOps[i]});
108 }
109
110
111 // send slowest non-macro samples
112 var simpleOps = samplesBuffer.filter(function(sample) {
113 return !sample.isMacro;
114 });
115
116 var simpleOps = simpleOps.sort(function(a, b) {
117 return b._ms - a._ms;
118 });
119
120 for(var i = 0; i < (simpleOps.length < 10 ? simpleOps.length : 10); i++) {
121 nt.agent.send({cmd: 'updateData', args: simpleOps[i]});
122 }
123
124
125 samplesBuffer = [];
126};
127
128