HerokuRichard SchneemanBenchmark
Benchmarking Rack Middleware and Improved Speed
Summary
ActionDispatch::Static, enabled by config.serve_static_assets = true, checks incoming Rails requests to decide whether to serve a disk file or pass the request onward. To measure the middleware in isolation, the author loads it directly, wraps it around a no-op Proc, and uses Rack::MockRequest with benchmark-ips to compare requests against the no-op baseline. The original middleware achieved 15,891.2 iterations per second, versus 28,660.9 for the no-op, while the modified version reached 24,490.9 iterations per second. That change made non-asset middleware requests roughly 54% faster, bringing the gap to the no-op case to about 10%, although asset requests became slightly slower and the difference was not measurable. Across the full Rails application stack, the reported overall speedup was approximately 2.6%.
Context
ActionDispatch::Static checks every incoming request when Rails static asset serving is enabled, using a disk lookup to determine whether to serve a file or pass the request onward. The author needed a way to measure this individual Rack middleware rather than only the complete Rails application stack.
Approach / What changed
The middleware was loaded directly and instantiated around a no-op Proc, then exercised through Rack::MockRequest. benchmark-ips compared middleware requests with requests against the no-op baseline, using five-second warmup and measurement periods; the middleware was then tested after optimizations intended to avoid disk access.
Takeaways
The benchmark compared ActionDispatch::Static with a no-op Rack endpoint, treating the no-op as the fastest practical baseline for middleware overhead.
The modified middleware reached 24,490.9 iterations per second compared with 15,891.2 for the original when handling a request that did not request a file.
Optimizing non-asset requests introduced more logic and made asset requests slightly slower, though the asset-request difference was not measurable in the reported tests.
Source and freshness
- Source
- heroku.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.