src: move debug agent from deps/ to src/
[platform/upstream/nodejs.git] / benchmark / http_simple.rb
1 DIR = File.dirname(__FILE__)
2
3 def fib(n)
4   return 1 if n <= 1
5   fib(n-1) + fib(n-2)
6 end
7
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
15
16 class SimpleApp
17   @@responses = {}
18
19   def initialize
20     @count = 0
21   end
22
23   def deferred?(env)
24     false
25   end
26
27   def call(env)
28     path = env['PATH_INFO'] || env['REQUEST_URI']
29     commands = path.split('/')
30
31     @count += 1
32     if commands.include?('periodical_activity') and @count % 10 != 1
33       return [200, {'Content-Type'=>'text/plain'}, "quick response!\r\n"]
34     end
35
36     if commands.include?('fibonacci')
37       n = commands.last.to_i
38       raise "fibonacci called with n <= 0" if n <= 0
39       body = (1..n).to_a.map { |i| fib(i).to_s }.join(' ')
40       status = 200
41
42     elsif commands.include?('wait')
43       n = commands.last.to_f
44       raise "wait called with n <= 0" if n <= 0
45       wait(n)
46       body = "waited about #{n} seconds"
47       status = 200
48
49     elsif commands.include?('bytes')
50       n = commands.last.to_i
51       raise "bytes called with n <= 0" if n <= 0
52       body = @@responses[n] || "C"*n
53       status = 200
54
55     elsif commands.include?('fixed')
56       n = 20 * 1024;
57       body = @@responses[n] || "C"*n
58       status = 200
59
60     elsif commands.include?('test_post_length')
61       input_body = ""
62       while chunk = env['rack.input'].read(512)
63         input_body << chunk
64       end
65       if env['CONTENT_LENGTH'].to_i == input_body.length
66         body = "Content-Length matches input length"
67         status = 200
68       else
69         body = "Content-Length doesn't matches input length!
70           content_length = #{env['CONTENT_LENGTH'].to_i}
71           input_body.length = #{input_body.length}"
72         status = 500
73       end
74     else
75       status = 404
76       body = "Undefined url"
77     end
78
79     body += "\r\n"
80     headers = {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }
81     [status, headers, [body]]
82   end
83 end
84
85
86 if $0 == __FILE__
87   #require DIR + '/../lib/ebb'
88   require 'rubygems'
89   require 'rack'
90   require 'thin'
91   require 'ebb'
92 #  Rack::Handler::Mongrel.run(SimpleApp.new, :Port => 8000)
93   Thin::Server.start("0.0.0.0", 8000, SimpleApp.new)
94 #  Ebb::start_server(SimpleApp.new, :port => 8000)
95 end