1 | 'use strict';
|
2 |
|
3 | var Measured = require('measured');
|
4 |
|
5 | module.exports = Stats;
|
6 |
|
7 | function Stats(experiment) {
|
8 | var stats = {
|
9 | requestsPerSecond: new Measured.Meter(),
|
10 | latencyNs: new Measured.Histogram(),
|
11 | requests: {},
|
12 | statusCodes: {},
|
13 | errors: {},
|
14 | verificationErrors: {}
|
15 | };
|
16 |
|
17 | stats.requestsPerSecond.unref();
|
18 |
|
19 | experiment.on('request', function(req) {
|
20 | var start = process.hrtime();
|
21 | stats.requestsPerSecond.mark();
|
22 |
|
23 | var key = req.method + ' ' + req.uri.href;
|
24 | var stat = stats.requests[key];
|
25 | if (! stat) {
|
26 | stat = stats.requests[key] = {
|
27 | latencyNs: new Measured.Histogram(),
|
28 | statusCodes: {},
|
29 | errors: {},
|
30 | verificationErrors: {}
|
31 | };
|
32 | }
|
33 |
|
34 | req.once('response', function(response) {
|
35 | var diff = process.hrtime(start);
|
36 | var ns = diff[0] * 1e9 + diff[1];
|
37 | stats.latencyNs.update(ns);
|
38 | stat.latencyNs.update(ns);
|
39 |
|
40 | var statusCodeStat = stat.statusCodes[response.statusCode];
|
41 | if (! statusCodeStat) {
|
42 | statusCodeStat =
|
43 | stat.statusCodes[response.statusCode] =
|
44 | new Measured.Counter();
|
45 | }
|
46 | statusCodeStat.inc();
|
47 |
|
48 | var statusCode = response.statusCode;
|
49 | var statusCodeStats = stats.statusCodes[statusCode];
|
50 | if (! statusCodeStats) {
|
51 | statusCodeStats =
|
52 | stats.statusCodes[statusCode] = new Measured.Counter();
|
53 | }
|
54 | statusCodeStats.inc();
|
55 | });
|
56 | });
|
57 |
|
58 | experiment.on('request-error', function(req, err) {
|
59 | var code = err.code || err.message;
|
60 |
|
61 | var stat = stats.errors[code];
|
62 | if (! stat) {
|
63 | stat = stats.errors[code] = new Measured.Counter();
|
64 | }
|
65 | stat.inc();
|
66 |
|
67 | var key = req.method + ' ' + req.uri.href;
|
68 | stat = stats.requests[key];
|
69 | if (! stat) {
|
70 | stat = stats.requests[key] = {
|
71 | latencyNs: new Measured.Histogram(),
|
72 | statusCodes: {},
|
73 | errors: {},
|
74 | verificationErrors: {}
|
75 | };
|
76 | }
|
77 |
|
78 | var errorStat = stat.errors[code];
|
79 | if (! errorStat) {
|
80 | errorStat = stat.errors[code] = new Measured.Counter();
|
81 | }
|
82 | errorStat.inc();
|
83 | });
|
84 |
|
85 | experiment.on('verify-error', function(err, req, res) {
|
86 | var code = err.code || err.message;
|
87 |
|
88 | var stat = stats.verificationErrors[code];
|
89 | if (! stat) {
|
90 | stat = stats.verificationErrors[code] = new Measured.Counter();
|
91 | }
|
92 | stat.inc();
|
93 |
|
94 | var key = req.method + ' ' + req.uri.href;
|
95 | stat = stats.requests[key];
|
96 | if (! stat) {
|
97 | stat = stats.requests[key] = {
|
98 | latencyNs: new Measured.Histogram(),
|
99 | statusCodes: {},
|
100 | errors: {},
|
101 | verificationErrors: {}
|
102 | };
|
103 | }
|
104 |
|
105 | var errorStat = stat.verificationErrors[code];
|
106 | if (! errorStat) {
|
107 | errorStat = stat.verificationErrors[code] = new Measured.Counter();
|
108 | }
|
109 | errorStat.inc();
|
110 | });
|
111 |
|
112 | function toJSON() {
|
113 | var error, count;
|
114 |
|
115 | var ret = {
|
116 | requestsPerSecond: stats.requestsPerSecond.toJSON(),
|
117 | latencyNs: stats.latencyNs.toJSON(),
|
118 | requests: {},
|
119 | statusCodes: {},
|
120 | errors: {},
|
121 | verificationErrors: {}
|
122 | };
|
123 |
|
124 | for(var req in stats.requests) {
|
125 |
|
126 | var statusCodes = {};
|
127 | for(var statusCode in stats.requests[req].statusCodes) {
|
128 | count = stats.requests[req].statusCodes[statusCode].toJSON();
|
129 | statusCodes[statusCode] = {
|
130 | count: count,
|
131 | percentage: count / stats.requests[req].latencyNs.toJSON().count
|
132 | };
|
133 | }
|
134 |
|
135 | var errors = {};
|
136 | for(error in stats.requests[req].errors) {
|
137 | count = stats.requests[req].errors[error].toJSON();
|
138 | errors[error] = {
|
139 | count: count,
|
140 | percentage: count / stats.requests[req].latencyNs.toJSON().count
|
141 | };
|
142 | }
|
143 |
|
144 | var verificationErrors = {};
|
145 | for(error in stats.requests[req].verificationErrors) {
|
146 | count = stats.requests[req].verificationErrors[error].toJSON();
|
147 | verificationErrors[error] = {
|
148 | count: count,
|
149 | percentage: count / stats.requests[req].latencyNs.toJSON().count
|
150 | };
|
151 | }
|
152 |
|
153 | ret.requests[req] = {
|
154 | latencyNs: stats.requests[req].latencyNs.toJSON(),
|
155 | statusCodes: statusCodes,
|
156 | errors: errors,
|
157 | verificationErrors: verificationErrors
|
158 | };
|
159 | }
|
160 |
|
161 | for(var code in stats.statusCodes) {
|
162 | count = stats.statusCodes[code].toJSON();
|
163 | ret.statusCodes[code] = {
|
164 | count: count,
|
165 | percentage: count / ret.requestsPerSecond.count
|
166 | };
|
167 | }
|
168 |
|
169 | for(error in stats.errors) {
|
170 | count = stats.errors[error].toJSON();
|
171 | ret.errors[error] = {
|
172 | count: count,
|
173 | percentage: count / ret.requestsPerSecond.count
|
174 | };
|
175 | }
|
176 |
|
177 | for(error in stats.verificationErrors) {
|
178 | count = stats.verificationErrors[error].toJSON();
|
179 | ret.errors[error] = {
|
180 | count: count,
|
181 | percentage: count / ret.requestsPerSecond.count
|
182 | };
|
183 | }
|
184 |
|
185 | return ret;
|
186 | }
|
187 |
|
188 | return {
|
189 | toJSON: toJSON
|
190 | };
|
191 | } |
\ | No newline at end of file |