Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4d03d9c127
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 78 lines (50 sloc) 2.23 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#!/usr/bin/env coffee

connect = require 'connect'
fs = require 'fs'
sys = require 'sys'
program = require 'commander'

version = '0.0.4'
program
  .version(version)
  .option('-d, --debug', 'Show when flush happens.', false)
  .option('-c, --canned-response [file]', 'Existing file name to read a response from.')
  .option('-o, --output [file]', 'File name to output to.', 'webnull.log')
  .option('-i, --interval [seconds]', 'Flush interval.', 10)
  .option('-p, --port [number]', 'Port to listen on.', 4000)
  .parse(process.argv)

console.log("== web/null v#{version}. I eat your HTTP. ==")
console.log("* Running in debug mode.") if program.debug?
console.log("* Listening on port #{program.port}.")
console.log("* Flushing to #{program.output} every #{program.interval} seconds.")

canned_response = ''
if program.cannedResponse?
  canned_response = fs.readFileSync(program.cannedResponse, 'utf-8')
  console.log("* Response will be #{canned_response.length} bytes read from #{program.cannedResponse}.")




fd = fs.openSync(program.output, 'a', 0644)
process.addListener "exit", ()->fs.close(fd)


s = if program.debug then connect.createServer(connect.logger()) else connect.createServer()
s.listen(program.port)


data =
  hits: 0
  size: 0

total_data =
  hits: 0
  size: 0



write = (time, t_req_count, t_data_size, req_count, data_size, req_sec, data_avg)->
  console.log "#{time}\t#{t_req_count} req(total)\t#{t_data_size} bytes(total)\t#{req_count} reqs\t#{data_size} bytes\t#{req_sec} req(s)\t#{data_avg} bytes(avg)" if program.debug?
  fs.writeSync(fd, "#{time},#{t_req_count},#{t_data_size},#{req_count},#{data_size},#{req_sec},#{data_avg}\n", null, "utf-8")



flush =()->
  req_sec = data.hits/program.interval
  data_avg = if data.hits == 0 then 0 else data.size/data.hits
  total_data.hits += data.hits
  total_data.size += data.size
  write(Math.round(new Date().getTime()/1000.0), total_data.hits, total_data.size, data.hits, data.size, req_sec, data_avg)
  data = {hits:0, size:0}



null_resp = (req, res)->
  data.hits += 1
  req.on 'data', (chunk)->
    data.size += chunk.length
  req.on 'end', ()->
    res.end(canned_response)

s.use '/', null_resp



setInterval flush, program.interval * 1000

Something went wrong with that request. Please try again.