Skip to content

Instantly share code, notes, and snippets.

@sbeam
Created December 2, 2014 19:12
Show Gist options
  • Save sbeam/f86a4640c6f977afc57c to your computer and use it in GitHub Desktop.
Save sbeam/f86a4640c6f977afc57c to your computer and use it in GitHub Desktop.
request = require 'request'
logger = require('winston')
lineReader = require('line-reader')
q = require 'q'
now = new Date
first_time = null
host = 'hydra-staging.herokuapp.com'
port = '80'
totals = {}
fetches = []
lineReader.eachLine('test4.log', (line, last)->
parts = line.split(' ')
return unless parts.length > 5
unless first_time
first_time = (new Date(Date.parse(parts[0]))).getTime()
time = (new Date(Date.parse(parts[0]))).getTime()
path = parts[4].replace(/^path=\"([^\"]+)\"/, '$1')
department = parts[5].replace(/^host=([^.]+).reviewed.com/, '$1')
offset = time - first_time
deferred = q.defer()
fetches.push q.delay(offset).then ->
start_time = (new Date).getTime()
request({ url: "http://#{host}:#{port}#{path}", headers: { "X-Reviewed-Category": department } }, (error, resp, body)->
if error
logger.error("error fetching '#{path}': #{error.message}")
else
delta = (new Date).getTime() - start_time
logger.info("fetched #{path} with status #{resp.statusCode}, got #{body.length} bytes in #{delta}ms")
deferred.resolve(bytes: body.length, time: delta, status: resp.statusCode)
)
)
q.all(fetches).done (data)->
console.log 'done!'
(totals[data.status] ||= {bytes: 0, time: 0})['bytes'] += data.bytes
totals[data.status]['time'] += data.time
console.log(totals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment