RequireBench
Have a Ruby project that has stopped loading, and you aren't sure where the issue is?
Knowing the last file that was successfully required, or loaded by Ruby can be helpful in diagnosing the issue. This gem will help you find that last required file. It can also help you see where expensive (slow) processing is occurring, by adding Benchmark.realtime
to every require
/ load
, and printing the result for every file.
As of version 1.0.4 it can also add timeout, rescue, and additional logging to both require
and load
.
This is an extraction of a debugging tool that I have copy/pasted into many projects over the years, and it is now time to set it free.
Note: This gem will make code load slower than normal, but may end up saving you time by showing you where a problem is.
Warning: This gem is for debugging problems. It uses a global $ variable, which is sad practice. It uses it as a safety semaphore, so I consider it justified. If you can think of a better way to implement the safety semaphore, let me know!
Caveat: This gem has no effects unless a particular environment variable is set. It does nothing at all unless it is 'invoked' by detection of the environment variable (ENV['REQUIRE_BENCH'] == 'true'
). The Warning above is mitigated by the gem not having any of its code, other than the namespace and version, activated under normal circumstances.
Project | RequireBench |
---|---|
gem name | require_bench |
license | |
download rank | |
version | |
dependencies | |
continuous integration | |
test coverage | |
maintainability | |
code triage | |
homepage | on Github.com, on Railsbling.com |
documentation | on RDoc.info |
Support | Chat on Element / Matrix / Gitter |
Spread ~♡ⓛⓞⓥⓔ♡~ | 🌏, 👼, |
Installation
Add this line to your application's Gemfile:
gem "require_bench"
And then execute:
$ bundle
Or install it yourself as:
$ gem install require_bench
Usage
Require the library where it will be loaded prior to any other requires you want to benchmark.
require "require_bench"
By default this gem does nothing, hacks nothing, and has zero effects.
Turn on benchmarking & output
Add an environment variable, however you normally do such things, so that in Ruby:
ENV["REQUIRE_BENCH"] == "true"
Any value other than 'true'
means RequireBench is still turned off.
Handy Rake Task
For a Gem Library
Require in Rakefile:
require "bundler/setup"
require "require_bench/tasks" # Near the top, just below require 'bundler/setup'!
For Rails
Require in Rakefile:
require_relative "config/application"
require "require_bench/tasks" # Near the top, just below require_relative 'config/application'!
Example
When running from command line, you will see output as the Rails app boots.
$ REQUIRE_BENCH=true bundle exec rake require_bench:hello
[RequireBench-r] 12.179703 /path/to/my_app/config/application
[RequireBench-r] 0.001726 resque/tasks
[RequireBench-r] 0.000917 resque/scheduler/tasks
[RequireBench-r] 0.000011 rake
[RequireBench-r] 0.000014 active_record
[RequireBench-r] 0.008673 sprockets/rails/task
[RequireBench-r] 0.000012 dynamoid
[RequireBench-r] 0.000004 dynamoid/tasks/database
[RequireBench-r] 0.000012 raven/integrations/tasks
[RequireBench-r] 0.003107 rspec/core/rake_task
[RequireBench-r] 0.000017 csv
[RequireBench-r] 0.000012 resque/tasks
[RequireBench-r] 0.000007 resque/scheduler/tasks
[RequireBench-r] 0.064950 rails/tasks
[RequireBench-r] 0.003305 rake/testtask
[RequireBench-r] 0.001886 rubocop/rake_task
[RequireBench-r] 0.000012 hubspot-ruby
[RequireBench-r] 2.291259 /path/to/my_app/config/environment.rb
[RequireBench] Slowest Loads by Library, in order
1. 11.914224 /path/to/my_app/config/application
2. 2.153282 /path/to/my_app/config/environment.rb
3. 0.061008 rails
4. 0.010827 sprockets
5. 0.003179 rspec
6. 0.003144 rake
7. 0.003127 resque
8. 0.001543 rubocop
9. 0.000021 dynamoid
10. 0.000016 csv
11. 0.000016 active_record
12. 0.000010 raven
13. 0.000005 hubspot-ruby
==========
14.150402 TOTAL
Output Options
If the output is too noisy from deep libraries you can add a regex to skip benchmarking of files that match.
export REQUIRE_BENCH_SKIP_PATTERN=activesupport,rspec
ENV['REQUIRE_BENCH_SKIP_PATTERN']
must be one of:
- a string, to be split by comma (
,
), then joined by pipe (|
) withRegexp.union
- a string, to be split by pipe (
|
), then joined by pipe (|
) withRegexp.union
ENV["REQUIRE_BENCH_SKIP_PATTERN"] = "activesupport,rspec"
# or
ENV["REQUIRE_BENCH_SKIP_PATTERN"] = "activesupport|rspec"
Any file being required that matches the pattern will use the standard, rather than the benchmarked, require.
Fully qualified paths
Fully qualified paths, or any portion thereof, are fine, because the strings are always Regexp escaped.
Other ENV control variables
- wrap/log load in addition to require
ENV['REQUIRE_BENCH_TRACKED_METHODS']
- rescue errors
ENV['REQUIRE_BENCH_RESCUED_CLASSES']
- log start
ENV['REQUIRE_BENCH_LOG_START']
- load/require timeout
ENV['REQUIRE_BENCH_TIMEOUT']
- A pattern for paths that should be included/tracked
ENV['REQUIRE_BENCH_INCLUDE_PATTERN']
- Should grouping be by basename or by path?
ENV['REQUIRE_BENCH_GROUP_PRECEDENCE']
- Prefer to not group some pattern (i.e. some libraries)
ENV['REQUIRE_BENCH_NO_GROUP_PATTERN']
If you'd like to help document any of these further, PRs are appreciated!
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
To get code coverage:
CI_CODECOV=true COVER_ALL=false bundle exec rspec
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/pboling/require_bench.
Code of Conduct
Everyone interacting in the AnonymousActiveRecord project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Versioning
This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.
As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.
For example:
spec.add_dependency("require_bench", "~> 0.0")
License
- Copyright (c) 2018-2020, 2023 Peter H. Boling of Rails Bling