diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-12-11 16:00:57 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-12-11 16:00:57 -0600 |
commit | 37195bedbc6d1f02a47fea5712ad792aad5c1d4b (patch) | |
tree | 9e6e6ab54f604b6d395ec226686ee82f0624f428 | |
parent | 7077d4b6d3eb25c856c83b7c8a22210216fde645 (diff) | |
download | rack-37195bedbc6d1f02a47fea5712ad792aad5c1d4b.tar.gz |
Import runtime middleware by paul (Paul Sadauskas) into core
-rw-r--r-- | lib/rack.rb | 1 | ||||
-rw-r--r-- | lib/rack/runtime.rb | 27 | ||||
-rw-r--r-- | test/spec_rack_runtime.rb | 35 |
3 files changed, 63 insertions, 0 deletions
diff --git a/lib/rack.rb b/lib/rack.rb index 20b3c379..bb1e4ed1 100644 --- a/lib/rack.rb +++ b/lib/rack.rb @@ -44,6 +44,7 @@ module Rack 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" diff --git a/lib/rack/runtime.rb b/lib/rack/runtime.rb new file mode 100644 index 00000000..1bd411fd --- /dev/null +++ b/lib/rack/runtime.rb @@ -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 diff --git a/test/spec_rack_runtime.rb b/test/spec_rack_runtime.rb new file mode 100644 index 00000000..62d80956 --- /dev/null +++ b/test/spec_rack_runtime.rb @@ -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 |