Knapsack splits tests evenly across parallel CI nodes to run fast CI build and save you time.
Knapsack splits tests evenly across parallel CI nodes to run fast CI build and save you time.
[!WARNING] Knapsack is archived. But Knapsack Pro is available.
Knapsack Pro comes with a free plan for OSS projects and discounts on paid plans for people coming from Knapsack (see how to migrate in 10 minutes).
This repository remains available to fork and the gem hosted on RubyGems, so your existing setup won't be affected.
Speed up your tests
Run your 1-hour test suite in 2 minutes with optimal parallelisation on your existing CI infrastructure
Knapsack wraps your current test runner and works with your existing CI infrastructure to split tests optimally.
It comes in two flavors, knapsack and knapsack_pro:
knapsack |
knapsack_pro |
|
|---|---|---|
| Free | ✅ | ✅ OSS projects |
| Static split | ✅ | ✅ |
| Dynamic split | ❌ | ✅ |
| Split by test examples | ❌ | ✅ |
| Graphs, metrics, and stats | ❌ | ✅ |
| Programming languages | (Ruby) | ✅ (Ruby, Cypress, Jest, SDK/API) |
| CI providers | Limited | ✅ (All) |
| Heroku add-on | ❌ | ✅ |
| Automated execution time recording | ❌ | ✅ |
| Test split based on most recent execution times | ❌ | ✅ |
| Support for spot/preemptible CI nodes | ❌ | ✅ |
| Additional features | ❌ | (Overview) |
| Install | Install |
knapsack to knapsack_proIf you already use knapsack and want to give knapsack_pro a try, here's how to migrate in 10 minutes.
knapsackKnapsack generates a test time execution report and uses it for future test runs.
The knapsack gem supports:
>= Ruby 2.1.0
Please check CHANGELOG.md before updating the gem. Knapsack follows semantic versioning.
Add these lines to your application's Gemfile:
group :test, :development do
gem 'knapsack'
endAnd then execute:
bundleAdd this line at the bottom of Rakefile:
Knapsack.load_tasks if defined?(Knapsack)Here's an example of a Rails app with Knapsack.
https://github.com/KnapsackPro/rails-app-with-knapsack
Add at the beginning of your spec_helper.rb:
require 'knapsack'
# CUSTOM_CONFIG_GOES_HERE
Knapsack::Adapters::RSpecAdapter.bindCreate features/support/knapsack.rb:
require 'knapsack'
# CUSTOM_CONFIG_GOES_HERE
Knapsack::Adapters::CucumberAdapter.bindAdd the Knapsack code after you load the app environment in test/test_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'knapsack'
# CUSTOM_CONFIG_GOES_HERE
knapsack_adapter = Knapsack::Adapters::MinitestAdapter.bind
knapsack_adapter.set_test_helper_path(__FILE__)Create features/support/env.rb:
require 'knapsack'
# CUSTOM_CONFIG_GOES_HERE
Knapsack::Adapters::SpinachAdapter.bindYou can change the default Knapsack configuration for RSpec, Cucumber, Minitest, or Spinach tests.
Here are some examples (that you can insert in CUSTOM_CONFIG_GOES_HERE):
Knapsack.tracker.config({
enable_time_offset_warning: true,
time_offset_in_seconds: 30
})
Knapsack.report.config({
test_file_pattern: 'spec/**{,/*/**}/*_spec.rb', # default value based on adapter
report_path: 'knapsack_custom_report.json'
})
# You can use your logger:
require 'logger'
Knapsack.logger = Logger.new(STDOUT)
Knapsack.logger.level = Logger::INFOGenerate the time execution report for your test files. Run the command below on one of your CI nodes:
# Step for RSpec:
KNAPSACK_GENERATE_REPORT=true bundle exec rspec spec
# Step for Cucumber:
KNAPSACK_GENERATE_REPORT=true bundle exec cucumber features
# Step for Minitest:
KNAPSACK_GENERATE_REPORT=true bundle exec rake test
# If you use Rails 5.0.x then run this instead:
KNAPSACK_GENERATE_REPORT=true bundle exec rake test
# If you use Rails >= 5.1's SystemTest, run both unit and system tests:
KNAPSACK_GENERATE_REPORT=true bundle exec rake test test:system
# Step for Spinach:
KNAPSACK_GENERATE_REPORT=true bundle exec spinachCommit the generated report knapsack_rspec_report.json, knapsack_cucumber_report.json, knapsack_minitest_report.json or knapsack_spinach_report.json into your repository.
This report should be updated after you add a lot of new slow tests or you change existing ones, which causes a big time execution difference between CI nodes.
You will get a time offset warning at the end of the RSpec/Cucumber/Minitest run, which reminds you when it’s a good time to regenerate the Knapsack report.
KNAPSACK_GENERATE_REPORT is truthy with "true" or 0. All other values are falsy, though "false" and 1 are semantically preferrable.
There is no need to regenerate the report every time you add/remove test files.
If you remove a test file, Knapsack will ignore its entry in the report. If you add a new test file that is not listed in the report, the test file will be assigned to one of the CI nodes.
You'll want to regenerate your execution report whenever you remove or add a test file with a long time execution time that would affect one of the CI nodes. Knapsack warns you when it's a good time to regenerate the report.
On your CI server, run the following command for the first CI node (increase CI_NODE_INDEX for the next nodes):
# Step for RSpec:
CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:rspec
# Step for Cucumber:
CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:cucumber
# Step for Minitest:
CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:minitest
# Step for Spinach:
CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:spinachYou can add KNAPSACK_TEST_FILE_PATTERN if your tests are not in the default directory:
…You can set KNAPSACK_REPORT_PATH if your Knapsack report was saved in a non-default location:
# Step for RSpec:
KNAPSACK_REPORT_PATH="knapsack_custom_report.json" CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:rspec
# Step for Cucumber:
KNAPSACK_REPORT_PATH="knapsack_custom_report.json" CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:cucumber
# Step for Minitest:
KNAPSACK_REPORT_PATH="knapsack_custom_report.json" CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:minitest
# Step for Spinach:
KNAPSACK_REPORT_PATH="knapsack_custom_report.json" CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:spinachCI_NODE_TOTAL - total number of CI nodes you have.
CI_NODE_INDEX - index of the current CI node starting from 0 (ie, the second CI node should have CI_NODE_INDEX=1).
Some CI providers like GitLab CI have the same name of environment variable like CI_NODE_INDEX, which starts from 1 instead of 0. Knapsack will automatically pick it up and change it from 1 to 0.
Knapsack allows you to pass arguments through to RSpec. For example, if you want to run only specs that have the tag focus. If you do this with RSpec directly, it would look like:
bundle exec rake rspec --tag focusTo do this with Knapsack, you simply add your RSpec arguments as parameters to the Knapsack Rake task:
bundle exec rake "knapsack:rspec[--tag focus]"Remember that using tags to limit which specs get run will affect the time each file takes to run. One solution to this is to generate a new knapsack_rspec_report.json for the commonly run scenarios.
bundle exec rake "knapsack:cucumber[--name feature]"No open issues yet, or sync has not completed.