Parallel tests
in comparable time
Artur Trzop
X 2014
A long time ago
many, many commits ago
Build was green
and fast
![](https://s3.amazonaws.com/media-p.slid.es/uploads/arturt/images/721475/tests_ago.png)
Tests were even faster
![](https://s3.amazonaws.com/media-p.slid.es/uploads/arturt/images/721488/tests_ago_-_rspec.png)
But winter finally came
Actually it was summer
and build was sl w
![](https://s3.amazonaws.com/media-p.slid.es/uploads/arturt/images/721519/slow_build.png)
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:
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...
![](https://s3.amazonaws.com/media-p.slid.es/uploads/arturt/images/721575/without_knapsack.png)
Time for the rescue
let's split tests based on the time execution
with
Knapsack
gem
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 ab ut time
![](https://s3.amazonaws.com/media-p.slid.es/uploads/arturt/images/721647/with_knapsack.png)
How to set up Knapsack?
# Gemfile
gem 'knapsack'
spec_helper.rb
require 'knapsack'
Knapsack::Adapters::RspecAdapter.bind
Rakefile
require 'knapsack'
Knapsack.load_tasks
Generate time execution report for you spec files
run it on CI machine
$ KNAPSACK_GENERATE_REPORT=true \
bundle exec rspec spec
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
That's it.
What else?
Upcoming features
Cucumber support
shippable.com CI support
![](https://s3.amazonaws.com/media-p.slid.es/uploads/arturt/images/721767/knapsack_logo_with_badges.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/arturt/images/721772/knapsack_stars2.png)
Parallel tests in comparable time
By ArturT
Parallel tests in comparable time
Knapsack splits tests across CI nodes and makes sure that tests will run comparable time on each node.
- 3,962