Skip to content

Commit

Permalink
Switch to use Hoek.Bench. Closes hapijs#1165
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Nov 21, 2013
1 parent be35039 commit f7748ed
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 79 deletions.
1 change: 0 additions & 1 deletion lib/auth/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var internals = {};

exports = module.exports = internals.Scheme = function (server, options) {

Utils.assert(this.constructor === internals.Scheme, 'Scheme must be instantiated using new');
Utils.assert(options, 'Invalid options');
Utils.assert(options.scheme === 'basic', 'Wrong scheme');
Utils.assert(options.validateFunc, 'Missing required validateFunc method in configuration');
Expand Down
1 change: 0 additions & 1 deletion lib/auth/bewit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var internals = {};

exports = module.exports = internals.Scheme = function (server, options) {

Utils.assert(this.constructor === internals.Scheme, 'Scheme must be instantiated using new');
Utils.assert(options, 'Invalid options');
Utils.assert(options.scheme === 'bewit', 'Wrong scheme');
Utils.assert(options.getCredentialsFunc, 'Missing required getCredentialsFunc method in configuration');
Expand Down
1 change: 0 additions & 1 deletion lib/auth/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var internals = {};

exports = module.exports = internals.Scheme = function (server, options) {

Utils.assert(this.constructor === internals.Scheme, 'Scheme must be instantiated using new');
Utils.assert(server, 'Server is required');
Utils.assert(options, 'Invalid options');
Utils.assert(options.scheme === 'cookie', 'Wrong scheme');
Expand Down
1 change: 0 additions & 1 deletion lib/auth/hawk.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var internals = {};

exports = module.exports = internals.Scheme = function (server, options) {

Utils.assert(this.constructor === internals.Scheme, 'Scheme must be instantiated using new');
Utils.assert(options, 'Invalid options');
Utils.assert(options.scheme === 'hawk', 'Wrong scheme');
Utils.assert(options.getCredentialsFunc, 'Missing required getCredentialsFunc method in configuration');
Expand Down
2 changes: 0 additions & 2 deletions lib/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ var internals = {};

exports = module.exports = internals.Auth = function (server) {

Utils.assert(this.constructor === internals.Auth, 'Auth must be instantiated using new');

this.server = server;

// Load strategies
Expand Down
2 changes: 0 additions & 2 deletions lib/dtrace.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ var internals = {};

module.exports = internals.DTrace = function (name) {

Utils.assert(this.constructor === internals.DTrace, 'DTrace must be instantiated using new');

if (!internals.DTrace.isInstalled()) {
this.report = function () {};
return;
Expand Down
3 changes: 1 addition & 2 deletions lib/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ exports.read = function (request, next) {
var clientTimeoutId = null;

if (clientTimeout) {
clientTimeout -= (Date.now() - request._timestamp);

clientTimeout -= request._bench.elapsed();
clientTimeoutId = setTimeout(function () {

finish(Boom.clientTimeout('Client is taking too long to send request'));
Expand Down
13 changes: 7 additions & 6 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ var internals = {};

exports = module.exports = internals.Request = function (server, req, res, options) {

var now = Date.now(); // Take measurement as soon as possible
// Take measurement as soon as possible

this._bench = new Utils.Bench();
var now = Date.now();

Utils.assert(this.constructor === internals.Request, 'Request must be instantiated using new');
Utils.assert(server, 'server must be provided');
Utils.assert(req, 'req must be provided');
Utils.assert(res, 'res must be provided');
Expand Down Expand Up @@ -103,7 +105,6 @@ exports = module.exports = internals.Request = function (server, req, res, optio

// Private members

this._timestamp = now;
this._states = {}; // Appended to response states when setting response headers
this._logger = [];

Expand Down Expand Up @@ -278,7 +279,7 @@ internals.Request.prototype._execute = function () {

var serverTimeout = self.server.settings.timeout.server;
if (serverTimeout) {
serverTimeout -= (Date.now() - self._timestamp); // Calculate the timeout from when the request was constructed
serverTimeout -= self._bench.elapsed(); // Calculate the timeout from when the request was constructed
var timeoutReply = function () {

self._reply(Boom.serverTimeout());
Expand Down Expand Up @@ -433,7 +434,7 @@ internals.Request.bindPre = function (pre) {

Ext.runProtected(request, 'pre', next, function (enter, exit) {

var timer = new Utils.Timer();
var timer = new Utils.Bench();
var finalize = function (result) {

request._undecorateReply();
Expand Down Expand Up @@ -553,7 +554,7 @@ internals.Request.handler = function (request, next) {

Ext.runProtected(request, 'handler', callback, function (enter, exit) {

var timer = new Utils.Timer();
var timer = new Utils.Bench();

var isFinalized = false;
var finalize = function (response) {
Expand Down
2 changes: 0 additions & 2 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ exports = module.exports = internals.Route = function (options, server, env) {

var self = this;

Utils.assert(this.constructor === internals.Route, 'Route must be instantiated using new');

// Setup and validate route configuration

Utils.assert(options.handler || (options.config && options.config.handler), 'Missing or undefined handler', options);
Expand Down
4 changes: 2 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ internals.Server.prototype._start = function (callback) {
if (this.settings.load.sampleInterval) {
var loopSample = function () {

var start = Date.now();
var bench = new Utils.Bench();
var measure = function () {

// Event loop delay
Expand All @@ -309,7 +309,7 @@ internals.Server.prototype._start = function (callback) {
self.load.eventLoopDelaySamples.shift();
}

self.load.eventLoopDelaySamples.push(Date.now() - start - self.settings.load.sampleInterval);
self.load.eventLoopDelaySamples.push(bench.elapsed() - self.settings.load.sampleInterval);
self.load.eventLoopDelay = internals.avg(self.load.eventLoopDelaySamples);

// Heap used
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hapi",
"description": "HTTP Server framework",
"homepage": "http://hapijs.com",
"version": "1.19.4",
"version": "1.19.5",
"repository": {
"type": "git",
"url": "git://github.com/spumko/hapi"
Expand Down
6 changes: 3 additions & 3 deletions test/integration/clientTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Client Timeout', function () {

it('returns client error message when client request taking too long', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();
var options = {
hostname: '127.0.0.1',
port: _server.info.port,
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('Client Timeout', function () {

it('doesn\'t return a client error message when response is taking a long time to send', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();
var options = {
hostname: '127.0.0.1',
port: _server.info.port,
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('Client Timeout', function () {

it('client doesn\'t return an error', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();
var options = {
hostname: '127.0.0.1',
port: _server.info.port,
Expand Down
4 changes: 2 additions & 2 deletions test/integration/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('Server', function () {
server.listener.getConnections(function (err, count) {

expect(count).to.be.greaterThan(0);
var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();

server.stop({ timeout: 20 }, function () {

Expand Down Expand Up @@ -134,7 +134,7 @@ describe('Server', function () {
server.listener.getConnections(function (err, count) {

expect(count).to.be.greaterThan(0);
var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();

server.stop(function () {

Expand Down
12 changes: 6 additions & 6 deletions test/integration/serverTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('Server Timeout', function () {

it('returns server error message when server taking too long', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();

server.inject('/timeout', function (res) {

Expand Down Expand Up @@ -153,7 +153,7 @@ describe('Server Timeout', function () {

it('doesn\'t return an error response when server is slow but faster than timeout', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();
server.inject('/slow', function (res) {

expect(timer.elapsed()).to.be.at.least(29);
Expand All @@ -164,7 +164,7 @@ describe('Server Timeout', function () {

it('doesn\'t return an error when server is responding when the timeout occurs', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();

var options = {
hostname: '127.0.0.1',
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('Server Timeout', function () {

it('response timeouts aren\'t cached on subsequent requests', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();
server.inject('/timeoutcache', function (res1) {

expect(timer.elapsed()).to.be.at.least(49);
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('Server and Client timeouts', function () {

it('are returned when both client and server timeouts are the same and the client times out', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();
var options = {
hostname: '127.0.0.1',
port: server.info.port,
Expand Down Expand Up @@ -283,7 +283,7 @@ describe('Server and Client timeouts', function () {

it('initial long running requests don\'t prevent server timeouts from occuring on future requests', function (done) {

var timer = new Hapi.utils.Timer();
var timer = new Hapi.utils.Bench();
var options = {
hostname: '127.0.0.1',
port: server.info.port,
Expand Down
29 changes: 9 additions & 20 deletions test/unit/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ describe('Auth', function () {

describe('#constructor', function () {

it('throws an error when constructed without new', function (done) {

var fn = function () {

var auth = Auth();
};

expect(fn).to.throw('Auth must be instantiated using new');
done();
});

it('throws an error when constructed without options', function (done) {

var fn = function () {
Expand Down Expand Up @@ -97,7 +86,7 @@ describe('Auth', function () {
it('doesn\'t throw an error if strategies are defined and used', function (done) {

var request = {
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
mode: 'required',
Expand Down Expand Up @@ -199,7 +188,7 @@ describe('Auth', function () {
auth: {
credentials: {}
},
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
entity: 'any',
Expand Down Expand Up @@ -229,7 +218,7 @@ describe('Auth', function () {
auth: {
credentials: {}
},
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
strategies: ['default']
Expand Down Expand Up @@ -260,7 +249,7 @@ describe('Auth', function () {
user: 'test'
}
},
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
entity: 'user',
Expand Down Expand Up @@ -290,7 +279,7 @@ describe('Auth', function () {
auth: {
credentials: {}
},
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
entity: 'user',
Expand Down Expand Up @@ -323,7 +312,7 @@ describe('Auth', function () {
user: 'test'
}
},
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
entity: 'app',
Expand Down Expand Up @@ -356,7 +345,7 @@ describe('Auth', function () {
app: 'test'
}
},
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
entity: 'app',
Expand Down Expand Up @@ -388,7 +377,7 @@ describe('Auth', function () {
user: 'test'
}
},
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
entity: 'wrongEntity',
Expand Down Expand Up @@ -416,7 +405,7 @@ describe('Auth', function () {
};

var request = {
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
route: {
auth: {
mode: 'required',
Expand Down
12 changes: 6 additions & 6 deletions test/unit/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Payload', function () {

it('passes null to the callback when the method is not put, patch or post', function (done) {
var request = {
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
method: 'delete',
_route: new Route({ method: '*', path: '/', handler: function () { } }, server),
raw: {
Expand All @@ -64,7 +64,7 @@ describe('Payload', function () {
shotRequest('POST', '/', { 'content-type': 'application/json' }, '{ "item": "test" }', function (req, res) {

var request = {
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
method: 'post',
_route: new Route({ method: 'post', path: '/', handler: function () { } }, server),
raw: {
Expand All @@ -89,7 +89,7 @@ describe('Payload', function () {
shotRequest('POST', '/', { 'content-type': 'application/json' }, '{ this is just wrong }', function (req, res) {

var request = {
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
method: 'post',
_route: new Route({ method: 'post', path: '/', handler: function () { } }, server),
raw: {
Expand All @@ -115,7 +115,7 @@ describe('Payload', function () {
shotRequest('POST', '/', { 'content-type': 'application/x-www-form-urlencoded' }, 'item=test', function (req, res) {

var request = {
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
method: 'post',
_route: new Route({ method: 'post', path: '/', handler: function () { } }, server),
raw: {
Expand All @@ -140,7 +140,7 @@ describe('Payload', function () {
shotRequest('POST', '/', { 'content-type': 'application/json' }, '{ "key":"12345678901234567890123456789012345678901234567890" }', function (req, res) {

var request = {
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
method: 'post',
_route: new Route({ method: 'post', path: '/', handler: function () { } }, server),
raw: {
Expand All @@ -166,7 +166,7 @@ describe('Payload', function () {
shotRequest('POST', '/', { 'content-type': 'application/json', 'content-length': 62 }, '{ "key":"12345678901234567890123456789012345678901234567890" }', function (req, res) {

var request = {
_timestamp: Date.now(),
_bench: new Hapi.utils.Bench(),
method: 'post',
_route: new Route({ method: 'post', path: '/', handler: function () { } }, server),
raw: {
Expand Down
Loading

0 comments on commit f7748ed

Please sign in to comment.