antimony

drive firefox from node.js
git clone https://wehaveforgeathome.hates.computer/antimony.git
Log | Files | Refs | Submodules | LICENSE

commit ecd909c2250d12de785c42cf800fbcadd8744a62
parent 154a89c6dc185b516d9d1c67843fbe5431fbae14
Author: Ryan Wolf <rwolf@borderstylo.com>
Date:   Wed, 15 Dec 2010 12:44:33 -0800

with a queue, antimony + firefox + proxatron looks like it can handle 3 / second

Diffstat:
Maddon/chrome/content/content.js | 18++----------------
Mclient.js | 29++++++++++++++++++++++++++++-
Mexample/twitter_comments.js | 8++++++--
Mrouter.js | 1-
Mserver.js | 9+++------
5 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/addon/chrome/content/content.js b/addon/chrome/content/content.js @@ -58,20 +58,12 @@ var runOnPage = function (url, script, callback) { }, false); }; -var messageQueue = []; - -var running = false; -var processNext = function () { - if (running || messageQueue.length === 0) { return; } - running = true; - var json = messageQueue.shift(); +new Poller('http://localhost:1234/', function (json) { var message = JSON.parse(json); var callback = function (data) { var xhr = new XMLHttpRequest(); xhr.open("POST", 'http://localhost:1235/browser/' + message.id, true); xhr.send(JSON.stringify({res: data})); - running = false; - processNext(); }; try { if (message.type == 'page') { @@ -83,12 +75,6 @@ var processNext = function () { } catch (e) { Cu.reportError(e); - running = false; - processNext(); + callback(e); } -}; - -new Poller('http://localhost:1234/', function (json) { - messageQueue.push(json); - processNext(); }); diff --git a/client.js b/client.js @@ -2,9 +2,14 @@ var request = require('request'); var Client = function (host) { this.host = host; + this.out = 0; + this.limit = 100; + this.pending = []; }; -Client.prototype.runOnPage = function (url, f, callback) { +Client.prototype._runOnPage = function (url, f, callback) { + var that = this; + this.out++; var data = { type: 'page', url: url, @@ -16,9 +21,31 @@ Client.prototype.runOnPage = function (url, f, callback) { body: JSON.stringify(data) }; request(params, function (error, response, body) { + if (error) { throw error; } var data = JSON.parse(body); callback(data.res); + that.out--; + that.handlePending(); }); }; +Client.prototype.handlePending = function () { + var that = this; + if (this.out >= this.limit) { return; } + if (this.pending.length === 0) { return; } + var remaining = this.limit - this.out; + var j = (remaining <= this.pending.length) ? remaining : this.pending.length; + for (var i = 0; i < j; i++) { + var next = this.pending.shift(); + this._runOnPage(next.url, next.f, next.callback); + } +}; + +Client.prototype.runOnPage = function (url, f, callback) { + if (this.out < this.limit) { + return this._runOnPage(url, f, callback); + } + this.pending.push({ url: url, f: f, callback: callback }); +}; + module.exports = Client; diff --git a/example/twitter_comments.js b/example/twitter_comments.js @@ -1,9 +1,12 @@ +var puts = require('sys').puts; + var Client = require('../client'); var client = new Client('localhost:1235'); +var responses = 0; -for (var i = 0; i < 100; i++) { +for (var i = 0; i < 500; i++) { client.runOnPage( 'http://www.twitter.com/writeonglass', function (callback) { @@ -12,7 +15,8 @@ for (var i = 0; i < 100; i++) { callback(c); }, function (data) { - console.log(data); + responses++; + puts(responses); } ); } diff --git a/router.js b/router.js @@ -31,7 +31,6 @@ exports.notFound = function (response) { exports.server = http.createServer(function (request, response) { var method = request.method.toLowerCase(); var path = url_parse(request.url).pathname.replace(/\/$/, ''); - console.log(method + ': ' + path); var matchingRoutes = routes[method].filter(function (route) { return route.regex.test(path); }); diff --git a/server.js b/server.js @@ -12,14 +12,13 @@ var forwardNextMessage = function () { var message = clientMessages.shift(); var res = clientResponses[message.id]; var json = message.json; - console.log('\tsending to push ' + json); pushRes.writeHead(200, {'Content-Type': 'application/json'}); pushRes.end(json); pushRes = null; + console.log('forwarded ' + message.id); }; http.createServer(function (req, res) { - console.log('push connected'); pushRes = res; forwardNextMessage(); }).listen(1234); @@ -35,28 +34,26 @@ router.post('client', function (req, res) { var data = JSON.parse(json); data.id = id; json = JSON.stringify(data); - console.log('\tsaving message (' + id + ')'); clientMessages.push({ id: id, json: json}); clientResponses[id] = res; + console.log('saved ' + id); forwardNextMessage(); }); }); router.post('browser/:id', function (req, res, id) { - console.log('post from browser (' + id + ')'); var json = ''; req.on('data', function (data) { json += data; }); req.on('end', function (data) { if (data) { json += data; } - console.log('\trecieved from browser: ' + json); - console.log('\tresponding to client'); var clientRes = clientResponses[id]; delete clientResponses[id]; clientRes.writeHead(200); clientRes.end(json); res.writeHead(204); res.end(); + console.log('responded to ' + id); }); });