Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

return aggregate stats on /aggregate requests

  • Loading branch information...
commit 53dd1f2865e9680508156222591690bd64ddec1d 1 parent ea48599
@igrigorik authored
View
10 lib/rack/aggregate/context.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
11 spec/context_spec.rb
@@ -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
37 spec/helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.