fixed ebb_rails stopping
[ebb.git] / benchmark / application.rb
blob1d4bc481383c9e0b80b093a9d5ad841ea3b64d3f
1 DIR = File.dirname(__FILE__)
3 def fib(n)
4   return 1 if n <= 1
5   fib(n-1) + fib(n-2)
6 end
8 def wait(seconds)
9   n = (seconds / 0.01).to_i
10   n.times do 
11     sleep(0.01)
12     #File.read(DIR + '/yahoo.html') 
13   end
14 end
16 class SimpleApp
17   @@responses = {}
18   
19   def initialize
20     @count = 0
21   end
22   
23   def call(env)
24     commands = env['PATH_INFO'].split('/')
25     
26     @count += 1
27     if commands.include?('periodical_activity') and @count % 10 != 1
28       return [200, {'Content-Type'=>'text/plain'}, "quick response!\r\n"]
29     end
30     
31     if commands.include?('fibonacci')
32       n = commands.last.to_i
33       raise "fibonacci called with n <= 0" if n <= 0
34       body = (1..n).to_a.map { |i| fib(i).to_s }.join(' ')
35       status = 200
36     
37     elsif commands.include?('wait')
38       n = commands.last.to_i
39       raise "wait called with n <= 0" if n <= 0
40       wait(n)
41       body = "waited about #{n} seconds"
42       status = 200
43     
44     elsif commands.include?('bytes')
45       n = commands.last.to_i
46       raise "bytes called with n <= 0" if n <= 0
47       body = @@responses[n] || "C"*n
48       status = 200
49       
50     elsif commands.include?('test_post_length')
51       input_body = ""
52       while chunk = env['rack.input'].read(512)
53         input_body << chunk 
54       end
55       if env['HTTP_CONTENT_LENGTH'].to_i == input_body.length
56         body = "Content-Length matches input length"
57         status = 200
58       else
59         body = "Content-Length doesn't matches input length! 
60           content_length = #{env['HTTP_CONTENT_LENGTH'].to_i}
61           input_body.length = #{input_body.length}"
62         status = 500
63       end
64     
65     else
66       status = 404
67       body = "Undefined url"
68     end
69     
70     [status, {'Content-Type' => 'text/plain'}, body + "\r\n\r\n"]
71   end
72 end
75 if $0 == __FILE__
76   require DIR + '/../ruby_lib/ebb'
77   require 'rubygems'
78   require 'ruby-debug'
79   Debugger.start
80   
81   server = Ebb::Server.new(SimpleApp.new, :port => 4001)
82   puts "Ebb started on http://0.0.0.0:4001/"
83   server.start
84 end