Parallel your specs

A long time ago

many, many commits ago

Build was green

and fast :)

Tests were even faster


But winter finally came

Actually it was summer


and build was slow :-(

Save time

&

get faster feedback

Parallel specs

splitting your test suite


Test suite example


1_fast_spec.rb
2_fast_spec.rb
3_fast_spec.rb
4_fast_spec.rb

5_slow_spec.rb
6_slow_spec.rb
7_slow_spec.rb

8_super_slow_spec.rb

Balancing

split all test files across N nodes:
1. CircleCI example script
2. circleci-specs-in-parallel.sh


Node 1

1_fast_spec.rb
2_fast_spec.rb

Node 2

3_fast_spec.rb
4_fast_spec.rb

Node 3

5_slow_spec.rb
6_slow_spec.rb

Node 4

7_slow_spec.rb

8_super_slow_spec.rb

It ends up like this...


Time for the rescue



let's split tests based on the time execution
with Knapsack gem

The Epic Split



Node 1

8_super_slow_spec.rb



Node 2

7_slow_spec.rb
4_fast_spec.rb
1_fast_spec.rb

Node 3

6_slow_spec.rb
3_fast_spec.rb

Node 4

5_slow_spec.rb

2_fast_spec.rb


How about time


How to set up Knapsack?


# Gemfilegem 'knapsack'


spec_helper.rb


require 'knapsack'Knapsack::Adapters::RspecAdapter.bind


Rakefile


require 'knapsack' Knapsack.load_tasks


Generate time execution report

for your spec files


$ KNAPSACK_GENERATE_REPORT=true \ bundle exec rspec spec


run it on CI machine

Commit generated report

into your repository


knapsack_report.json

Setup your CI server


$ CI_NODE_TOTAL=2 CI_NODE_INDEX=0 \ bundle exec rake knapsack:rspec


Note for CircleCI users

circleci.yml

test:  override:    - bundle exec rake knapsack:rspec: # required colon at the end        parallel: true # 4 spaces indentation

github.com/ArturT/knapsack




badly needed


Happy coding



Sources:
https://circleci.com/docs/parallel-manual-setup
https://gist.github.com/sakatam/7374387

Why knapsack?
http://en.wikipedia.org/wiki/Knapsack_problem



Artur Trzop lunarlogic.io
VII 2014