Created
July 24, 2011 17:47
-
-
Save steelThread/1102873 to your computer and use it in GitHub Desktop.
Client codez
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
# | |
# Reads xml in chunks, parses to js obj. | |
# | |
net = require 'net' | |
sys = require 'sys' | |
xml2js = require 'xml2js' | |
start = new Date().getMilliseconds() | |
parser = new xml2js.Parser() | |
buffer = '' | |
count = 0 | |
endTag = '</MLB-event>' | |
socket = new net.Socket() | |
socket.setEncoding 'utf8' | |
socket.connect 8124, 'ec2-50-19-181-80.compute-1.amazonaws.com' | |
socket.on 'data', (chunk) -> | |
buffer += chunk | |
parseEvents() | |
parser.on 'end', -> count++ | |
parseEvents = -> | |
end = buffer.indexOf endTag | |
if end isnt -1 | |
end += endTag.length | |
parser.parseString buffer.slice(0, end) | |
buffer = buffer.substr end | |
parseEvents() | |
process.on 'exit', -> | |
console.log "took #{new Date().getMilliseconds() - start}ms" | |
console.log "received #{count} events" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting between 250 - 270ms to consume and parse all 1000 events.
$ coffee client.coffee
took 250ms
received 1000 events
$ coffee client.coffee
took 248ms
received 1000 events
$ coffee client.coffee
took 252ms
received 1000 events
$ coffee client.coffee
took 277ms
received 1000 events