Class: Rack::Bench

Inherits:
Object show all
Defined in:
lib/kiss/rack/bench.rb

Overview

Rack::Bench shows total request duration for any request.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Bench

Returns a new instance of Bench.



9
10
11
# File 'lib/kiss/rack/bench.rb', line 9

def initialize(app)
  @_app = app
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/kiss/rack/bench.rb', line 13

def call(env)
  start_time = Time.now
  code, headers, body = @_app.call(env)
  end_time = Time.now
  
  html = <<-EOT
<style>
.kiss_bench {
  text-align: left;
  padding: 3px 7px;
  border: 1px solid #ec4;
  border-top: 1px solid #fff4bb;
  border-bottom: 1px solid #d91;
  background-color: #ffe590;
  font-size: 12px;
  color: #101;
}
.kiss_bench a {
  color: #930;
  text-decoration: none;
}
.kiss_bench a:hover {
  color: #930;
  text-decoration: underline;
}
.kiss_bench small {
  font-family: arial, sans-serif;
  float: right;
  margin-left: 8px;
  color: #a60;
  text-align: right;
  white-space: nowrap;
}
</style>
<div class="kiss_bench">
<small>kiss bench</small>
<tt><b>TOTAL request duration: #{sprintf("%0.3f", end_time.to_f - start_time.to_f)} s</b></tt>
</div>
EOT
  
  body = body.prepend_html(html, 'body')
  headers['Content-Length'] = body.content_length.to_s

  [ code, headers, body ]
end