用于 Active Record 查询的跟踪器
A tracer for Active Record queries You identified (or suspect) that the reason for the slowness of some code is Active Record, specifically lots of queries and/or loaded records. How do you easily detect which queries, which records are loaded the most, and the sources of those? This tool to the rescue!
Add this line to your application's Gemfile:
gem "active_record_tracer", group: [:development, :test]And then execute:
$ bundleOr install it yourself as:
$ gem install active_record_tracerrequire "active_record_tracer"
report = ActiveRecordTracer.report do
# run your code here
end
report.pretty_printOr, you can use the .start/.stop API as well:
require "active_record_tracer"
ActiveRecordTracer.start
# run your code
report = ActiveRecordTracer.stop
report.pretty_printNOTE: .start/.stop can only be run once per report, and .stop will
be the only time you can retrieve the report using this API.
To trace tests, use the following in test_helper.rb for Minitest:
ActiveRecordTracer.start
Minitest.after_run do
report = ActiveRecordTracer.stop
report.pretty_print(to_file: "tmp/active_record_tracer-tests.txt")
endreportThe report method can take a few options:
top: maximum number of entries to display in a report (default is 50)backtrace_lines: maximum number of backtrace lines to include in the report (default is 5)ignore_cached_queries: whether to ignore cached queries (default is false)ignore_schema_queries: whether to ignore schema queries (default is true)Check out Reporter#new for more details.
pretty_printThe pretty_print method can take a few options:
to_file: a path to your log file - can be given a Stringdetailed_report: whether to include detailed information - can be given a BooleanCheck out Report#pretty_print for more details.
…After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rake test to run the tests. This project uses multiple Gemfiles to test against multiple versions of Active Record; you can run the tests against the specific version with BUNDLE_GEMFILE=gemfiles/activerecord_70.gemfile bundle exec rake test.
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/fatkodima/active_record_tracer.
The gem is available as open source under the terms of the MIT License.
暂无开放 Issues,或尚未同步最近议题。