Autotest CPU Fix

Posted by Scott on January 5, 2008

Update 1/15/2008
Autotest and rspec both posted updates today. Now, the way to fix this issue is slightly different:
[ruby]
Autotest.add_hook :run do |autotest|
autotest.add_exception(/^\.\/vendor/)
autotest.add_exception(/\.svn/)
end
[/ruby]
Thanks Ryan and David for the updates!

I’ve been noticing in my current project that running autotest was constantly consuming about 25-30% of my cpu and causing my macbook pro to run really hot. I did a little googling and found this discussion on the topic.

For me, my problem was definitely the vendor directory (~3500 files in 4 plugins: rspec, rspec_on_rails, restful_open_id_authentication and active_merchant). I tried excluding the entire directory by adding this to my ~/.autotest file:

[ruby]
Autotest.add_hook :initialize do |autotest|
autotest.exceptions << %r%^\./(?:coverage|db|doc|log|public|script|vendor|previous_failures.txt)%
end
[/ruby]

However, that didn’t work. After some investigation, I found out that the rspec_on_rails plugin in my vendor directory was subclassing Autotest::Rspec and setting it’s own exceptions string like this:

[ruby]
def initialize # :nodoc:
super
@exceptions = %r%^\./(?:coverage|db|doc|log|public|script|vendor\/rails|previous_failures.txt)%

[/ruby]

See the problem? It dutifully calls super so AutoTest:initialize can do it’s stuff (including calling my hook) and then wipes out the exception string with it’s own.

So, I browsed the lib/autotest.rb code and found another hook. Adding this to my ~/.autotest now lowers the cpu usage to about 5%. Huzzah!

[ruby]
Autotest.add_hook :run do |autotest|
autotest.exceptions = Regexp.union(/^\.\/vendor/, autotest.exceptions)
end
[/ruby]

8 Responses

  1. Ryan Davis
    January 7, 2008

    I should have communicated more and synchronized with Dave about the changes I made to @exceptions. sorry. We’ll both release something soon to get them back in sync and happier with each other.


  2. BrianC
    January 11, 2008

    Thanks for posting this. I noticed the same thing but vendor wasn’t my issue. I ran ‘find’ looking for a big chunk of files and found them in .git This is a project where I’m using git-svn. After adding .git to the exception CPU usage is down below 5%.


  3. th
    January 16, 2008

    Thanks! This helped me stop acts_as_solr from making autotest run in a continuous loop when the plugin log files were updated!


  4. austinfromboston
    January 17, 2008

    This was really helpful, thank you.


  5. KevinB
    January 18, 2008

    I have also noticed absurd CPU usage spikes with autotest. So I was happy to see this post. I upgraded ZenTest to 3.8.0 and put the updated code in my ~/.autotest and now get the following bustage:

    loading autotest/rails_rspec
    /usr/local/lib/ruby/gems/1.8/gems/ZenTest-3.8.0/lib/autotest.rb:515:in `add_exception’: exceptions already compiled (RuntimeError)

    Any ideas?


  6. KevinB
    January 18, 2008

    Needed to update rspec too - works like a charm.

    Thanks!


  7. scott
    January 19, 2008

    If you’re getting the “exceptions already compiled” error, you could try changing the hook to “Autotest.add_hook :initialize”. I didn’t need to do that, but both work.


  8. Zubin
    February 26, 2008

    After editing my ~/.autotest file as described, got this message when starting autotest:

    hook run has been deprecated, use initialize

    After making the change suggested by Scott (thanks!), the message disappeared.

    But it still ran slow, so I added more directories. Responds about 10 times faster now!

    Here’s what I did (some are probably already excluded by default):

    Autotest.add_hook :initialize do |autotest|
    autotest.add_exception(/^\.\/vendor/)
    autotest.add_exception(/^\.\/artwork/)
    autotest.add_exception(/^\.\/public/)
    autotest.add_exception(/^\.\/db/)
    autotest.add_exception(/^\.\/lib/)
    autotest.add_exception(/^\.\/tmp/)
    autotest.add_exception(/\.svn/)
    end


Leave a Reply


Who Are These Guys?

Netphase, LLC. is a Charlotte-based web application development company specializing in really rapid application development on the Ruby on Rails framework.

 

Both Scott and Chris have been active Ruby on Rails developers for more than 3 years, and each has spent more than a decade designing, devloping and deploying web applications.

 

We have worked on projects of all sizes, and delivered successful sites to production for some of the biggest names in the business. For all of the gory details, check out our about page.