Permalink
Browse files

return aggregate stats on /aggregate requests

  • Loading branch information...
1 parent ea48599 commit 53dd1f2865e9680508156222591690bd64ddec1d @igrigorik committed Jan 9, 2011
Showing with 56 additions and 2 deletions.
  1. +9 −1 lib/rack/aggregate/context.rb
  2. +11 −0 spec/context_spec.rb
  3. +36 −1 spec/helper.rb
@@ -8,7 +8,8 @@ def initialize(app, options = {}, &blk)
# ...
}.merge(options)
- @app = app
+ @app = app
+ @aggregate = ::Aggregate.new
yield self if block_given?
end
@@ -18,6 +19,13 @@ def call(env)
resp = Rack::Response.new('', 200)
resp['Content-Type'] = 'text/plain'
+ [:mean, :min, :max, :std_dev].each do |metric|
+ resp.write "#{metric}: %1.2fms\n" % (@aggregate.send(metric) || 0)
+ end
+
+ resp.write "Request histogram:\n"
+ resp.write @aggregate.to_s
+
return resp.finish
end
View
@@ -12,4 +12,15 @@
lambda { Rack::Aggregate.new(app, {}) }.should_not raise_error(ArgumentError)
end
end
+
+ describe 'response' do
+ it 'should respond with 200 to requests to the aggregate endpoint' do
+ respond_with(200)
+ response = get('/aggregate')
+
+ response.status.should == 200
+ response.body.should match('Empty histogram')
+ end
+
+ end
end
View
@@ -1,4 +1,39 @@
require 'bundler'
Bundler.setup
-require 'rack/aggregate'
+require 'rack/aggregate'
+
+[ STDOUT, STDERR ].each { |io| io.sync = true }
+
+def respond_with(status=200, headers={}, body=['Hello World'])
+ called = false
+ @app = lambda do |env|
+ called = true
+ response = Rack::Response.new(body, status, headers)
+ request = Rack::Request.new(env)
+
+ yield request, response if block_given?
+
+ response.finish
+ end
+
+ @app
+end
+
+def request(method, uri='/', opts={})
+ opts = {
+ 'rack.run_once' => true,
+ 'rack.errors' => @errors,
+ }.merge(opts)
+
+ @aggregate ||= Rack::Aggregate::Context.new(@app)
+ @request = Rack::MockRequest.new(@aggregate)
+
+ yield @aggregate if block_given?
+
+ @request.request(method.to_s.upcase, uri, opts)
+end
+
+def get(uri, env={}, &b)
+ request(:get, uri, env, &b)
+end

0 comments on commit 53dd1f2

Please sign in to comment.