File: benchmarker.rb

package info (click to toggle)
thin 1.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,084 kB
  • sloc: ruby: 4,138; ansic: 1,537; sh: 82; makefile: 8
file content (80 lines) | stat: -rw-r--r-- 1,777 bytes parent folder | download | duplicates (5)
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
78
79
80
require 'rack/lobster'

class Benchmarker
  PORT    = 7000
  ADDRESS = '0.0.0.0'
  
  attr_accessor :requests, :concurrencies, :servers, :keep_alive
  
  def initialize
    @servers = %w(Mongrel EMongrel Thin)
    @requests = 1000
    @concurrencies = [1, 10, 100]
  end
  
  def writer(&block)
    @writer = block
  end
  
  def run!
    @concurrencies.each do |concurrency|
      @servers.each do |server|
        req_sec, failed = run_one(server, concurrency)
        @writer.call(server, @requests, concurrency, req_sec, failed)
      end
    end
  end
  
  private
    def start_server(handler_name)
      @server = fork do
        [STDOUT, STDERR].each { |o| o.reopen "/dev/null" }

        case handler_name
        when 'EMongrel'
          require 'swiftcore/evented_mongrel'
          handler_name = 'Mongrel'
        end

        app = proc do |env|
          [200, {'Content-Type' => 'text/html', 'Content-Length' => '11'}, ['hello world']]
        end

        handler = Rack::Handler.const_get(handler_name)
        handler.run app, :Host => ADDRESS, :Port => PORT
      end
    
      sleep 2
    end
  
    def stop_server
      Process.kill('SIGKILL', @server)
      Process.wait
    end
  
    def run_ab(concurrency)
      `nice -n20 ab -c #{concurrency} -n #{@requests} #{@keep_alive ? '-k' : ''} #{ADDRESS}:#{PORT}/ 2> /dev/null`
    end
  
    def run_one(handler_name, concurrency)
      start_server(handler_name)

      out = run_ab(concurrency)

      stop_server

      req_sec = if matches = out.match(/^Requests.+?(\d+\.\d+)/)
        matches[1].to_i
      else
        0
      end
    
      failed = if matches = out.match(/^Failed requests.+?(\d+)/)
        matches[1].to_i
      else
        0
      end
    
      [req_sec, failed]
    end
end