Permalink
Browse files

Benchmark refactored to not depend on the main project

  • Loading branch information...
1 parent e68e1e4 commit dd51746113bcdf66ac618e38437d599e61ceb6fb @penartur committed May 4, 2012
Showing with 98 additions and 146 deletions.
  1. +98 −146 lib/benchmark.js
View
@@ -3,79 +3,6 @@
var request = require('request');
var WorkingQueue = require('capisce').WorkingQueue;
var http = require('http');
-var config = require('../config');
-
-var currentRequests = 0;
-
-var benchPage = function (url, expectedLength, callback) {
- var start = new Date().getTime(); //todo: use process.hrtime here
- currentRequests++;
- //console.log("Requesting page " + url + "; currently processing " + currentRequests + " requests");
- request(url, function (error, response, body) {
- currentRequests--;
- //console.log("Got response from " + url + "; remaining " + currentRequests + " requests");
- var end = new Date().getTime();
- var total = end - start;
- if (error) {
- //console.log("Got error while trying to connect to " + url);
- callback(error, total);
- } else {
- if (body.length != expectedLength) {
- callback(new Error("Expected " + expectedLength + " bytes, got " + body.length + " bytes"), total);
- } else {
- callback(false, total);
- }
- }
- });
-};
-
-var pages = {
- /*"emptyPage": {
- url: "/",
- weight: 1,
- expectedLength: {
- "legacy": 5425,
- "modern": 41
- }
- },*/
- "hook.ws": {
- url: "/memory/platform_qa/hook.ws",
- weight: 3,
- expectedLength: {
- "legacy": 1338,
- "modern": 5047
- }
- },
- "categories": {
- url: "/memory/platform_qa/api.ws?v=2.0&format=jsonp&skipls=0",
- weight: 3,
- expectedLength: {
- "legacy": 2772,
- "modern": 2483
- }
- },
- "laptops": {
- url: "/memory/platform_qa/api.ws?v=2.0&format=jsonp&key=ABA&skipls=0",
- weight: 1,
- expectedLength: {
- "legacy": 21616,
- "modern": 19589
- }
- },
- "search": {
- url: "/memory/platform_qa/api.ws?v=2.0&s=md,lg,ru&format=jsonp&query=acer",
- weight: 1,
- expectedLength: {
- "legacy": 116245,
- "modern": 76467
- }
- }
-};
-
-var engines = {
- "legacy": config.LegacySelectorsBase.absolute,
- "modern": config.Server.absolute
-};
var statistics = {
median: function (list) {
@@ -102,106 +29,131 @@ var statistics = {
var result = {
median: this.median(list),
mean: this.mean(list),
- max: this.max(list)
+ max: this.max(list),
+ num: list.length
};
//console.log(list);
//console.log(result);
return result;
}
};
-var displayResponseTimes = function (responseTimes) {
- for (var pageName in pages) {
- var averages = {};
- for (var engineName in engines) {
- averages[engineName] = statistics.all(responseTimes[pageName][engineName]);
+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);
+ }
}
- console.log("For page " + pageName);
- console.log(averages);
- }
-}
-
-var benchmark = function (simultaneousRequests, done) {
+ });
+};
- if (simultaneousRequests > 1024) {
- return;
+var Benchmark = function (engines, pages, options) {
+ this.engines = engines;
+ this.pages = pages;
+ this.options = {
+ iterations: 9
+ };
+ if (options.iterations !== undefined) {
+ this.options.iterations = options.iterations;
}
- console.log("");
- console.log("Measuring average reponse time for " + simultaneousRequests + " simultaneous requests");
+ this.pagesExpectedLength = {};
+ for (var pageName in pages) {
+ this.pagesExpectedLength[pageName] = {};
+ }
+}
- http.globalAgent.maxSockets = simultaneousRequests;
+//Instances of BenchmarkContext are one-time only
+var BenchmarkContext = function (benchmark, simultaneousRequests, done) {
+ this.benchmark = benchmark;
+ this.simultaneousRequests = simultaneousRequests;
+ this.done = done;
+
+ this.responseTimes = {};
+ for (var pageName in this.benchmark.pages) {
+ this.responseTimes[pageName] = {};
+ for (var engineName in this.benchmark.engines) {
+ this.responseTimes[pageName][engineName] = [];
+ }
+ }
+}
- var responseTimes = {};
- for (var pageName in pages) {
- responseTimes[pageName] = {};
- for (var engineName in engines) {
- responseTimes[pageName][engineName] = [];
+BenchmarkContext.prototype.onDone = function () {
+ for (var pageName in this.benchmark.pages) {
+ var averages = {};
+ for (var engineName in this.benchmark.engines) {
+ averages[engineName] = statistics.all(this.responseTimes[pageName][engineName]);
}
+ console.log("For page " + pageName);
+ console.log(averages);
}
+ this.done();
+}
- var processor = function (engineName, pageName, over) {
- benchPage(
- engines[engineName] + pages[pageName].url,
- pages[pageName].expectedLength[engineName],
- function (err, total) {
- if (err) {
- console.log(err);
- }
- responseTimes[pageName][engineName].push(total);
- over();
+BenchmarkContext.prototype.runPage = function (engineName, pageName, over) {
+ requestPage(
+ this.benchmark.engines[engineName] + this.benchmark.pages[pageName].url,
+ this.benchmark.pages[pageName].expectedLength[engineName],
+ (function (err, total) {
+ if (err) {
+ console.log(err);
}
- );
- }
+ this.responseTimes[pageName][engineName].push(total);
+ over();
+ }).bind(this)
+ );
+}
- var iterations = 9;
- var queues = {};
- for (var engineName in engines) {
- var queue = new WorkingQueue(simultaneousRequests);
- queue.hold();
- for (var i = 0; i < iterations * simultaneousRequests; i++) {
- for (var pageName in pages) {
- for (var j = 0; j < pages[pageName].weight; j++) {
- queue.perform(processor, engineName, pageName);
- }
+BenchmarkContext.prototype.runEngine = function (engineName, over) {
+ var queue = new WorkingQueue(this.simultaneousRequests);
+ queue.hold();
+ for (var i = 0; i < this.benchmark.options.iterations * this.simultaneousRequests; i++) {
+ for (var pageName in this.benchmark.pages) {
+ for (var j = 0; j < this.benchmark.pages[pageName].weight; j++) {
+ queue.perform(this.runPage.bind(this), engineName, pageName);
}
}
- queues[engineName] = queue;
}
+ queue.whenDone(over);
+ queue.go();
+}
+
+BenchmarkContext.prototype.run = function (done) {
+ console.log("");
+ console.log("Measuring average reponse time for " + this.simultaneousRequests + " simultaneous requests");
+
+ http.globalAgent.maxSockets = this.simultaneousRequests;
- var metaProcessor = function (engineName, over) {
- queues[engineName].whenDone(function () {
- over();
- });
- queues[engineName].go();
- }
var metaQueue = new WorkingQueue(1);
metaQueue.hold();
- for (var engineName in engines) {
- metaQueue.perform(metaProcessor, engineName);
+ for (var engineName in this.benchmark.engines) {
+ metaQueue.perform(this.runEngine.bind(this), engineName);
}
- metaQueue.whenDone(function () {
- displayResponseTimes(responseTimes);
- done();
- });
+ metaQueue.whenDone(this.onDone.bind(this));
metaQueue.go();
};
-function main() {
- benchmark(1, function () {
- benchmark(50, function () { //burning in
- var requests = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024];
- var queue = new WorkingQueue(1);
- queue.hold();
- for (var i in requests) {
- queue.perform(benchmark, requests[i]);
- }
- queue.whenDone(function () {
- console.log("done global");
- });
- queue.go();
- })
- });
+Benchmark.prototype.run = function (simultaneousRequests, done) {
+ var context = new BenchmarkContext(this, simultaneousRequests, done);
+ context.run(done);
+}
+
+Benchmark.prototype.runMultiple = function (simultaneousRequestsList, done) {
+ var queue = new WorkingQueue(1);
+ queue.hold();
+ for (var i in simultaneousRequestsList) {
+ queue.perform(this.run.bind(this), simultaneousRequestsList[i]);
+ }
+ queue.whenDone(done);
+ queue.go();
}
-main();
+module.exports = Benchmark;

0 comments on commit dd51746

Please sign in to comment.