From 37195bedbc6d1f02a47fea5712ad792aad5c1d4b Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 11 Dec 2009 16:00:57 -0600 Subject: Import runtime middleware by paul (Paul Sadauskas) into core --- lib/rack.rb | 1 + lib/rack/runtime.rb | 27 +++++++++++++++++++++++++++ test/spec_rack_runtime.rb | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 lib/rack/runtime.rb create mode 100644 test/spec_rack_runtime.rb 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 -- cgit v1.2.3-24-ge0c7