Permalink
Browse files

Timer feature abstracted

  • Loading branch information...
1 parent dd51746 commit f268101a933a916613e06ac688c40c84f81a0bac @penartur committed May 4, 2012
Showing with 47 additions and 20 deletions.
  1. +39 −20 lib/benchmark.js
  2. +8 −0 lib/timers/datetimer.js
View
59 lib/benchmark.js
@@ -4,6 +4,21 @@ var request = require('request');
var WorkingQueue = require('capisce').WorkingQueue;
var http = require('http');
+var dateTimer = require('./timers/datetimer');
+
+var TIMERTYPES = {
+ DATETIMER: 'datetimer'
+}
+
+var getTimer = function (timerType) {
+ switch (timerType) {
+ case TIMERTYPES.DATETIMER:
+ return dateTimer;
+ default:
+ throw new Error("Unknown timer type " + timerType);
+ }
+}
+
var statistics = {
median: function (list) {
list.sort(function (a, b) { return (a - b); });
@@ -36,34 +51,22 @@ var statistics = {
//console.log(result);
return result;
}
-};
-
-var requestPage = function (url, expectedLength, callback) {
- var start = new Date().getTime(); //todo: use process.hrtime here
- request(url, function (error, response, body) {
- var end = new Date().getTime();
- var total = end - start;
- if (error) {
- callback(error, total);
- } else {
- if (body.length != expectedLength) {
- callback(new Error("Expected " + expectedLength + " bytes, got " + body.length + " bytes"), total);
- } else {
- callback(false, total);
- }
- }
- });
-};
+}
var Benchmark = function (engines, pages, options) {
this.engines = engines;
this.pages = pages;
this.options = {
- iterations: 9
+ iterations: 9,
+ timerType: TIMERTYPES.DATETIMER
};
if (options.iterations !== undefined) {
this.options.iterations = options.iterations;
}
+ if (options.timerType !== undefined) {
+ this.options.timerType = options.timerType;
+ }
+ this.timers = getTimer(this.options.timerType);
this.pagesExpectedLength = {};
for (var pageName in pages) {
@@ -98,8 +101,24 @@ BenchmarkContext.prototype.onDone = function () {
this.done();
}
+BenchmarkContext.prototype.requestPage = function (url, expectedLength, callback) {
+ var timer = this.benchmark.timers.start();
+ request(url, function (error, response, body) {
+ var elapsed = timer.elapsed();
+ if (error) {
+ callback(error, elapsed);
+ } else {
+ if (body.length != expectedLength) {
+ callback(new Error("Expected " + expectedLength + " bytes, got " + body.length + " bytes"), elapsed);
+ } else {
+ callback(false, elapsed);
+ }
+ }
+ });
+};
+
BenchmarkContext.prototype.runPage = function (engineName, pageName, over) {
- requestPage(
+ this.requestPage(
this.benchmark.engines[engineName] + this.benchmark.pages[pageName].url,
this.benchmark.pages[pageName].expectedLength[engineName],
(function (err, total) {
View
8 lib/timers/datetimer.js
@@ -0,0 +1,8 @@
+exports.start = function () {
+ var start = new Date().getTime();
+ return {
+ elapsed: function () {
+ return new Date().getTime() - start;
+ }
+ };
+};

0 comments on commit f268101

Please sign in to comment.