Profiling of Ruby Minitest tests - ruby-on-rails

I have a lot of slow-running Minitest tests for Rails app (3-4 minutes on one test). I want to profile them to learn how to fix them.
I tried to use standard Ruby profiler and ruby-prof - neither worked. Standard Ruby profiler profiles... something, but it prints its results BEFORE running tests, and I get no information on them. Ruby-prof simply hangs my tests if I include RubyProf.start in the test.
The Internet is full of advices on profiling Rails apps. Hovever, I did not find any guide for profiling tests for Rails apps. Please, help me choose proper strategy for test profiling!

I was able to add the profile to my unit tests successfully by including require 'profile' at the top of my test file. It does hang at the beginning (maybe for 30 seconds) when it's setting up, but after the tests ran I got a detailed output on how much time was spent in different functions.
I ran the tests from the command line with ruby -rprofile -I"lib:test" <test filepath>.

Related

Rspec running very, very slowly on Ruby on Rails

I am running Ruby on Rails 5.1.4 and am trying to upgrade to Ruby 2.5.1 from Ruby 2.4.4. When I run Rspec, it is running incredibly slowly. I have used the method described here by myronmarston allows me to see what is happening.
The Rspec program appears to be progressing normally, just every step is taking a long time, for example it takes over half an hour to finish the rspec.configure block in spec_helper, minutes to execute a validate statement in a model. I have tried different tests and it is not any specific test code.
If I start up the rails server, the app appears to be working normally. The console appears to work normally as well, it is just rspec. I have tried spring stop and it makes no difference to outcome.
Any ideas on what is causing this?

RubyMine - Running RSpec/ jruby Java::JavaLang::OutOfMemoryError: GC overhead limit exceeded

I've recently been blessed with RubyMine 8.0.3 at work and despite the IDE being quite easy to use, I am having troubles with running my RSpec tests through the built in RSpec console. The problem is that my specs fail during the initial set-up with the error "Java::JavaLang::OutOfMemoryError: GC overhead limit exceeded".
I am aware that this is the java garbage collector running out of memory- probably caused by some inefficient code somewhere that pre-dates my involvement in the project. However, I have no problem running the rspec tests through the bash console, outside of RubyMine, with the command rspec "path/to/test_spec.rb".
The set-up process is actually a bit quicker here, but I hate trolling through all the poorly indented error lines in the console when my tests do actually execute, hence my preference of using RubyMines nice way of inspecting failures/ successful tests (why pay for the subscription if you're not gonna use it, right?)
By the looks of things, RubyMine calls some sort of jruby command before the RSpec- so is there a way to configure RubyMine to not invoke JRuby when running rspec tests? Or at least a way to configure the jruby GC in RubyMine, so that I don't run into these memory issues?
Thanks,
[SOLVED] So after a bit of digging around in the IDE's various options, I found an "edit configurations" option from the run/debug environment drop down. Here you can set environment variables for JRuby. If you click on the RSpec option, this will extend to show all of your RSpec tests. Clicking on them will show the configurations for that test, which includes an option called "Environment Variables". If you stick -J-Xmx2048m in there, this will allocate 2048mb's to the JRuby memory heap.
As expected, this solves the issue, as RubyMine seems to be restricting the amount of memory the JRuby runtime has available by default.
Obviously, you can change the memory value to whatever your machine is capable of, but this solves the issue.

How can I decrease my Rails test overhead?

I'm using Test::Unit on a large app with a large number of gem dependencies (>75). I'm trying to develop using BDD, but it takes minutes for the app to load it's dependencies before it can run the tests. Is there a way to preload the dependencies and just auto-run the test on changes, or a similar solution?
I would look into Spork. It works wonders.
https://github.com/sporkrb/spork
https://github.com/sporkrb/spork-testunit
I am using RSpec and there's a great tool for it, called Spork. It basically loads your app once and then just reloads modified parts. If you combine it with Guard, you get "continuous testing". That is, you hit 'Save' in your editor and tests start executing, giving you instant feedback. This still amazes me after some months :)
Edit
As #THEM points out, there's a plugin for Spork to support TestUnit. You should look into it.
There was also an interesting article about test speed on the 37Signals blog a while back. Might be of interest even if you end up going with Spork or another solution.

Speeding up rails tests during development by keeping Rails in memory?

When running rspec tests on my rails app using "rake" or "rake spec", it takes a long time to initialize Rails and start running tests.
Is there a way to keep Rails loaded in memory and run tests against it? I'm hoping there's something similar to using "grails interactive" like this How to speed up grails test execution but for Rails.
It's almost what Spork is all about.
See the most awesome railscast "How I test" http://railscasts.com/episodes/275-how-i-test
This uses guard to run a relevant test every time you save a file so you know almost instantly if the code you have written has caused a pass or a fail. Also if you are running on linux then you can use the libnotify and libnotify-rails gems which pop up a window for you (totally unobtrusively) indicating a pass or failure so you don't have to keep checking the console.
There is a Railscast called Spork that specifically discusses this problem at http://railscasts.com/episodes/285-spork and offers an excellent solution with a detailed walkthrough.

Rails 3 RSpec 2 NetBeans integration

NetBeans 6.9 provides a custom Runner class for RSpec to be integrated into the IDE. I'm trying to get my Rails 3 applications specs to be correctly displayed inside NetBeans, but RSpec 2 seems no longer to support custom Runner classes in general.
Any ideas how to get the specs into the IDE anyway?
Just in. Oracle has just announced they are withdrawing support for Rails in future version of NetBeans. Time to start looking at other IDE options.
http://news.ycombinator.com/item?id=2148161
So far (NB 6.9.1) the only way I know to run Rspec2 tests from inside NetBeans is by using rake tasks. But I was not able to make it work with UI Test Runner, because of this and few other problems. So the best way is to avoid invoking UI runner, this can be done in many ways:
Disable it via tools -> options -> miscellaneous -> Ruby
modify project.properties file
give other name to task than 'spec', so naming task as 'rspec' will avoid invoking UI runner
This way you will have just test results in output pan, but it is still usable, because you can click anywhere on stack trace, and NB will take you immediately to that file:line.
There is one thing left, auto generated by NB Rakefile has not valid task (for Rails projects, there is NO such problem), to make it work one needs at least:
require 'rspec/core/rake_task'
Rspec::Core::RakeTask.new(:rspec)
I know this is not what you are expecting but you might want to check RubyMine3 out it comes out of the box, you do need to buy a licence but at least you can check it out in the 30 day trial
I am using RVM.
And at the minimum I wanted to be able to run my Ruby 1.9.2 / Rails 3 / RSpec 2 specs from inside the IDE and be able to click on stack traces for Netbeans to open the right files and lines.
I found a work-around for that:
Put somewhere in the project a ruby file that shells out to run the spec suite.
E. g. my ruby file has the following content:
system <<EOF
time ~/.rvm/wrappers/ruby-1.9.2-p290#default/rspec --drb spec
EOF
Change the ruby version and gemset as you need it.
The major limitation:
I cannot just run only a single spec. For that I have to change the "spec" parameter to the target spec file (which isn't such a big deal though).
Netbeans is fairly sluggish running specs (using rspec1 here), would recommend running specs from command line.

Resources