Skip to content

Commit b917ff6

Browse files
committed
Add category and type stats property
1 parent ad22237 commit b917ff6

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function getBytes(res) {
4040
function requestStats() {
4141
const timePoints = {};
4242
const result = {
43-
type: 'request',
43+
category: 'request',
4444
};
4545
statsData.requesting += 1;
4646
let done = false;
@@ -78,13 +78,16 @@ function requestStats() {
7878
timing.transfer = timePoints.close - timePoints.data;
7979
}
8080
timing.all = timePoints.close - timePoints.start;
81+
const status = (this.res && this.res.statusCode) || 0;
8182
Object.assign(result, {
8283
requesting: statsData.requesting,
8384
method: this.method,
8485
/* eslint no-underscore-dangle:0 */
8586
host: this._headers && this._headers.host,
8687
url: this.path,
87-
status: (this.res && this.res.statusCode) || -1,
88+
/* eslint no-bitwise:0 */
89+
type: status / 100 | 0,
90+
status,
8891
bytes: getBytes(this.res),
8992
timing,
9093
});
@@ -128,13 +131,16 @@ function responseStats() {
128131
const socketUse = Date.now() - startedAt;
129132
socket.once('close', () => {
130133
statsData.responsing -= 1;
134+
const status = this.statusCode || 0;
131135
const result = {
132-
type: 'response',
136+
category: 'response',
133137
method: this.req.method,
134138
timing: {
135139
all: Date.now() - startedAt,
136140
socket: socketUse,
137141
},
142+
/* eslint no-bitwise:0 */
143+
type: status / 100 | 0,
138144
status: this.statusCode,
139145
url: this.req && this.req.originalUrl,
140146
bytes: getBytes(this.req),

test/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ describe('http-performance', () => {
1111
it('should get request performance', function (done) {
1212
this.timeout(10 * 1000);
1313
httpPerf.once('stats', (stats) => {
14-
assert.equal(stats.type, 'request');
14+
assert.equal(stats.category, 'request');
1515
assert(stats.dns.ip);
1616
assert.equal(stats.dns.addressType, '4');
1717
assert.equal(stats.url, '/');
1818
assert.equal(stats.host, 'www.baidu.com');
1919
assert.equal(stats.method, 'GET');
2020
assert.equal(stats.status, 200);
21+
assert.equal(stats.type, 2);
2122
assert(stats.bytes);
2223
assert(stats.timing);
2324
done();
@@ -88,11 +89,12 @@ describe('http-performance', () => {
8889
const server = app.listen();
8990

9091
httpPerf.on('stats', (stats) => {
91-
if (stats.type !== 'response') {
92+
if (stats.category !== 'response') {
9293
return;
9394
}
9495
assert.equal(stats.method, 'GET');
9596
assert.equal(stats.status, 200);
97+
assert.equal(stats.type, 2);
9698
server.close();
9799
done();
98100
});

0 commit comments

Comments
 (0)