<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.7.2">Jekyll</generator><link href="https://devopsvoyage.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://devopsvoyage.com/" rel="alternate" type="text/html" /><updated>2020-02-26T21:05:16+00:00</updated><id>https://devopsvoyage.com/</id><title type="html">DevOps Voyage</title><subtitle>An excerpt from day to day debugging.</subtitle><author><name>Leszek Zalewski</name><uri>https://devopsvoyage.com</uri></author><entry><title type="html">How to: Execute RSpec in parallel locally</title><link href="https://devopsvoyage.com/2018/10/22/execute-rspec-locally-in-parallel.html" rel="alternate" type="text/html" title="How to: Execute RSpec in parallel locally " /><published>2018-10-22T00:00:00+00:00</published><updated>2018-10-24T12:00:00+00:00</updated><id>https://devopsvoyage.com/2018/10/22/execute-rspec-locally-in-parallel</id><content type="html" xml:base="https://devopsvoyage.com/2018/10/22/execute-rspec-locally-in-parallel.html">&lt;p&gt;Last entry in series about stable and faster test suite. Previous posts were focusing on parallel execution when running the test suite on CI nodes.
This time we will see how we can run it locally as well.
 You can find previous articles here&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/2018/09/12/road-to-fast-and-stable-test-suite.html&quot; title=&quot;How to: Road to fast and stable test suite&quot;&gt;&lt;em&gt;How to: Road to fast and stable test suite&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/2018/09/26/get-most-of-the-database-cleaner.html&quot; title=&quot;How to: Get most of the database cleaner&quot;&gt;&lt;em&gt;How to: Get most of the database cleaner&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;How to: Execute RSpec in parallel locally&lt;/em&gt; (You are here)&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;ParallelTests gem&lt;/h1&gt;

&lt;p&gt;So far we used &lt;a href=&quot;https://docs.knapsackpro.com/ruby/knapsack&quot; title=&quot;Knapsack Gem documentation page&quot;&gt;Knapsack&lt;/a&gt; gem for dividing our tests in order to evenly run them on CI,
but we can&amp;rsquo;t really use it for running them all in parallel locally. This is where &lt;a href=&quot;https://github.com/grosser/parallel_tests&quot; title=&quot;ParallelTests source code and documentation&quot;&gt;&lt;code&gt;ParallelTests&lt;/code&gt;&lt;/a&gt; gem
will come in handy.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ParallelTests splits tests into even groups (by the number of lines or runtime) and runs each group in a single process with its own database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So in short, each process spawned by ParallelTests should have its resources isolated from one another, so they don&amp;rsquo;t interfere with each other.&lt;/p&gt;

&lt;h2&gt;Basic setup&lt;/h2&gt;

&lt;p&gt;Add parallel tests to development and test groups in Gemfile.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;gem&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'parallel_tests'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;group: &lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;%i[development test]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, let us decide on the number of processors You want to use. By default &lt;code&gt;parallel_tests&lt;/code&gt; will set it to
number of CPUs available (i.e. 4 cores with hyperthreading, will count as 8 cores).
If You would like to override it, append &lt;code&gt;[&amp;lt;no of processors to run&amp;gt;]&lt;/code&gt; to tasks below or better,
export environment variable to have it the same for all the tasks, i.e.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PARALLEL_TEST_PROCESSORS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Just remember to put it in something like &lt;code&gt;.bashrc&lt;/code&gt; or &lt;code&gt;.env&lt;/code&gt; file, in order to have it always loaded between sessions.&lt;/p&gt;

&lt;h2&gt;Resources configuration&lt;/h2&gt;

&lt;h3&gt;1. Database&lt;/h3&gt;

&lt;p&gt;I assume You use a database, otherwise, you wouldn&amp;rsquo;t have issues with slow running tests in the first place.
For this to work, we need to update our &lt;code&gt;config/database.yml&lt;/code&gt; with an extended database name.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;na&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;database&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;yourproject_test&amp;lt;%= ENV['TEST_ENV_NUMBER'] %&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will append the processor number to your database name, whenever you will run tests with more than 1 process, i.e.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First process &lt;code&gt;yourproject_test&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Second process &lt;code&gt;yourproject_test1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;etc&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now let&amp;rsquo;s create all databases.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;rake parallel:create
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And load the &lt;code&gt;schema.rb&lt;/code&gt; or &lt;code&gt;structure.sql&lt;/code&gt; to all DBs created above.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;rake parallel:prepare
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;2. Capybara&lt;/h3&gt;

&lt;p&gt;Capybara servers should run on separate ports, i.e.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Capybara&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configure&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;server_port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9887&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'TEST_ENV_NUMBER'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_i&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;3. Other resources&lt;/h3&gt;

&lt;p&gt;You should do the same for any other kind of resource You use, those should be completely separated from each other, i.e.:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;files, i.e. for rails/sprockets cache should lay in different directories&lt;/li&gt;
&lt;li&gt;redis, use different DBs per process&lt;/li&gt;
&lt;li&gt;sphinx, run a separate instance per each processor&lt;/li&gt;
&lt;li&gt;etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Checkout extensive &lt;a href=&quot;https://github.com/grosser/parallel_tests/wiki&quot; title=&quot;ParallelTests wiki page with documentation about various resources setup&quot;&gt;wiki&lt;/a&gt; for details.&lt;/p&gt;

&lt;h2&gt;Lets run this!&lt;/h2&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;rake parallel:spec
&lt;span class=&quot;c&quot;&gt;# =&amp;gt; 8 processes for 500 specs, ~ 62 specs per process&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;figure&gt;
  &lt;img alt=&quot;Specs using full power of all CPUs via htop tool&quot;src=&quot;/assets/images/2018-10-22/cpu-load.gif&quot;/&gt;
  &lt;figcaption&gt;Full throttle!&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Awesome! Now we can run our test suite locally as fast as on CI.&lt;/p&gt;

&lt;h2&gt;Speedup process boot&lt;/h2&gt;

&lt;p&gt;Now when spawning all those processes, each of them will take some time, based on the size of your application.
We can speed this up with &lt;code&gt;spring&lt;/code&gt;, which comes by default with rails installations these days.
To make it work we will have to do a small patch for it, otherwise it won&amp;rsquo;t work with &lt;code&gt;parallel_tests&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is due to the fact that with &lt;code&gt;spring&lt;/code&gt;, when it boot up the server process, the configuration will be already set.
This means DB name will always equal to &lt;code&gt;yourproject_test&lt;/code&gt; in each spawned process based on the server one.&lt;/p&gt;

&lt;p&gt;In order to mitigate this, we will pick up the correct DB configuration
after forking the server process. You can find it in &lt;a href=&quot;https://github.com/grosser/parallel_tests/wiki&quot; title=&quot;ParallelTests wiki page with documentation about various resources setup&quot;&gt;&lt;code&gt;parallel_tests&lt;/code&gt; Wiki&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Create new file under &lt;code&gt;config/spring.rb&lt;/code&gt; - it should get picked up by spring automatically.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'spring/application'&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Spring&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Application&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connect_database_orig&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connect_database&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Disconnect &amp;amp; reconfigure to pickup DB name with&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# TEST_ENV_NUMBER suffix&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;connect_database&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;disconnect_database&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;reconfigure_database&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;connect_database_orig&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Here we simply replace existing AR from main spring process&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reconfigure_database&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;active_record_configured?&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configurations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;no&quot;&gt;Rails&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;database_configuration&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then you can either prepend &lt;code&gt;DISABLE_SPRING=0&lt;/code&gt; to commands, export it, or put in &lt;code&gt;.bashrc&lt;/code&gt;/&lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;With this, &lt;code&gt;bin/rake parallel:spec&lt;/code&gt; will boot up way faster by making use of &lt;code&gt;spring&lt;/code&gt; preloader.
You should notice this by extra lines in output like&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;Running via Spring preloader &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;process 20005
Running via Spring preloader &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;process 20012
Running via Spring preloader &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;process 20015
... etc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Tests distribution&lt;/h2&gt;

&lt;p&gt;Another thing to consider is, how to evenly distribute tests across processes.
&lt;code&gt;parallel_tests&lt;/code&gt; has similar functionality as &lt;code&gt;knapsack&lt;/code&gt;, it can log tests runtime in a JSON file and then use it to spread them evenly across all processes.
To do so add &lt;code&gt;.rspec_parallel&lt;/code&gt; file in project root directory, so on next run it will create the report, and use it in consecutive executions.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nt&quot;&gt;--format&lt;/span&gt; progress
&lt;span class=&quot;nt&quot;&gt;--format&lt;/span&gt; ParallelTests::RSpec::RuntimeLogger &lt;span class=&quot;nt&quot;&gt;--out&lt;/span&gt; tmp/parallel_runtime_rspec.log
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Remember to put any significat config from &lt;code&gt;.rspec&lt;/code&gt; file to &lt;code&gt;.rspec_parallel&lt;/code&gt;, i.e. &lt;code&gt;--require spec_helper&lt;/code&gt; -
as parallel tests will use the later only, what can lead to issues with tests.&lt;/p&gt;

&lt;p&gt;Now, this is good for having local runtime as low as possible, but what if we would like to use the knapsack report, which we already have?
Sadly &lt;code&gt;parallel_tests&lt;/code&gt; doesn&amp;rsquo;t have any integration for it, but we can play around and add it by ourselves - because we can :)&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s create a wrapper task we will use to run it&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/tasks/knapsack.rake&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:knapsack&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;local: :environment&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'CI_NODE_TOTAL'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'PARALLEL_TEST_PROCESSORS'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'RAILS_ENV'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'test'&lt;/span&gt;

    &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'knapsack'&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;require_relative&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'../../config/boot'&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;require_relative&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'parallel_tests_patch'&lt;/span&gt;

    &lt;span class=&quot;no&quot;&gt;ParallelTests&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;CLI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'--type'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'rspec'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'spec'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now a small monkey patch to use knapsack allocator in parallel tests&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/tasks/parallel_tests_patch.rb&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'parallel_tests/rspec/runner'&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ParallelTests&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;RSpec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Runner&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;tests_in_groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tests&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;options&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'ParallelTests with Knapsack runtime report :woohoo:'&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'CI_NODE_INDEX'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_s&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;Knapsack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;AllocatorBuilder&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Knapsack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Adapters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;RSpecAdapter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;allocator&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;node_tests&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s test it!&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;rake knapsack:local
&lt;span class=&quot;c&quot;&gt;# ParallelTests with Knapsack runtime report :woohoo:&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 8 processes for 500 specs, ~ 62 specs per process&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; For local execution I would still use &lt;code&gt;parallel_tests&lt;/code&gt; allocator instead, as it will be generated based on our machine performance, whereas knapsack is supposed to be based on the CI node.&lt;/p&gt;

&lt;h1&gt;Runtime comparison&lt;/h1&gt;

&lt;table&gt;&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Processes&lt;/th&gt;
&lt;th&gt;Spring?&lt;/th&gt;
&lt;th&gt;Runtime log?&lt;/th&gt;
&lt;th&gt;Runtime&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;8m 22.928s&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;8m 55.850s&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;8m 57.309s&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;10m 03.678s&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;9&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;10m 09.448s&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;code&gt;12m 59.819s&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;&lt;code&gt;42m 00.501s&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;As you can see above, our suite runtime without any parallelization takes quite a while, around ~42 minutes.&lt;/p&gt;

&lt;p&gt;When we parallelize it with 8 processes result differs based on extra switches.&lt;/p&gt;

&lt;p&gt;Without the runtime log to evenly distribute tests, take the longest (even with spring support).&lt;/p&gt;

&lt;p&gt;We can also see that having more than 8 processes is also degrading runtime.&lt;/p&gt;

&lt;p&gt;In our case the best results are achieved when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;running against 8 processes on 8 available cores&lt;/li&gt;
&lt;li&gt;run together with runtime logs for tests distribution (&lt;em&gt;-4&amp;#39;30&amp;quot;&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;processes are preloaded by spring (&lt;em&gt;-30&amp;quot;&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This can be due to the fact that we have a lot of IO in tests, in tests and appications that do heavy computing, less
processes can actually give better results. As usuall measure, compare and take the most performant option ;-)&lt;/p&gt;

&lt;h1&gt;Summary and what&amp;rsquo;s next&lt;/h1&gt;

&lt;p&gt;From now on it should be easy to run Your test suite in parallel, both locally and on CI with help of &lt;code&gt;knapsack&lt;/code&gt; and &lt;code&gt;parallel_tests&lt;/code&gt; gems.&lt;/p&gt;

&lt;p&gt;The test suite itself should be also faster and more stable thanks to better usage of &lt;code&gt;DatabaseCleaner&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thankfully Rails 6 should bring us built-in support for&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;running tests in parallel&lt;/li&gt;
&lt;li&gt;better transactions handling&lt;/li&gt;
&lt;li&gt;and multi-database connection support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we won&amp;rsquo;t have to hack our way through in new apps, for old ones running on Rails &lt;code&gt;&amp;lt;= 5&lt;/code&gt; we are already covered.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stay tuned and happy hacking!&lt;/em&gt;&lt;/p&gt;</content><author><name>Leszek Zalewski</name><uri>https://devopsvoyage.com</uri></author><category term="ruby" /><category term="rspec" /><category term="testing" /><category term="parallel" /><summary type="html">Previous posts were focusing on parallel execution when running the test suite on CI nodes. This time we will see how we can run it locally as well.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://devopsvoyage.com/assets/images/2018-10-22/parallel.jpg" /></entry><entry><title type="html">How to: Get most of the database cleaner</title><link href="https://devopsvoyage.com/2018/09/26/get-most-of-the-database-cleaner.html" rel="alternate" type="text/html" title="How to: Get most of the database cleaner" /><published>2018-09-26T00:00:00+00:00</published><updated>2018-09-27T09:52:28+00:00</updated><id>https://devopsvoyage.com/2018/09/26/get-most-of-the-database-cleaner</id><content type="html" xml:base="https://devopsvoyage.com/2018/09/26/get-most-of-the-database-cleaner.html">&lt;p&gt;In the &lt;a href=&quot;/2018/09/12/road-to-fast-and-stable-test-suite.html&quot; title=&quot;How to: Road to fast and stable test suite&quot;&gt;previous post&lt;/a&gt;, we saw how we can divide and speed up test suite by using &lt;a href=&quot;https://docs.knapsackpro.com/ruby/knapsack&quot; title=&quot;Knapsack Gem documentation page&quot;&gt;Knapsack gem&lt;/a&gt; from ~1 hour to 11 minutes.
We also cached our dependencies on CI and started looking out for randomly failing tests.
This time we will take a look at how to setup DatabaseCleaner in order to make use of different
strategies for wiping out data between test runs in order to squeeze in even more from the suite.&lt;/p&gt;

&lt;h1&gt;DatabaseCleaner gem&lt;/h1&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/DatabaseCleaner/database_cleaner&quot; title=&quot;DatabaseCleaner repository page on Github&quot;&gt;DatabaseCleaner&lt;/a&gt; is a set of strategies for cleaning your database in Ruby.
It&amp;rsquo;s a simple yet powerful tool that You most probably already use. Even though it&amp;rsquo;s simple,
its configuration can get tricky at times. Messing up configuration can cause extra failures
related to database pollution. So let us see how we can set it up in order to have a stable test suite.&lt;/p&gt;

&lt;h2&gt;Basic setup&lt;/h2&gt;

&lt;p&gt;Add database cleaner to test group in Gemfile (if it&amp;rsquo;s not yet there).&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:test&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;gem&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'database_cleaner'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Make sure you load &lt;em&gt;rspec/support&lt;/em&gt; helpers, it&amp;rsquo;s loaded by default with RSpec setup in &lt;code&gt;spec/rails_helper&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# depending on age of the project look into rails or spec helper file&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Rails&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'spec'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'support'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'**'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'*.rb'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Create &lt;code&gt;spec/support/database_cleaner.rb&lt;/code&gt; with&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;RSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configure&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:suite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clean_with&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:deletion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is a good starting point, it will make sure the database is clean before the whole suite starts in case
there are any leftovers from previous run.&lt;/p&gt;

&lt;h2&gt;Transactions - fastest all-rounder&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Transactions&lt;/strong&gt; should be used by default, when you need to use database calls it&amp;rsquo;s the fastest strategy available.
DB Cleaner will open new transaction on each test case and roll it back when it&amp;rsquo;s finished. This way it doesn&amp;rsquo;t
need to truncate all the tables, even though You used just one.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;RSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configure&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:transaction&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clean&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Deletion - rescue for after commit callbacks&lt;/h2&gt;

&lt;p&gt;Sometimes transactions are not enough, especially when you use after commit callbacks in ActiveRecord.
As the name implies those are executed after committing the transaction. This can&amp;rsquo;t work with the previous
strategy as transaction would still be open on the DB Cleaner level.&lt;/p&gt;

&lt;p&gt;For this kind of unit tests where you touch DB and need access to data after committing the transaction,
you can use &lt;strong&gt;deletion&lt;/strong&gt; strategy. Which won&amp;rsquo;t be significantly slower than the &lt;em&gt;transaction&lt;/em&gt; for small data sets.&lt;/p&gt;

&lt;p&gt;To fix this up without switching all the tests to &lt;em&gt;deletion&lt;/em&gt;, we can use RSpec examples metadata,
in order to pick correct strategy on demand.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:strategy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:transaction&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With this whenever we need to use the deletion strategy, we will just add &lt;code&gt;strategy: :deletion&lt;/code&gt;, like:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# for whole block&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;executes after commit callbacks&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;strategy: :deletion&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# or a single example&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;creates thumbnails on save&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;strategy: :deletion&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Truncation - safe acceptance examples&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Truncation&lt;/strong&gt; is the &lt;em&gt;slowest&lt;/em&gt; but also the &lt;em&gt;safest&lt;/em&gt; and most stable strategy.
There are no compromises when it comes to clean state here.
It&amp;rsquo;s the best strategy for acceptance tests when using capybara with javascript driver.&lt;/p&gt;

&lt;p&gt;Again we can make use of metadata, and use it by default for all js related tests.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;ss&quot;&gt;:truncation&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:strategy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:transaction&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# ...  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Truncation or Deletion?&lt;/h2&gt;

&lt;p&gt;Rule of thumb should be:&lt;/p&gt;

&lt;p&gt;For big setups before the tests, when you create a lot of objects with associations that use foreign keys, etc -
use &lt;strong&gt;truncation&lt;/strong&gt;, as it&amp;rsquo;s a fixed time regardless of the amount of data. This also means the more tables you have
the more time it will take, as it takes the same amount of time for empty tables as well. You can think about it as
drop plus create table, indexes, etc.&lt;/p&gt;

&lt;p&gt;On the other hand, when you have a simple setup, with unit alike tests - use &lt;strong&gt;deletion&lt;/strong&gt; as it&amp;rsquo;s faster for small
datasets, it won&amp;rsquo;t recreate tables or indexes.&lt;/p&gt;

&lt;p&gt;If you are interested in details, then &lt;a href=&quot;https://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886&quot; title=&quot;Answer by Craig Ringer for &amp;#39;Postgresql Truncation speed&amp;#39; question on StackOverflow&quot;&gt;here&amp;rsquo;s a great answer for PostgreSQL on StackOverflow&lt;/a&gt; explaining it thoroughly.&lt;/p&gt;

&lt;h2&gt;Bonus no.1: Cleaning on demand&lt;/h2&gt;

&lt;p&gt;Life is hard, and usually, you will find yourself with a really big setup of objects. This can happen especially
when you try to test some kind of filtering or querying classes, which do read-only queries against data,
without any side effects. In such cases, it would be great if we could disable DB Cleaner per example,
and run it only once in &lt;code&gt;before/after&lt;/code&gt; all blocks. Again this is easy to achieve with examples metadata, lets us see.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;  &lt;span class=&quot;c1&quot;&gt;# before :suite ...&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:truncation&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clean&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clean&lt;/span&gt;  
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Lets go into detail what do we do here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;before :all, :cleaner_for_context&lt;/code&gt; block, will run once for whole context/describe block. We used truncation, as it will usually be faster in such examples.&lt;/li&gt;
&lt;li&gt;We skip &lt;code&gt;before&lt;/code&gt; and &lt;code&gt;after :each&lt;/code&gt; blocks whenever we set &lt;code&gt;:cleaner_for_context&lt;/code&gt; metadata.&lt;/li&gt;
&lt;li&gt;We clean database once for whole context/describe block with &lt;code&gt;after :all&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then in our test, we can do&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;RSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;describe&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;CoreDashboardQuery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;before&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:all&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@data&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# it ...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Remember that whenever you use before/after all blocks, those are run &lt;strong&gt;once&lt;/strong&gt; for the given context/describe block, so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;don&amp;rsquo;t do any data updates or additions within those blocks, unless you want to shoot yourself in the foot with a canon&lt;/li&gt;
&lt;li&gt;whenever you use this technique, create the setup on top of the file, don&amp;rsquo;t hide it somewhere nested on the bottom&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Bonus no.2: Catch data pollution issues&lt;/h2&gt;

&lt;p&gt;Getting back to our test suite example, which is still &lt;em&gt;dark and full of terrors&lt;/em&gt;. We had random failures that were related to test
leftovers in the database. Those were caused in examples that were running with &lt;code&gt;:transaction&lt;/code&gt; strategy, were &lt;code&gt;before :all&lt;/code&gt; block
was lurking in the depths of nested context blocks.&lt;/p&gt;

&lt;p&gt;The easiest way to find those is to crash loudly whenever the database is not clean in &lt;code&gt;after :all&lt;/code&gt; block. To know what to count,
look out for errors like &lt;em&gt;&amp;ldquo;Couldn&amp;rsquo;t create Project because of unique key validation&amp;rdquo;&lt;/em&gt; or similar. To catch those drop this line in our setup.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# still in our spec/support/database_cleaner.rb file&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;RSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configure&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DirtyDatabaseError&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RuntimeError&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:full_description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\t&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clean&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;DirtyDatabaseError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Project&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will crash as soon as there are records still in DB, and will print example which caused it:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;Failures:

  1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; ProjectsQuery#build_query sorts projects by title
     Failure/Error: raise DirtyDatabaseError.new&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;example.metadata&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if &lt;/span&gt;Project.count &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0

     DirtyDatabaseError:
        ProjectsQuery#build_query sorts projects by title
         ./spec/queries/projects_query_spec.rb:278
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Of course, you don&amp;rsquo;t want to run it every time,
so just drop it in whenever You see some suspicious errors.&lt;/p&gt;

&lt;h1&gt;Summary&lt;/h1&gt;

&lt;p&gt;Our final DatabaseCleaner configuration for RSpec.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DirtyDatabaseError&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RuntimeError&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:full_description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\t&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;RSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configure&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:suite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clean_with&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:deletion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:truncation&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strategy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;ss&quot;&gt;:truncation&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:strategy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:transaction&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clean&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# raise DirtyDatabaseError.new(example.metadata) if Record.count &amp;gt; 0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:cleaner_for_context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;DatabaseCleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clean&lt;/span&gt;  
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In our case, we were using &lt;code&gt;:truncation&lt;/code&gt; for everything. In order to speed things up, we added above test configuration to use &lt;code&gt;:transaction&lt;/code&gt;
by default for everything, and we left &lt;code&gt;:truncation&lt;/code&gt; for JS tests only.&lt;/p&gt;

&lt;p&gt;We also used &lt;code&gt;:deletion&lt;/code&gt; instead of &lt;code&gt;:truncation&lt;/code&gt; for unit alike tests,
that used after commit callbacks, i.e. attachment objects that are processing files when it&amp;rsquo;s saved. In those cases, the deletion was taking a
fraction of the time of truncation.&lt;/p&gt;

&lt;p&gt;On the way, we found out which tests were polluting database, and we were able to fix it easily. When we
finished the move, our suite runtime dropped by another 2 minutes and now stays stable at ~9 minutes per job.&lt;/p&gt;

&lt;p&gt;Next time we will try making use of knapsack to run the test suite in parallel locally, so &lt;em&gt;stay tuned and happy hacking!&lt;/em&gt;&lt;/p&gt;</content><author><name>Leszek Zalewski</name><uri>https://devopsvoyage.com</uri></author><category term="ruby" /><category term="rspec" /><category term="testing" /><category term="database" /><summary type="html">The second part from fast and stable test suite series. We will see how various strategies for cleaning the database in tests can speed up test suite even more.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://devopsvoyage.com/assets/images/2018-09-26/drift.jpg" /></entry><entry><title type="html">How to: Road to fast and stable test suite</title><link href="https://devopsvoyage.com/2018/09/12/road-to-fast-and-stable-test-suite.html" rel="alternate" type="text/html" title="How to: Road to fast and stable test suite" /><published>2018-09-12T00:00:00+00:00</published><updated>2018-09-27T09:52:28+00:00</updated><id>https://devopsvoyage.com/2018/09/12/road-to-fast-and-stable-test-suite</id><content type="html" xml:base="https://devopsvoyage.com/2018/09/12/road-to-fast-and-stable-test-suite.html">&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;It&amp;rsquo;s an extension for my presentation from Ruby User Group Berlin meetup in September, 2018, &lt;a href=&quot;https://slides.com/zalesz/testing-sanity-p1&quot;&gt;You can find slides here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A beginning of the story on how we speed up test suite in one of the projects at Akelius from one hour to less than 10 minutes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;: &lt;em&gt;Divide and Conquer&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;start with making tests run in &lt;em&gt;parallel&lt;/em&gt; in the easiest possible manner&lt;/li&gt;
&lt;li&gt;start tracking tests that fail in each of the subsets of tests&lt;/li&gt;
&lt;li&gt;reproduce and fix issues locally&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Why?&lt;/h2&gt;

&lt;p&gt;I don&amp;rsquo;t think it needs to be said out loud but here it goes - &lt;em&gt;slow and flaky test suites are bad for you!&lt;/em&gt;
If Your suite runs for &lt;em&gt;over&lt;/em&gt; 10 minutes you are hurting yourself and your team.
But let us start from the beginning. When I started in Akelius, application test suite was running for an hour.
Yes, you read it correctly - an &lt;strong&gt;HOUR&lt;/strong&gt;. It also had order dependant test failures, so 1 in 4 builds could randomly fail.&lt;/p&gt;

&lt;figure&gt;
  &lt;img alt=&quot;Slow, failed build on Travis CI&quot; src=&quot;/assets/images/2018-09-12/01-slow-failure.png&quot;/&gt;
  &lt;figcaption&gt;Oh no, not again!&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;By using Github checks, that CI needs to pass in order to merge a Pull Request, this would basically block the deployment to next stages.
Due to a random failure, the build would be usually restarted in order to move forward - which doesn&amp;rsquo;t mean it would be green on the next run.&lt;/p&gt;

&lt;figure&gt;
  &lt;img alt=&quot;Slow, successfull build on Travis CI&quot; src=&quot;/assets/images/2018-09-12/02-slow-success.png&quot; /&gt;
  &lt;figcaption&gt;Finally, some time later, after 1-3 restarts.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;This resulted in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;very long feedback loops&lt;/li&gt;
&lt;li&gt;lots of distractions due to context switching&lt;/li&gt;
&lt;li&gt;frustration&lt;/li&gt;
&lt;li&gt;and a useless test suite, which was more of a drag than a help&lt;/li&gt;
&lt;li&gt;cost us lots of time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So how did we handle this? We had to speed it up first, in order to easier track down random test failures.
How to do it with least effort in the ruby world?&lt;/p&gt;

&lt;h2&gt;Enter: knapsack gem&lt;/h2&gt;

&lt;figure&gt;
  &lt;img alt=&quot;Ruby gem 'Knapsack' logo&quot; src=&quot;/assets/images/2018-09-12/03-knapsack.png&quot; /&gt;
  &lt;figcaption&gt;Knapsack splits tests across CI nodes and makes sure that tests will run comparable time on each node.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.knapsackpro.com/ruby/knapsack&quot; title=&quot;Knapsack Gem documentation page&quot;&gt;Knapsack&lt;/a&gt; doesn&amp;rsquo;t require any extra setup for additional services you are using, like database or chrome.
Instead, it relies completely on the isolation provided by CI nodes.
The only logic involved is &lt;strong&gt;how to evenly divide test files across nodes, so nodes run time is balanced&lt;/strong&gt;,
i.e. all nodes finish in ~5 minutes, instead of one finishes in 2 minutes and another one in 8.&lt;/p&gt;

&lt;p&gt;Installation is quite easy and documentation covers a variety of ruby test runners.&lt;/p&gt;

&lt;h2&gt;Parallelize tests with the knapsack&lt;/h2&gt;

&lt;p&gt;Add gem in Gemfile.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:development&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;gem&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;knapsack&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Add tasks in Rakefile.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Knapsack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;load_tasks&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defined?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Knapsack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Bind knapsack in top of spec helper file.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;knapsack&quot;&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Knapsack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Adapters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;RSpecAdapter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bind&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Generate report, so knapsack will know runtime of the specific test files and will be able to evenly split them across nodes.
It&amp;rsquo;s &lt;strong&gt;best to run it on actual CI node&lt;/strong&gt;, so results will be closest to real ones.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;c&quot;&gt;# locally&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;KNAPSACK_GENERATE_REPORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true &lt;/span&gt;bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;rspec spec
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# or on CI, i.e. edit .travis.yml file&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;KNAPSACK_GENERATE_REPORT=true&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bundle&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;rspec&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;spec&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When the build completes, it will print out the new JSON report which will look something like:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;spec/models/supply_spec.rb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.7876174449920654&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;spec/models/tax_spec.rb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.22003436088562012&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;spec/models/text_document_spec.rb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.3623762130737305&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;spec/models/user_spec.rb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;318.7685122489929&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Commit report to the repository in &lt;code&gt;knapsack_rspec_report.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;The last step is to actually configure CI setup.
In our case it was TravisCI, but you can find docs for CircleCI and others as well, on &lt;a href=&quot;https://docs.knapsackpro.com/ruby/knapsack&quot; title=&quot;Knapsack Gem documentation page&quot;&gt;knapsack documentation page&lt;/a&gt;.
Here we say that we run our tests across 8 workers.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;global&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;RAILS_ENV=test&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;CI_NODE_TOTAL=8&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# total number of workers&lt;/span&gt;

  &lt;span class=&quot;na&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;CI_NODE_INDEX=0&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;CI_NODE_INDEX=1&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# ... 5 more vars&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;CI_NODE_INDEX=7&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bundle&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;rake&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;knapsack:rspec&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now lets watch them fly! 🚀&lt;/p&gt;

&lt;figure&gt;
  &lt;img alt=&quot;Fast and successful test run on CI in under 12 minutes across 8 CI workers&quot; src=&quot;/assets/images/2018-09-12/04-first-time-flying-tests.png&quot;/&gt;
  &lt;figcaption&gt;First runtime improvement, from ~60 to ~12 minutes.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3&gt;Bonus #1: Outsource linters&lt;/h3&gt;

&lt;p&gt;In our case we were using linters during CI run. By moving to CodeClimate, we shaved another ~30 seconds per build.&lt;/p&gt;

&lt;p&gt;Afterwards we could let go 4 gems from Gemfile, which reduced total number of dependencies by 11.&lt;/p&gt;

&lt;h3&gt;Bonus #2: Use headless chrome and cache dependencies&lt;/h3&gt;

&lt;p&gt;Always try to prefer dependencies, that come preloaded on CI machines and make use of them.
This will usually result in extra bonus seconds, that can be spent running suite, instead of installing dependencies by hand.&lt;/p&gt;

&lt;p&gt;One of such dependencies is chrome, which we use for acceptance tests.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# .travis.yml&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# remove&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;before_install&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;sudo apt-get install chromium-browser&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# add&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;addons&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;chrome&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;stable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And if you are using headless chrome, instead of &lt;code&gt;xvfb&lt;/code&gt;, you need to download the chromedriver for it to work.
We make use of builtin caching mechanism, to download it, only if it&amp;rsquo;s missing in the cached directory.
With this we saved extra ~20s per build. And we can easily install newer version if needed.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# .travis.yml&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;bundler&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;directories&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;~/bin&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;before_install&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;./bin/ci_install_chromedriver&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In &lt;code&gt;bin/ci_install_chromedriver&lt;/code&gt; we specify the script for checking and installing chromedriver.
We test against specific version, which will help invalidate the cache when we update.
You can use something simillar for other dependencies as well.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-Eeuo&lt;/span&gt; pipefail

&lt;span class=&quot;nv&quot;&gt;VERSION&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;2.41&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;NAME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;chromedriver_&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;VERSION&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;STORAGE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;https://chromedriver.storage.googleapis.com

install_chromedriver&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; ~/bin/chromedriver&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;

  wget &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;STORAGE&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;VERSION&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/chromedriver_linux64.zip

  unzip chromedriver_linux64.zip
  &lt;span class=&quot;nb&quot;&gt;rm &lt;/span&gt;chromedriver_linux64.zip
  &lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; chromedriver ~/bin/&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x ~/bin/&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nb&quot;&gt;ln&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; ~/bin/&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; ~/bin/chromedriver
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; ~/bin/&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; install_chromedriver
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Track and fix random failures&lt;/h2&gt;

&lt;p&gt;Since we now have fast running test suite, we don&amp;rsquo;t only fail fast, we also reproduce the problem fast as well - as we now know the subset of tests that&amp;rsquo;s causing the failure.
In order to reproduce those locally, we first need to find the job that failed, its &lt;code&gt;CI_NODE_INDEX&lt;/code&gt;, and rspec order &lt;code&gt;seed&lt;/code&gt;.
First one decides which files to run, second dictates in which order those are run.&lt;/p&gt;

&lt;figure&gt;
  &lt;img alt=&quot;One of the 8 jobs failed on Travis CI&quot; src=&quot;/assets/images/2018-09-12/05-flying-tests-fail.png&quot; /&gt;
  &lt;figcaption&gt;We now can run a subset of tests, which takes only ~8 minutes instead of 60.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;After finding the job, you should be able to locate the &lt;code&gt;seed&lt;/code&gt;, at the bottom of the failed test output.&lt;/p&gt;

&lt;figure&gt;
  &lt;img alt=&quot;Bottom of failed test output listing failed tests case and showing rspec seed which was used for ordering tests&quot;
       src=&quot;/assets/images/2018-09-12/06-copy-rspec-seed.png&quot; /&gt;
  &lt;figcaption&gt;**4049** is what we are looking for.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Now, we can easily reproduce it locally, by running following command.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;CI_NODE_TOTAL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
   &lt;span class=&quot;nv&quot;&gt;CI_NODE_INDEX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
   bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;rake &lt;span class=&quot;s2&quot;&gt;&quot;knapsack:rspec[--seed 4049]&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can also try using &lt;a href=&quot;https://relishapp.com/rspec/rspec-core/v/3-8/docs/command-line/bisect#use-%60--bisect%60-flag-to-create-a-minimal-repro-case-for-the-ordering-dependency&quot; title=&quot;RSpec bisect flag documentation&quot;&gt;RSpec &lt;code&gt;--bisect&lt;/code&gt; flag&lt;/a&gt;, to create a minimal repro case for the ordering dependency.
It will usually also find other issues on the way and take some time depending on the test suite.
Usually it&amp;rsquo;s worth the wait, and if it&amp;rsquo;s taking too long, you can always get results so far by hitting &lt;code&gt;CTRL+C&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;CI_NODE_TOTAL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
   &lt;span class=&quot;nv&quot;&gt;CI_NODE_INDEX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
   bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;rake &lt;span class=&quot;s2&quot;&gt;&quot;knapsack:rspec[--seed 4049 --bisect]&quot;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# =&amp;gt;&lt;/span&gt;

Report specs:
spec/models/user_spec.rb
&lt;span class=&quot;c&quot;&gt;# and a load of other tests scoped by knapsack&lt;/span&gt;

Leftover specs:

Running via Spring preloader &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;process 17947
Bisect started using options: &lt;span class=&quot;s2&quot;&gt;&quot;--seed 4049 --default-path spec -- spec/models/user_spec.rb and a load of other tests scoped by knapsack&quot;&lt;/span&gt;
Running suite to find failures... &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;4 minutes 23.9 seconds&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Starting bisect with 6 failing examples and 921 non-failing examples.
Checking that failure&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; are order-dependent... failure&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do &lt;/span&gt;not require any non-failures to run first

Bisect &lt;span class=&quot;nb&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; Reduced necessary non-failing examples from 921 to 0 &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;12.51 seconds.

The minimal reproduction &lt;span class=&quot;nb&quot;&gt;command &lt;/span&gt;is:
  rspec ./spec/features/attachment_spec.rb[1:1:1,1:2,1:3] ./spec/features/project_spec.rb[1:1] ... &lt;span class=&quot;nt&quot;&gt;--seed&lt;/span&gt; 4049 &lt;span class=&quot;nt&quot;&gt;--default-path&lt;/span&gt; spec &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We had one example failing in the job, and we have found &lt;em&gt;6 in total&lt;/em&gt; by running it with &lt;code&gt;--bisect&lt;/code&gt; flag.&lt;/p&gt;

&lt;h2&gt;Fix the issue and repeat&lt;/h2&gt;

&lt;p&gt;This is subject for whole new post, or most probably, even a book. It will mostly depend on the test suite, but in general look out for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if using Rails, autoloading issues, especially when you have deeply nested structures like ActiveRecord models and Single Table Inheritance&lt;/li&gt;
&lt;li&gt;using sleep statements, commonly found in acceptance tests&lt;/li&gt;
&lt;li&gt;database pollution, i.e. some deeply nested &lt;code&gt;before :all&lt;/code&gt; blocks, which run once for the whole file and not in the scope of nested describe/context blocks&lt;/li&gt;
&lt;li&gt;make sure that the order of the elements doesn&amp;rsquo;t play a role in tests that don&amp;rsquo;t demand it, i.e. results in arrays&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If You can&amp;rsquo;t fix them, as they happen, don&amp;rsquo;t blindly restart the build. Instead note down the &lt;code&gt;CI_NODE_INDEX&lt;/code&gt; and &lt;code&gt;seed&lt;/code&gt;.
You can use some simple &lt;em&gt;TODO&lt;/em&gt; list, but from my experience it&amp;rsquo;s better to plan it properly.
For example, create maintenance ticket in JIRA (or in project management tool you are using), keep track of it, write down other failed examples.
So when the time comes, You can fix them in one go or assign as subtasks within the team.&lt;/p&gt;

&lt;h2&gt;Profit &amp;amp; Sanity&lt;/h2&gt;

&lt;p&gt;After the first improvements, tests were running around 49 minutes faster per build, which resulted in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;faster iterations on the tasks at hand&lt;/li&gt;
&lt;li&gt;running the whole suite more often without the fear of failure&lt;/li&gt;
&lt;li&gt;fewer distractions and context switching due to waiting for the results&lt;/li&gt;
&lt;li&gt;team happiness and time to delivery :)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There was one small drawback though - by using 8 workers, we block most of our available CI capacity.
You can probably already imagine, pushing one commit after another and blocking CI for everyone.
This is something to look out for, also maybe there are ways to cancel previous builds on the same branch.
We will investigate it in future when it becomes a bottleneck.&lt;/p&gt;

&lt;h2&gt;Next steps&lt;/h2&gt;

&lt;p&gt;First improvements resulted in a build time of around 11 minutes, in next article we will go over how we shaved another 2 minutes, by using a mix of different strategies for cleaning up a database.
We will also explore Jenkins, as a CI setup with kubernetes support and using &lt;em&gt;docker images&lt;/em&gt; for build environments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stay tuned and happy hacking!&lt;/em&gt;&lt;/p&gt;</content><author><name>Leszek Zalewski</name><uri>https://devopsvoyage.com</uri></author><category term="ruby" /><category term="rspec" /><category term="testing" /><category term="continuous integration" /><summary type="html">A beginning of the story on how we speed up test suite in one of the projects at Akelius from one hour to less than 10 minutes.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://devopsvoyage.com/assets/images/2018-09-12/road.jpg" /></entry></feed>