Created
December 8, 2014 11:03
-
-
Save FREEZX/bbea4403f20f05c40ef6 to your computer and use it in GitHub Desktop.
Benchmark primus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var async = require('async'); | |
| var benchtest = require('./' + process.argv[2]); | |
| benchtest.prepareClient(parseInt(process.argv[4]), process.argv[3]); | |
| console.log(JSON.stringify({message: 'start'})); | |
| var sockets = parseInt(process.argv[5]); | |
| var messages = parseInt(process.argv[6]); | |
| var q = async.queue(function(task, callback){ | |
| benchtest.test(messages, callback); | |
| }, 50); | |
| for(var i=0; i<sockets; ++i){ | |
| q.push(); | |
| } | |
| q.drain = function() { | |
| console.log(JSON.stringify({message: 'end'})); | |
| process.exit(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var http = require('http'), | |
| Primus = require('primus'); | |
| var server, primus, Socket, PORT; | |
| exports.prepareServer = function(port){ | |
| PORT = port; | |
| server = http.createServer(); | |
| primus = new Primus(server, { transformer: process.argv[3] }); | |
| primus.on('connection', function connection(spark) { | |
| spark.on('data', function echo(data) { | |
| spark.write(data); | |
| }); | |
| }); | |
| server.listen(PORT); | |
| } | |
| exports.prepareClient = function(port, transformer){ | |
| PORT = port; | |
| Socket = Primus.createSocket({ transformer: transformer }); | |
| } | |
| exports.test = function(messages, done){ | |
| var socket; | |
| var responses = 0; | |
| var url = 'http://localhost:'+PORT; | |
| (function connect() { | |
| socket = new Socket(url); | |
| socket.on('open', function open() { | |
| for (i = 0; i < messages - responses; i++) socket.write('data'); | |
| }); | |
| socket.on('data', function message() { | |
| if (++responses === messages) socket.end(); | |
| }); | |
| socket.on('end', function close() { | |
| if (responses === messages) return done(); | |
| connect(); | |
| }); | |
| })(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var async = require('async'), | |
| microtime = require('microtime'), | |
| bunyan = require('bunyan'), | |
| usage = require('usage'), | |
| child_process = require('child_process'), | |
| spawn = child_process.spawn; | |
| var benchtest = require('./' + process.argv[2]); | |
| var pid = process.pid; | |
| var log = bunyan.createLogger({name: 'SockBench'}); | |
| var options = { keepHistory: true }; | |
| setInterval(function() { | |
| usage.lookup(pid, function(err, result) { | |
| log.info(result, 'stat'); | |
| }); | |
| }, 500); | |
| var PORT = 3000; | |
| benchtest.prepareServer(PORT); | |
| function performTest(sockets, messages, endCallback){ | |
| var clientRunner = spawn('node', ['client.js', process.argv[2], process.argv[3], PORT, sockets, messages]); | |
| var time; | |
| var interval; | |
| clientRunner.stdout.on('data', function (data) { | |
| data = JSON.parse(data); | |
| if(data.message === 'start'){ | |
| time = microtime.nowDouble(); | |
| } | |
| else{ | |
| interval = microtime.nowDouble()-time; | |
| } | |
| }); | |
| clientRunner.stderr.on('data', function (data) { | |
| console.error(data.toString()); | |
| }); | |
| clientRunner.on('exit', function(){ | |
| endCallback(interval); | |
| }); | |
| } | |
| var testFunctions = []; | |
| sockets = [1, 10, 100, 1000, 10000]; | |
| messages = [1, 10, 100]; | |
| testFunctions.push(function(callback){ | |
| log.info('Start'); | |
| callback(); | |
| }); | |
| // Adding realtime tests | |
| for(var i=0; i<messages.length; ++i){ | |
| for(var j=0; j<sockets.length; ++j){ | |
| (function(tests, conc){ | |
| testFunctions.push(function(callback){ | |
| performTest(sockets[conc], messages[tests], function(time){ | |
| log.info({interval:time, messages:messages[tests], sockets:sockets[conc]}, 'messages'); | |
| callback(); | |
| }); | |
| }); | |
| })(i, j); | |
| } | |
| } | |
| async.series( | |
| testFunctions, | |
| function(){ | |
| log.info('End'); | |
| process.exit(); | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment