def bench(label = nil) Rack::Bench.close_bench_item(Kernel.caller[0]) Rack::Bench.on if label Rack::Bench.push_bench_item( :label => label, :start_time => Time.now, :start_context => Kernel.caller[0] ) end end module Rack # Rack::Bench adds benchmarking capabilities to Kiss applications. # # bench(label) starts a new timer, which ends upon the next call to # bench, or when execution returns to Rack::Bench. # # bench can be called without a label to end the previous timer # without starting a new one. # # Total request duration is also displayed for any request in which # the bench function is called. class Bench def self.on @@bench = true end def self.close_bench_item(end_context = nil) if @@bench_items[-1] && !@@bench_items[-1][:end_time] @@bench_items[-1][:end_time] = Time.now @@bench_items[-1][:end_context] = end_context end end def self.push_bench_item(item) @@bench_items.push(item) end def initialize(app) @app = app end def call(env) @@bench = false @@bench_items = [] start_time = Time.now code, headers, body = @app.call(env) end_time = Time.now if @@bench Rack::Bench.close_bench_item contents = <<-EOT EOT contents += @@bench_items.map do |item| start_link = context_link(item[:start_context]) end_link = context_link(item[:end_context]) <<-EOT