Skip to content

Commit

Permalink
feat(TimeLoggerInterceptor): collect numberOfRequests,numberOfFailedR…
Browse files Browse the repository at this point in the history
…equests,maxRequestProcessingTime and totalRequestProcessingTime
  • Loading branch information
arlac77 committed Jan 13, 2016
1 parent d4c4fe3 commit 9e4fa2b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
38 changes: 36 additions & 2 deletions lib/TimeLoggerInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,51 @@ class TimeLoggerInterceptor extends Interceptor {
return TimeLoggerInterceptor.type;
}

reset() {
this._numberOfRequests = 0;
this._numberOfFailedRequests = 0;
this._maxRequestProcessingTime = 0;
this._totalRequestProcessingTime = 0;
}

get numberOfRequests() {
return this._numberOfRequests;
}

get numberOfFailedRequests() {
return this._numberOfFailedRequests;
}

get maxRequestProcessingTime() {
return this._maxRequestProcessingTime;
}

get totalRequestProcessingTime() {
return this._totalRequestProcessingTime;
}

/**
* Logs the time the requests takes
*/
receive(request, oldRequest) {
const logger = this.logger;

this._numberOfRequests += 1;

const start = new Date();
const response = this.connected.receive(request, oldRequest);
return response.then(f => {
const now = new Date();
logger.debug(`took ${now - start} ms for ${request}`);
const pt = now - start;
this._totalRequestProcessingTime += pt;

if (pt > this._maxRequestProcessingTime) {
this._maxRequestProcessingTime = pt;
}
this.logger.debug(`took ${pt} ms for ${request}`);
return f;
}, r => {
this._numberOfFailedRequests += 1;
return r;
});
}
}
Expand Down
27 changes: 23 additions & 4 deletions tests/interceptor_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ const chai = require('chai'),
TimeoutInterceptor = require('../index').TimeoutInterceptor,
LimitingInterceptor = require('../index').LimitingInterceptor;

const logger = {};
llm.defineLogLevelProperties(logger, llm.defaultLogLevels, llm.defaultLogLevels);
const logger = {
debug(a) {
console.log(a);
}
};
//llm.defineLogLevelProperties(logger, llm.defaultLogLevels, llm.defaultLogLevels);


function DelayedResponse(request) {
Expand All @@ -41,7 +45,6 @@ describe('interceptors', () => {
const ep = dummyEndpoint('ep');

mochaInterceptorTest(Interceptor, ep, {}, "none", (itc, withConfig) => {

if (!withConfig) return;

itc.connected = dummyEndpoint('ep');
Expand All @@ -52,7 +55,23 @@ describe('interceptors', () => {
it('passing request', done => itc.receive(1).then(fullfilled => done()).catch(done));
});

mochaInterceptorTest(TimeLoggerInterceptor, ep, {}, "logger-request-time", itc => {});
mochaInterceptorTest(TimeLoggerInterceptor, ep, {}, "logger-request-time", (itc, withConfig) => {
if (!withConfig) return;

itc.connected = dummyEndpoint('ep');

// request value is the timeout
itc.connected.receive = DelayedResponse;

describe('count requests', () => {
it('passing request', done => itc.receive(10).then(fullfilled => {
assert.equal(itc.numberOfRequests, 1);
assert.closeTo(itc.maxRequestProcessingTime, 10, 10);
assert.closeTo(itc.totalRequestProcessingTime, 10, 10);
done();
}).catch(done));
});
});


const REQUEST_LIMIT = 2;
Expand Down

0 comments on commit 9e4fa2b

Please sign in to comment.