Why rspec so slow




















Created Mar 16, Code Revisions 1 Stars 13 Forks 1. Embed What would you like to do? Embed Embed this gist in your website. Share Copy sharable link for this gist. Learn more about clone URLs. Download ZIP. RSpec is not the reason your rails test suite is slow. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below.

To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters. This comment has been minimized. Sign in to view. Copy link Quote reply. Nevertheless, we must run all tests during our deployment cycle, using CI services. Would you like to wait dozens of minutes or even hours if there are loads of builds in the queue to deploy a hotfix?

I doubt so. For example, in my current project we have 5x parallelism and our average per job RSpec time is 2 minutes and 30 seconds for examples.

That means our EPM examples per minute equals Before any optimizations, examples ran for 4 minutes—that is only EPM!

Now we are saving minutes with every build. Let me skip all the lyrics and introduce TestProf —the Ruby test profiling toolbox. TestProf aims to help you identify the bottlenecks in your test suite and provide you with recipes to fix them. To answer the first question, you can use Tag Profiler from TestProf, which allows you to collect statistics grouped by a particular RSpec tag value.

RSpec automatically adds a type tag to examples, so we can use it:. Reports generated by these profilers can help you identify your hottest stack paths and, hence, answer the second question. Unfortunately, this type of profiling requires a lot of resources and slows down your already not-so-fast test suite even more.

You have to run it against a small portion of tests, but how to choose this portion? Well, randomly! Now try running StackProf against a sample of your controller tests because they are the slowest according to TagProf and read the output.

When I did this for one of my projects, I saw the following:. It turned out that our encryption configuration for Sorcery was as strict in test environment as in production. In a typical Rails application, most of the time you will see something like this in your reports:. A lot of ActiveRecord stuff—a lot of database usage. Wondering how to deal with it? Keep reading. Are you running this over Rails? If so, it's not RSpec's initialization that's slow, it's Rails'.

Rails has to initialize the entire codebase and yours before running the specs. Well, it doesn't have to, but it does. RSpec runs pretty fast for me under my small non-rails projects. You should use autotest which keeps the environment loaded and will check which files you edit. When you edit and save a file, only the tests that depend on these will run automatically and quickly.

If you're using a Mac I recommend using Rspactor over autotest as it uses a lot fewer resources for polling changed files than autotest. There is both a full Cocoa version. While these don't speed up individual rspec tests, they feel much faster as they auto run the affected spec's within a second of you hitting save. If you are on a Windows environment then there is probably little you can do as Rails seems to startup really slowly under Windows.

I had the same experience on Windows and had to move my setup to a Linux VM to make it really zippy I was also using autotest. Asked By: Adrian Dunston. Answered By: John Hinnegan.



0コメント

  • 1000 / 1000