port test/unit/test_ccc.rb to Perl 5
[unicorn.git] / test / benchmark / ddstream.ru
blobfd40cedd48db98ef3d702005117d21f65091555e
1 # frozen_string_literal: false
2 # This app is intended to test large HTTP responses with or without
3 # a fully-buffering reverse proxy such as nginx. Without a fully-buffering
4 # reverse proxy, unicorn will be unresponsive when client count exceeds
5 # worker_processes.
7 # To demonstrate how bad unicorn is at slowly reading clients:
9 #   # in one terminal, start unicorn with one worker:
10 #   unicorn -E none -l 127.0.0.1:8080 test/benchmark/ddstream.ru
12 #   # in a different terminal, start more slow curl processes than
13 #   # unicorn workers and watch time outputs
14 #   curl --limit-rate 8K --trace-time -vsN http://127.0.0.1:8080/ >/dev/null &
15 #   curl --limit-rate 8K --trace-time -vsN http://127.0.0.1:8080/ >/dev/null &
16 #   wait
18 # The last client won't see a response until the first one is done reading
20 # nginx note: do not change the default "proxy_buffering" behavior.
21 # Setting "proxy_buffering off" prevents nginx from protecting unicorn.
23 # totally standalone rack app to stream a giant response
24 class BigResponse
25   def initialize(bs, count)
26     @buf = "#{bs.to_s(16)}\r\n#{' ' * bs}\r\n"
27     @count = count
28     @res = [ 200,
29       { 'Transfer-Encoding' => -'chunked', 'Content-Type' => 'text/plain' },
30       self
31     ]
32   end
34   # rack response body iterator
35   def each
36     (1..@count).each { yield @buf }
37     yield -"0\r\n\r\n"
38   end
40   # rack app entry endpoint
41   def call(_env)
42     @res
43   end
44 end
46 # default to a giant (128M) response because kernel socket buffers
47 # can be ridiculously large on some systems
48 bs = ENV['bs'] ? ENV['bs'].to_i : 65536
49 count = ENV['count'] ? ENV['count'].to_i : 2048
50 warn "serving response with bs=#{bs} count=#{count} (#{bs*count} bytes)"
51 run BigResponse.new(bs, count)