Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Timer feature abstracted

  • Loading branch information...
commit f268101a933a916613e06ac688c40c84f81a0bac 1 parent dd51746
penartur authored May 04, 2012
59  lib/benchmark.js
@@ -4,6 +4,21 @@ var request = require('request');
4 4
 var WorkingQueue = require('capisce').WorkingQueue;
5 5
 var http = require('http');
6 6
 
  7
+var dateTimer = require('./timers/datetimer');
  8
+
  9
+var TIMERTYPES = {
  10
+	DATETIMER: 'datetimer'
  11
+}
  12
+
  13
+var getTimer = function (timerType) {
  14
+	switch (timerType) {
  15
+		case TIMERTYPES.DATETIMER:
  16
+			return dateTimer;
  17
+		default:
  18
+			throw new Error("Unknown timer type " + timerType);
  19
+	}
  20
+}
  21
+
7 22
 var statistics = {
8 23
 	median: function (list) {
9 24
 		list.sort(function (a, b) { return (a - b); });
@@ -36,34 +51,22 @@ var statistics = {
36 51
 		//console.log(result);
37 52
 		return result;
38 53
 	}
39  
-};
40  
-
41  
-var requestPage = function (url, expectedLength, callback) {
42  
-	var start = new Date().getTime(); //todo: use process.hrtime here
43  
-	request(url, function (error, response, body) {
44  
-		var end = new Date().getTime();
45  
-		var total = end - start;
46  
-		if (error) {
47  
-			callback(error, total);
48  
-		} else {
49  
-			if (body.length != expectedLength) {
50  
-				callback(new Error("Expected " + expectedLength + " bytes, got " + body.length + " bytes"), total);
51  
-			} else {
52  
-				callback(false, total);
53  
-			}
54  
-		}
55  
-	});
56  
-};
  54
+}
57 55
 
58 56
 var Benchmark = function (engines, pages, options) {
59 57
 	this.engines = engines;
60 58
 	this.pages = pages;
61 59
 	this.options = {
62  
-		iterations: 9
  60
+		iterations: 9,
  61
+		timerType: TIMERTYPES.DATETIMER
63 62
 	};
64 63
 	if (options.iterations !== undefined) {
65 64
 		this.options.iterations = options.iterations;
66 65
 	}
  66
+	if (options.timerType !== undefined) {
  67
+		this.options.timerType = options.timerType;
  68
+	}
  69
+	this.timers = getTimer(this.options.timerType);
67 70
 
68 71
 	this.pagesExpectedLength = {};
69 72
 	for (var pageName in pages) {
@@ -98,8 +101,24 @@ BenchmarkContext.prototype.onDone = function () {
98 101
 	this.done();
99 102
 }
100 103
 
  104
+BenchmarkContext.prototype.requestPage = function (url, expectedLength, callback) {
  105
+	var timer = this.benchmark.timers.start();
  106
+	request(url, function (error, response, body) {
  107
+		var elapsed = timer.elapsed();
  108
+		if (error) {
  109
+			callback(error, elapsed);
  110
+		} else {
  111
+			if (body.length != expectedLength) {
  112
+				callback(new Error("Expected " + expectedLength + " bytes, got " + body.length + " bytes"), elapsed);
  113
+			} else {
  114
+				callback(false, elapsed);
  115
+			}
  116
+		}
  117
+	});
  118
+};
  119
+
101 120
 BenchmarkContext.prototype.runPage = function (engineName, pageName, over) {
102  
-	requestPage(
  121
+	this.requestPage(
103 122
 		this.benchmark.engines[engineName] + this.benchmark.pages[pageName].url,
104 123
 		this.benchmark.pages[pageName].expectedLength[engineName],
105 124
 		(function (err, total) {
8  lib/timers/datetimer.js
... ...
@@ -0,0 +1,8 @@
  1
+exports.start = function () {
  2
+	var start = new Date().getTime();
  3
+	return {
  4
+		elapsed: function () {
  5
+			return new Date().getTime() - start;
  6
+		}
  7
+	};
  8
+};

0 notes on commit f268101

Please sign in to comment.
Something went wrong with that request. Please try again.