Adding a little script to run benchmarks. it save the data in a marshalled
[ebb.git] / ruby_binding / benchmark / test_camping.rb
blobc936caecf9c2204f38643530b77f0adb1bf489b5
1 #!/usr/bin/env ruby
2 require 'rubygems'
3 require '../ebb'
4 require 'camping'
5 require 'rack'
6 require 'mongrel'
7 require 'swiftcore/evented_mongrel' 
8 require 'thin'
10 Camping.goes :CampApp
11 module CampApp
12   module Controllers
13     class HW < R('/')
14       def get
15         @headers["X-Served-By"] = URI("http://rack.rubyforge.org")
16         @headers["Content-Type"] = 'text/plain'
17         "Camping works! " * 5000
18       end
19       def post
20         "Data: #{input.foo}"
21       end
22     end
23   end
24 end
26 class Array
27   def avg
28     sum.to_f / length
29   end
30   
31   def sum
32     inject(0) { |i, s| s += i }
33   end
34   
35   def rand_each(&block)
36     sort_by{ rand }.each &block
37   end
38 end
41 class ServerTest
42   attr_reader :name, :port, :app
43   def initialize(name, port, &start_block)
44     @name = name
45     @port = port
46     puts "Starting #{name}"
47     @pid = fork { start_block.call }
48     sleep 3
49   end
50   
51   def <=>(a)
52     @name <=> a.name
53   end
54   
55   def kill
56     Process.kill('KILL', @pid)
57   end
58   
59   def run_trial(concurrency)
60     print "#{@name} with concurrency #{concurrency}..."
61     $stdout.flush
62     r = %x{ab -q -c #{concurrency} -n 1000 http://0.0.0.0:#{@port}/ | grep "Requests per second" }
63     raise "couldn't match rps in #{r.inspect}" unless r =~ /Requests per second:\s*(\d+\.\d\d)/
64     rps = $1.to_f
65     puts rps
66     {
67       :test => 'camping1',
68       :server=> @name, 
69       :concurrency => concurrency, 
70       :rps => rps,
71       :time => Time.now
72     }
73   end
74 end
76 def all_tests(dump_file = "./benchmarks.ruby_dump")
77   
78   if File.readable?(dump_file)
79     $results = Marshal.load(File.read(dump_file))
80   else
81     $results = []
82   end
83   
84   app = Rack::Adapter::Camping.new(CampApp)
85   servers = []
86   
87   servers << ServerTest.new('evented mongrel', 4001) do
88     ENV['EVENT'] = "1"
89     Rack::Handler::Mongrel.run(app, :Port => 4001)
90   end
91   
92   servers << ServerTest.new('ebb', 4002) do
93     server = Ebb::Server.new(app, :Port => 4002)
94     server.start
95   end
96   
97   servers << ServerTest.new('thin', 4003) do
98     Rack::Handler::Thin.run(app, :Port => 4003)
99   end
100   
101   
102   ([1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91, 100]*3).rand_each do |concurrency|
103     servers.rand_each do |server| 
104       $results << server.run_trial(concurrency)
105       sleep 0.5 # give the other process some time to cool down?
106     end
107   end
108   
109 ensure
110   puts "killing servers"
111   servers.each { |server| server.kill }  
112   unless $results.empty?
113     puts "writing to dump file #{dump_file}"
114     File.open(dump_file, 'w+') do |f|
115       f.write Marshal.dump($results)
116     end
117   end
120 all_tests
122 # when 'ebb'
123 #   puts "ebb"
124 #   server = Ebb::Server.new(app, :Port => 4001)
125 #   server.start
126 # # when 'm'
127 # #   puts "mongrel"
128 # #   Rack::Handler::Mongrel.run(app, :Port => 4002)
129 # when 'mongrel'
131 # else
132 #   puts "unknown arg"
133 # end