Class: SimpleApp

Inherits:
Object
  • Object
show all
Defined in:
ext/node/benchmark/http_simple.rb

Constant Summary collapse

@@responses =
{}

Instance Method Summary collapse

Constructor Details

#initializeSimpleApp

Returns a new instance of SimpleApp.



19
20
21
# File 'ext/node/benchmark/http_simple.rb', line 19

def initialize
  @count = 0
end

Instance Method Details

#call(env) ⇒ Object



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
81
82
# File 'ext/node/benchmark/http_simple.rb', line 27

def call(env)
  path = env['PATH_INFO'] || env['REQUEST_URI']
  commands = path.split('/')

  @count += 1
  if commands.include?('periodical_activity') and @count % 10 != 1
    return [200, {'Content-Type'=>'text/plain'}, "quick response!\r\n"]
  end

  if commands.include?('fibonacci')
    n = commands.last.to_i
    raise "fibonacci called with n <= 0" if n <= 0
    body = (1..n).to_a.map { |i| fib(i).to_s }.join(' ')
    status = 200

  elsif commands.include?('wait')
    n = commands.last.to_f
    raise "wait called with n <= 0" if n <= 0
    wait(n)
    body = "waited about #{n} seconds"
    status = 200

  elsif commands.include?('bytes')
    n = commands.last.to_i
    raise "bytes called with n <= 0" if n <= 0
    body = @@responses[n] || "C"*n
    status = 200

  elsif commands.include?('fixed')
    n = 20 * 1024;
    body = @@responses[n] || "C"*n
    status = 200

  elsif commands.include?('test_post_length')
    input_body = ""
    while chunk = env['rack.input'].read(512)
      input_body << chunk
    end
    if env['CONTENT_LENGTH'].to_i == input_body.length
      body = "Content-Length matches input length"
      status = 200
    else
      body = "Content-Length doesn't matches input length!
        content_length = #{env['CONTENT_LENGTH'].to_i}
        input_body.length = #{input_body.length}"
      status = 500
    end
  else
    status = 404
    body = "Undefined url"
  end

  body += "\r\n"
  headers = {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }
  [status, headers, [body]]
end

#deferred?(env) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'ext/node/benchmark/http_simple.rb', line 23

def deferred?(env)
  false
end