Making a pipeline fast
Measure first
A pipeline that feels slow is not a measurement. Two commands give the numbers that matter: the install and the tests, each timed on the same machine in the same state.
The install, with the bundle already warm
time bundle install
real 0m0.576s user 0m0.463s sys 0m0.112s
The tests
time bundle exec rake test
real 0m0.959s user 0m0.817s sys 0m0.141s
Under a second each, on a machine with the bundle already installed. A cold install is the interesting number: it downloads every gem and it is the step that a cache removes.
Where the seconds go
In a Ruby pipeline the time usually sits in three
places: the bundle
install (network), the test suite (CPU) and the image build (both). A
cache fixes the first, a matrix multiplies the second, and a rebuild
fixes the third.
The wrong move is to skip the tests or the linter to save time. A
pipeline that is fast because it checks less is not faster; it is a
different pipeline, and the first broken release explains the difference.
Parallel jobs and split tests
Jobs that do not depend on each other can run at the same time. In GitHub Actions that is two jobs; in GitLab CI two entries in the same stage:
jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: "3.4" bundler-cache: true - run: bundle exec rake test lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: "3.4" bundler-cache: true - run: bundle exec rubocop
When one job is still the slow one, split it. A test file is a unit of work, so a job can take every third file:
Dir.mkdir("test") unless Dir.exist?("test") %w[01_add 02_done 03_list 04_remove 05_save 06_show].each do |name| File.write("test/#{name}_test.rb", "") end files = Dir["test/*_test.rb"].sort mine = files.each_with_index.select { |_, index| index % 3 == 0 }.map(&:first) abort "nothing to run" if mine.empty? p mine
ruby shard.rb
["test/01_add_test.rb", "test/04_remove_test.rb"]
The files are written by the program so the split is visible without a project around it; a real job ends with exec("ruby", "-Itest", *mine), which replaces the process with the tests of this shard. This project has one test file, so splitting it would not help; the pattern matters for a suite that takes minutes. Split by file and keep the split deterministic, or the same test lands in two jobs and in none.
What to watch for
Cache keys decide how often the cache helps. Key on the lock file, not on
the branch: a feature branch that changes no gem should reuse the same
bundle as the main branch.
Measure before and after each change, on the same machine. A new job
that saves four seconds but doubles the queue time is a net loss, and
only the numbers show it.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the first step of making a pipeline faster?
Answer
Measure the install and the tests on the same machine
A feeling is not a measurement, and the numbers decide which of the three places the seconds sit in.
-
What does this program print?
files = ["a", "b", "c", "d", "e", "f"] mine = files.each_with_index.select { |_, i| i % 3 == 0 }.map(&:first) p mineAnswer
["a", "d"]
The filter keeps the indexes divisible by three, which is one shard of a three-way split.
-
What does this program print?
start = Process.clock_gettime(Process::CLOCK_MONOTONIC) 1_000_000.times { |i| i * 2 } p Process.clock_gettime(Process::CLOCK_MONOTONIC) > startAnswer
true
The clock only moves forward, so the second reading is larger, and the exact duration is a number no page should show.
-
Why is skipping the tests not a way to make a pipeline faster?
Answer
The pipeline is then checking less, which is a different pipeline that fails at the first broken release
Speed comes from caching, parallel jobs and a smaller image, not from removing the checks the pipeline exists for.
-
What does this program print?
files = (1..12).map { |i| "t#{i}" } shards = files.each_slice(3).to_a p shards.sizeAnswer
4
Twelve files in slices of three make four groups, which would be four jobs of the same size.
-
Why key the gem cache on the lock file rather than on the branch?
Answer
A branch that changes no gem should reuse the same bundle as main
The installed set is what the cache holds, and the lock file is its identity, not the branch it was built on.
-
What does this program print?
files = %w[01_add 02_done 03_list 04_remove] p files.each_with_index.select { |_, i| i.even? }.map(&:first)Answer
["01_add", "03_list"]
The even indexes are 0 and 2, which is the other half of a two-way split.
-
Which install is the interesting one to time?
Answer
The cold one, which downloads every gem and is what the cache removes
A warm install is under a second, and the cache exists to turn the cold one into it.
-
What does this program print?
p 1.upto(6).select(&:even?)Answer
[2, 4, 6]
The symbol-to-proc form keeps the even numbers, which is the kind of filter a shard uses.
-
Why measure before and after each change?
Answer
A change can save seconds and still lose more in queue time, and only the numbers show it
Wall-clock time of the whole pipeline is the thing being optimised, not one step in isolation.
Article author: Arthur Isaev