Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 78 lines (50 sloc) 2.23 kb
37a701a @jondot npm pacakge, shebangs
authored
1 #!/usr/bin/env coffee
2
fcf721e @jondot initial import
authored
3 connect = require 'connect'
4 fs = require 'fs'
5 sys = require 'sys'
6 program = require 'commander'
7
37a701a @jondot npm pacakge, shebangs
authored
8 version = '0.0.4'
fcf721e @jondot initial import
authored
9 program
10 .version(version)
11 .option('-d, --debug', 'Show when flush happens.', false)
12 .option('-c, --canned-response [file]', 'Existing file name to read a response from.')
13 .option('-o, --output [file]', 'File name to output to.', 'webnull.log')
14 .option('-i, --interval [seconds]', 'Flush interval.', 10)
cdddb58 @jondot fixing typos
authored
15 .option('-p, --port [number]', 'Port to listen on.', 4000)
fcf721e @jondot initial import
authored
16 .parse(process.argv)
17
18 console.log("== web/null v#{version}. I eat your HTTP. ==")
19 console.log("* Running in debug mode.") if program.debug?
20 console.log("* Listening on port #{program.port}.")
21 console.log("* Flushing to #{program.output} every #{program.interval} seconds.")
22
23 canned_response = ''
24 if program.cannedResponse?
25 canned_response = fs.readFileSync(program.cannedResponse, 'utf-8')
26 console.log("* Response will be #{canned_response.length} bytes read from #{program.cannedResponse}.")
27
28
29
30
31 fd = fs.openSync(program.output, 'a', 0644)
32 process.addListener "exit", ()->fs.close(fd)
33
34
35 s = if program.debug then connect.createServer(connect.logger()) else connect.createServer()
36 s.listen(program.port)
37
38
39 data =
40 hits: 0
41 size: 0
42
43 total_data =
44 hits: 0
45 size: 0
46
47
48
49 write = (time, t_req_count, t_data_size, req_count, data_size, req_sec, data_avg)->
50 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?
51 fs.writeSync(fd, "#{time},#{t_req_count},#{t_data_size},#{req_count},#{data_size},#{req_sec},#{data_avg}\n", null, "utf-8")
52
53
54
55 flush =()->
56 req_sec = data.hits/program.interval
57 data_avg = if data.hits == 0 then 0 else data.size/data.hits
58 total_data.hits += data.hits
59 total_data.size += data.size
60 write(Math.round(new Date().getTime()/1000.0), total_data.hits, total_data.size, data.hits, data.size, req_sec, data_avg)
61 data = {hits:0, size:0}
62
63
64
65 null_resp = (req, res)->
66 data.hits += 1
67 req.on 'data', (chunk)->
68 data.size += chunk.length
69 req.on 'end', ()->
70 res.end(canned_response)
71
72 s.use '/', null_resp
73
74
75
76 setInterval flush, program.interval * 1000
77
Something went wrong with that request. Please try again.