runtime.rb 710 B

123456789101112131415161718192021222324252627
  1. module Rack
  2. # Sets an "X-Runtime" response header, indicating the response
  3. # time of the request, in seconds
  4. #
  5. # You can put it right before the application to see the processing
  6. # time, or before all the other middlewares to include time for them,
  7. # too.
  8. class Runtime
  9. def initialize(app, name = nil)
  10. @app = app
  11. @header_name = "X-Runtime"
  12. @header_name << "-#{name}" if name
  13. end
  14. def call(env)
  15. start_time = Time.now
  16. status, headers, body = @app.call(env)
  17. request_time = Time.now - start_time
  18. if !headers.has_key?(@header_name)
  19. headers[@header_name] = "%0.6f" % request_time
  20. end
  21. [status, headers, body]
  22. end
  23. end
  24. end