Permalink
Browse files

Import runtime middleware by paul (Paul Sadauskas) into core

  • Loading branch information...
1 parent 7077d4b commit 37195bedbc6d1f02a47fea5712ad792aad5c1d4b @josh josh committed Dec 11, 2009
Showing with 63 additions and 0 deletions.
  1. +1 −0 lib/rack.rb
  2. +27 −0 lib/rack/runtime.rb
  3. +35 −0 test/spec_rack_runtime.rb
View
@@ -44,6 +44,7 @@ def self.release
autoload :Mime, "rack/mime"
autoload :Recursive, "rack/recursive"
autoload :Reloader, "rack/reloader"
+ autoload :Runtime, "rack/runtime"
autoload :Server, "rack/server"
autoload :ShowExceptions, "rack/showexceptions"
autoload :ShowStatus, "rack/showstatus"
View
@@ -0,0 +1,27 @@
+module Rack
+ # Sets an "X-Runtime" response header, indicating the response
+ # time of the request, in seconds
+ #
+ # You can put it right before the application to see the processing
+ # time, or before all the other middlewares to include time for them,
+ # too.
+ class Runtime
+ def initialize(app, name = nil)
+ @app = app
+ @header_name = "X-Runtime"
+ @header_name << "-#{name}" if name
+ end
+
+ def call(env)
+ start_time = Time.now
+ status, headers, body = @app.call(env)
+ request_time = Time.now - start_time
+
+ if !headers.has_key?(@header_name)
+ headers[@header_name] = "%0.6f" % request_time
+ end
+
+ [status, headers, body]
+ end
+ end
+end
@@ -0,0 +1,35 @@
+require 'test/spec'
+require 'rack/mock'
+require 'rack/runtime'
+
+context "Rack::Runtime" do
+ specify "sets X-Runtime is none is set" do
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
+ response = Rack::Runtime.new(app).call({})
+ response[1]['X-Runtime'].should =~ /[\d\.]+/
+ end
+
+ specify "does not set the X-Runtime if it is already set" do
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain', "X-Runtime" => "foobar"}, "Hello, World!"] }
+ response = Rack::Runtime.new(app).call({})
+ response[1]['X-Runtime'].should == "foobar"
+ end
+
+ specify "should allow a suffix to be set" do
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
+ response = Rack::Runtime.new(app, "Test").call({})
+ response[1]['X-Runtime-Test'].should =~ /[\d\.]+/
+ end
+
+ specify "should allow multiple timers to be set" do
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
+ runtime1 = Rack::Runtime.new(app, "App")
+ runtime2 = Rack::Runtime.new(runtime1, "All")
+ response = runtime2.call({})
+
+ response[1]['X-Runtime-App'].should =~ /[\d\.]+/
+ response[1]['X-Runtime-All'].should =~ /[\d\.]+/
+
+ Float(response[1]['X-Runtime-All']).should > Float(response[1]['X-Runtime-App'])
+ end
+end

0 comments on commit 37195be

Please sign in to comment.