From a38bb00cbd13fe10f71b4a3b2a86ddba2e21161c Mon Sep 17 00:00:00 2001 From: penartur Date: Sat, 5 May 2012 12:57:24 +0400 Subject: [PATCH] Autolint compliance --- autolint.js | 12 +++++++ lib/benchmark-context.js | 73 +++++++++++++++++++++++---------------- lib/benchmark.js | 50 +++++++++++++++------------ lib/statistics-display.js | 26 +++++++++----- lib/statistics.js | 62 +++++++++++++++++++-------------- lib/timers/datetimer.js | 4 ++- 6 files changed, 139 insertions(+), 88 deletions(-) create mode 100644 autolint.js diff --git a/autolint.js b/autolint.js new file mode 100644 index 0000000..12be480 --- /dev/null +++ b/autolint.js @@ -0,0 +1,12 @@ +module.exports = { + paths: [ "./**/*.js" ], + linter: "jslint", + linterOptions: { + maxlen: 120, + node: true, + devel: true, + forin: true, + plusplus: true + }, + excludes: ["node_modules"] +}; diff --git a/lib/benchmark-context.js b/lib/benchmark-context.js index 8675017..64f4e7d 100644 --- a/lib/benchmark-context.js +++ b/lib/benchmark-context.js @@ -1,4 +1,4 @@ -"use strict"; +"use strict"; var request = require('request'); var WorkingQueue = require('capisce').WorkingQueue; @@ -9,30 +9,38 @@ var statistics = require('./statistics'); //Instances of BenchmarkContext are one-time only var BenchmarkContext = function (benchmark, simultaneousRequests, done) { + var pageName, + engineName; + this.benchmark = benchmark; this.simultaneousRequests = simultaneousRequests; this.done = done; - if (!(simultaneousRequests >= 1)) { + if (typeof simultaneousRequests !== "number") { + throw new Error("simultaneousRequests must be an integer; " + (typeof simultaneousRequests) + " passed"); + } + if (simultaneousRequests < 1) { throw new Error("simultaneousRequests must be at least 1; " + simultaneousRequests + " passed"); } this.responseTimes = {}; - for (var pageName in this.benchmark.pages) { + for (pageName in this.benchmark.pages) { this.responseTimes[pageName] = {}; - for (var engineName in this.benchmark.engines) { + for (engineName in this.benchmark.engines) { this.responseTimes[pageName][engineName] = []; } } this.totals = {}; -} +}; BenchmarkContext.prototype.onDone = function () { - var perPage = {}; - for (var pageName in this.benchmark.pages) { + var pageName, + engineName, + perPage = {}; + for (pageName in this.benchmark.pages) { perPage[pageName] = {}; - for (var engineName in this.benchmark.engines) { + for (engineName in this.benchmark.engines) { perPage[pageName][engineName] = statistics.all(this.responseTimes[pageName][engineName]); } } @@ -40,7 +48,7 @@ BenchmarkContext.prototype.onDone = function () { perPage: perPage, totals: this.totals }); -} +}; BenchmarkContext.prototype.requestPage = function (url, expectedLength, callback) { var timer = this.benchmark.timers.start(); @@ -49,7 +57,7 @@ BenchmarkContext.prototype.requestPage = function (url, expectedLength, callback if (error) { callback(new Error("An error occured while trying to process " + url + ": " + error), elapsed); } else { - if (body.length != expectedLength) { + if (body.length !== expectedLength) { callback(new Error("Expected " + expectedLength + " bytes, got " + body.length + " bytes"), elapsed); } else { callback(false, elapsed); @@ -62,52 +70,59 @@ BenchmarkContext.prototype.runPage = function (engineName, pageName, over) { this.requestPage( this.benchmark.engines[engineName] + this.benchmark.pages[pageName].url, this.benchmark.pagesExpectedLength[pageName][engineName], - (function (err, total) { + function (err, total) { if (err) { console.log(err); } else { this.responseTimes[pageName][engineName].push(total); } over(); - }).bind(this) + }.bind(this) ); -} +}; BenchmarkContext.prototype.runEngine = function (engineName, over) { - var tasks = 0; - //console.log("runEngine"); - var queue = new WorkingQueue(this.simultaneousRequests); + var i, + pageName, + j, + timer, + tasks = 0, + 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++) { + for (i = 0; i < this.benchmark.options.iterations * this.simultaneousRequests; i++) { + for (pageName in this.benchmark.pages) { + for (j = 0; j < this.benchmark.pages[pageName].weight; j++) { queue.perform(this.runPage.bind(this), engineName, pageName); tasks++; } } } - var timer = this.benchmark.timers.start(); - queue.whenDone((function () { + timer = this.benchmark.timers.start(); + queue.whenDone(function () { + var timeout; this.totals[engineName] = { time: timer.elapsed(), tasks: tasks }; - var timeout = 0; - if (os.platform() == 'win32') { + + timeout = 0; + if (os.platform() === 'win32') { timeout = this.simultaneousRequests * 2000; //to workaround ENOBUFS } - //console.log("done, waiting " + timeout + "ms"); setTimeout(over, timeout); - }).bind(this)); + }.bind(this)); queue.go(); -} +}; BenchmarkContext.prototype.run = function () { + var metaQueue, + engineName; - http.globalAgent.maxSockets = this.simultaneousRequests; //TODO: replace this global state changing with something else - var metaQueue = new WorkingQueue(1); + //TODO: replace this global state changing with something else + http.globalAgent.maxSockets = this.simultaneousRequests; + metaQueue = new WorkingQueue(1); metaQueue.hold(); - for (var engineName in this.benchmark.engines) { + for (engineName in this.benchmark.engines) { metaQueue.perform(this.runEngine.bind(this), engineName); } metaQueue.whenDone(this.onDone.bind(this)); diff --git a/lib/benchmark.js b/lib/benchmark.js index 9b30399..8d3ff31 100644 --- a/lib/benchmark.js +++ b/lib/benchmark.js @@ -9,16 +9,16 @@ var statisticsDisplay = require('./statistics-display'); var TIMERTYPES = { DATETIMER: 'datetimer' -} +}; var getTimer = function (timerType) { switch (timerType) { - case TIMERTYPES.DATETIMER: - return dateTimer; - default: - throw new Error("Unknown timer type " + timerType); + case TIMERTYPES.DATETIMER: + return dateTimer; + default: + throw new Error("Unknown timer type " + timerType); } -} +}; var Benchmark = function (engines, pages, options) { this.engines = engines; @@ -34,64 +34,68 @@ var Benchmark = function (engines, pages, options) { this.options.timerType = options.timerType; } this.timers = getTimer(this.options.timerType); -} +}; Benchmark.prototype.fillExpectedLength = function (pageName, engineName, over) { var url = this.engines[engineName] + this.pages[pageName].url; - request(url, (function (error, response, body) { + request(url, function (error, response, body) { if (error) { throw new Error("An error occured while trying to process " + url + ": " + error); } else { this.pagesExpectedLength[pageName][engineName] = body.length; over(); } - }).bind(this)); -} + }.bind(this)); +}; Benchmark.prototype.init = function (done) { + var pageName, + queue, + engineName; if (this.pagesExpectedLength) { process.nextTick(done); } else { this.pagesExpectedLength = {}; - for (var pageName in this.pages) { + for (pageName in this.pages) { this.pagesExpectedLength[pageName] = {}; } - var queue = new WorkingQueue(1); + queue = new WorkingQueue(1); queue.whenDone(done); - for (var pageName in this.pages) { - for (var engineName in this.engines) { + for (pageName in this.pages) { + for (engineName in this.engines) { queue.perform(this.fillExpectedLength.bind(this), pageName, engineName); } } queue.go(); } -} +}; Benchmark.prototype.doRun = function (simultaneousRequests, callback, over) { - this.init((function () { + this.init(function () { var context = new BenchmarkContext(this, simultaneousRequests, function (simultaneousRequests, stats) { callback(simultaneousRequests, stats); over(); }); - context.run() - }).bind(this)); -} + context.run(); + }.bind(this)); +}; //callback is function(simultaneousRequests, stats) Benchmark.prototype.run = function (simultaneousRequests, callback) { this.doRun(simultaneousRequests, callback, function () { }); -} +}; //callback is function(simultaneousRequests, stats) Benchmark.prototype.runMultiple = function (simultaneousRequestsList, callback, done) { - var queue = new WorkingQueue(1); + var i, + queue = new WorkingQueue(1); queue.hold(); - for (var i in simultaneousRequestsList) { + for (i = 0; i < simultaneousRequestsList.length; i++) { queue.perform(this.doRun.bind(this), simultaneousRequestsList[i], callback); } queue.whenDone(done); queue.go(); -} +}; Benchmark.statisticsDisplay = statisticsDisplay; diff --git a/lib/statistics-display.js b/lib/statistics-display.js index c03e808..562634e 100644 --- a/lib/statistics-display.js +++ b/lib/statistics-display.js @@ -1,14 +1,22 @@ -"use strict"; +"use strict"; exports.showCliTable = function (simultaneousRequests, stats) { - var Table = require('cli-table'); + var table, + engineName, + pageName, + entry, + Table = require('cli-table'); + console.log("Statistics for " + simultaneousRequests + " simultaneous requests"); - for (var engineName in stats.totals) { - console.log("Processing " + engineName + " took " + stats.totals[engineName].time + " (" + (stats.totals[engineName].time / stats.totals[engineName].tasks) + " per request)"); + for (engineName in stats.totals) { + console.log( + "Processing " + engineName + " took " + stats.totals[engineName].time + + " (" + (stats.totals[engineName].time / stats.totals[engineName].tasks) + " per request)" + ); } - for (var pageName in stats.perPage) { + for (pageName in stats.perPage) { console.log(pageName); - var table = new Table({ + table = new Table({ head: [ 'Engine', 'Trimmed mean', @@ -43,8 +51,8 @@ exports.showCliTable = function (simultaneousRequests, stats) { 'right' ] }); - for (var engineName in stats.perPage[pageName]) { - var entry = stats.perPage[pageName][engineName]; + for (engineName in stats.perPage[pageName]) { + entry = stats.perPage[pageName][engineName]; table.push([ engineName, entry.trimmedMean, @@ -60,4 +68,4 @@ exports.showCliTable = function (simultaneousRequests, stats) { console.log(table.toString()); console.log(); } -} +}; diff --git a/lib/statistics.js b/lib/statistics.js index ccbbc57..b557001 100644 --- a/lib/statistics.js +++ b/lib/statistics.js @@ -1,9 +1,10 @@ -"use strict"; +"use strict"; var internals = { temporalDisrepancy: function (list) { - var sliceLength = Math.min(list.length / 2, 8); - return Math.round(100 * this.trimmedMean(list.slice(-sliceLength)) / this.trimmedMean(list.slice(0, sliceLength))) / 100; + var sliceLength = Math.min(list.length / 2, 8), + computed = this.trimmedMean(list.slice(-sliceLength)) / this.trimmedMean(list.slice(0, sliceLength)); + return Math.round(100 * computed) / 100; //rounding to the percents }, sort: function (list) { if (!list.sorted) { @@ -13,20 +14,19 @@ var internals = { }, median: function (list) { this.sort(list); - if (list.length % 2 == 0) { + if (list.length % 2 === 0) { return Math.round((list[list.length / 2 - 1] + list[list.length / 2]) / 2); } else { return list[(list.length - 1) / 2]; } }, mean: function (list) { - var total = 0; - var num = 0; - for (var i in list) { + var i, + total = 0; + for (i = 0; i < list.length; i++) { total += list[i]; - num++; } - return Math.round(total / num); + return Math.round(total / list.length); }, trimmedMean: function (list) { if (list.length < 4) { @@ -41,24 +41,34 @@ var internals = { }; exports.all = function (list) { - var temporalDisrepancy = internals.temporalDisrepancy(list); - var mean = internals.mean(list); - var max = internals.max(list); - var num = list.length; + var temporalDisrepancy, + mean, + max, + num, + median, + trimmedMean, + top10, + bottom10, + result; + + temporalDisrepancy = internals.temporalDisrepancy(list); + mean = internals.mean(list); + max = internals.max(list); + num = list.length; internals.sort(list); - var median = internals.median(list); - var trimmedMean = internals.trimmedMean(list); - var top10 = internals.mean(list.slice(0, list.length / 10)); - var bottom10 = internals.mean(list.slice(-list.length / 10)); - var result = { - trimmedMean: trimmedMean - , median: median - , temporalDisrepancy: temporalDisrepancy - , mean: mean - , max: max - , num: num - , top10: top10 - , bottom10: bottom10 + median = internals.median(list); + trimmedMean = internals.trimmedMean(list); + top10 = internals.mean(list.slice(0, list.length / 10)); + bottom10 = internals.mean(list.slice(-list.length / 10)); + result = { + trimmedMean: trimmedMean, + median: median, + temporalDisrepancy: temporalDisrepancy, + mean: mean, + max: max, + num: num, + top10: top10, + bottom10: bottom10 }; //console.log(result); return result; diff --git a/lib/timers/datetimer.js b/lib/timers/datetimer.js index 3d28a0f..8367de1 100644 --- a/lib/timers/datetimer.js +++ b/lib/timers/datetimer.js @@ -1,4 +1,6 @@ -exports.start = function () { +"use strict"; + +exports.start = function () { var start = new Date().getTime(); return { elapsed: function () {