Skip to content

Commit

Permalink
U: add multiple socket.io proxy testing. It seems that, if run io.con…
Browse files Browse the repository at this point in the history
…nect(sameurl) multiple times, we will get the same socket instance. So, i guess on one client, socket.io client only has a tcp socket.
  • Loading branch information
fool2fish committed Jul 25, 2013
1 parent e8f0627 commit 5046772
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
32 changes: 26 additions & 6 deletions docs/http-and-socket-performance/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ app.use(express.static(path.join(__dirname, 'static')))

app.listen(port, host, function() {
console.info('Start client server: ' + host + ':' + port)
var socket = io.connect('http://' + capture)
var socket = io.connect('http://' + capture + '/stable')

socket.on('connect', function() {
socket.emit('start', {
Expand All @@ -29,23 +29,43 @@ app.listen(port, host, function() {
})
})

socket.on('proxyReq', function(file) {
socket.on('proxyReq', function(data) {
var type = data.type
var path = data.path

request(
'http://' + host + ':' + port + '/' + file,
'http://' + host + ':' + port + '/' + path,
function (err, res, body) {
socket.emit('proxyRes', {
path : file,
var res = {
path : path,
statusCode : res.statusCode,
header : res.header,
body : body
})
}

if (type === 'socket.io') {
socket.emit('proxyRes', res)

} else if (type === 'multiple socket.io') {
console.log('receive request:', type, ',', path)
var temp = io.connect('http://' + capture + '/temp')
temp.on('connect', function() {
console.log('temp socket connect:', type, ',', path)
temp.emit('proxyRes', res)
})
temp.on('disconnect', function() {
console.log('temp socket disconnect:', type, ',', path)
})
}

}
)
})

socket.on('disconnect', function() {
process.exit(0)
})

})


64 changes: 44 additions & 20 deletions docs/http-and-socket-performance/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var common = require('totoro-common')
var host = common.getExternalIpAddress()
var port = 9997
var client
var iSocket // stable socket

var rules = [
/*['file size', 'concurrency', 'proxy type']*/
Expand All @@ -24,6 +25,8 @@ var rules = [
['small', 'high', 'socket.io'],
['large', 'high', 'http'],
['large', 'high', 'socket.io'],
['small', 'high', 'multiple socket.io'],
['large', 'high', 'multiple socket.io']
]
var pos = 0 // current rule position
var amount // concurrency counter
Expand All @@ -35,23 +38,41 @@ var io = socketio.listen(server, {
log : false
})

server.listen(port, host, function(socket) {
server.listen(port, host, function() {
console.log('Start server: ' + host + ':' + port)
})

io.sockets.on('connection', function (socket) {
socket.on('start', function(data){
io.sockets.on('connection', function(socket) {
socket.on('message', function(data) {
console.log('---msg----')
})
})

io.of('/stable').on('connection', function(socket) {
iSocket = socket

socket.on('start', function(data) {
client = data
proxyReq(socket)
proxyReq()
})

socket.on('proxyRes', function(data) {
cb(data.path, socket)
console.timeEnd(data.path)
cb()
})
})

io.of('/temp').on('connection', function(socket) {
socket.on('proxyRes', function(data) {
socket.disconnect()
console.log('temp socket:', socket.id, 'get proxy response')
console.timeEnd(data.path)
cb()
})
})


function proxyReq(socket) {
function proxyReq() {
var host = client.host
var port = client.port

Expand All @@ -60,32 +81,35 @@ function proxyReq(socket) {
var concurrency = rule[1]
var type = rule[2]

var files = getFiles(size, concurrency)
amount = files.length
rule = rule.join(':')
var paths = getPaths(size, concurrency)
amount = paths.length

rule = rule.join(':')
console.log('\n== ' + rule.toUpperCase() + ' START ==')
console.time(rule)

files.forEach(function(file) {
console.time(file)
paths.forEach(function(path) {
console.time(path)

if (type === 'http') {
request(
'http://' + host + ':' + port + '/' + file,
'http://' + host + ':' + port + '/' + path,
function(err, res, body) {
cb(file, socket)
console.timeEnd(path)
cb()
}
)

} else if (type === 'socket.io') {
socket.emit('proxyReq', file)
} else {
iSocket.emit('proxyReq', {
path : path,
type : type
})
}
})
}

function cb(file, socket){
console.timeEnd(file)
function cb(){
amount--
if (amount === 0) {
var rule = rules[pos]
Expand All @@ -96,14 +120,14 @@ function cb(file, socket){
var len = rules.length
if (pos < len - 1) {
pos++
proxyReq(socket)
proxyReq()
} else {
process.exit(0)
}
}
}

function getFiles(size, concurrency) {
function getPaths(size, concurrency) {
if (size === 'small') {
if (concurrency === 'low') {
return ['simple.html']
Expand All @@ -118,7 +142,7 @@ function getFiles(size, concurrency) {
'simple1.js',
'simple2.js',
'simple3.js',
'simple4.js',
'simple4.js'
]
}
} else {
Expand Down

0 comments on commit 5046772

Please sign in to comment.