module Rack class LoadSpeed def initialize(app) @app = app end def call(env) env.delete("HTTP_IF_NONE_MATCH") status, headers, response = @app.call(env) if status == 200 && headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/ body = "" response.each {|part| body << part} index = body.rindex("") if index body.insert(index, performance_code) # # handle body w/ UTF8 characters for Ruby 1.9+ to avoid Rack::Lint::LintError # headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.length).to_s response = [body] end end [status, headers, response] end protected def performance_code events = "" Stopwatch::Log.events.each do |event| events << "" end event = Stopwatch::Log.event events << "" events << "
duration (ms)queries
#{event.template}#{event.duration}#{event.query_count}
#{event.payload[:path]}#{event.duration}#{Stopwatch::Log.query_count}
" html = <<-EOF
#{Stopwatch::Log.event.duration.to_i} ms #{Stopwatch::Log.query_count} queries #{events}
EOF html end end end