How can I profile ruby ​​blocks?

I would like to know how much it took to complete the 10 longest tests. For example, can I do with rspec, any suggestion?

+4
source share
3 answers

Short answer:

gem install minitest # Install MiniTest gem install minitest_tu_shim # Install Test::Unit shim use_minitest yes # Use MiniTest Test::Unit shim instead of stdlib Test::Unit ruby your_test.rb -v # Run your test in verbose mode 

Ruby 1.9 uses MiniTest as its default testing platform instead of Test::Unit . MiniTest is smaller, faster, has more useful features and is largely backward compatible with Test :: Unit. One of these new features is the measurement of the time that each test takes with the -v flag. When starting, make sure you put this flag after the script

If, as in rails, you use Rake::TestTask to run your tests, you can either specify it at run time by doing

 rake test TESTOPTS='-v' 

or specify it in the task by adding -v to the options attribute, for example:

 Rake::TestTask.new do |t| t.options = '-v' end 

Finally, if you use rails, and MiniTest is simply not enough for you, you can evaluate the test_benchmark plugin. Use is easy. Add the following line to config/environments/test.rb

 config.gem "timocratic-test_benchmark", :lib => 'test_benchmark', :source => 'http://gems.github.com' 

Install it with

 RAILS_ENV='test' rake gems:install 

From now on, you will get a good sorted list when running tests

 rake test:units [...] Test Benchmark Times: Suite Totals: 7.124 test_destroy(FeedTest) 7.219 test_create(FeedTest) 7.646 test_subscribe_to_auto_discovery(FeedTest) 9.339 test_auto_discover_updates_url(FeedTest) 9.543 test_find_or_create_by_auto_discover_url(FeedTest) 15.780 test_import_from_opml(FeedTest) 

Sorry that the MiniTest plugin and test_benchmark are not compatible with each other, but I highly recommend that you try MiniTest , as this will speed up your tests and will continue to be supported in Ruby 1.9.

+7
source

Minitest :: Profile is the new gem that lists the top intruders after a standard test run.

https://github.com/nmeans/minitest-profile

+1
source

Perhaps run your unit tests under the ruby ​​profile by running the ruby -rprofile code instead of ruby ?

0
source

All Articles