Patryk Ptasiński discusses 3 issues he encountered in the Ruby on Rails project with long-running tests. He presents solutions for improving test performance, followed by benchmarks and examples. Finally, he lists recommendations for writing fast tests for large Ruby on Rails projects.
Post Quantum Cryptography – The Impact on Identity
3 issues that made 30 test workers take 40 minutes
1. 3 issues that made 30 test
workers take 40 minutes
brought to you by
Patryk Ptasiński
2. wroc_love.rb presentation
Optimizing pro tests was inspired by presentation I
watched in person on wroc_love.rb ruby conference. I
highly recommend watching it whole:
https://www.youtube.com/watch?v=xTBBrPUJjGk
2
7. Issue nr 1 - very big test file
1. We use 30 test workers and we are splitting test
files among them
2. Some files take very long time to process
3. We cannot split big files*
4. Worker with index 15 always got 2 big rspec files
7
8. Issue nr 1 - solutions
1. Paid but advanced and really good: Knapsack PRO
2. Quick, simple and easy: change order with built-in
circle ci tool
3. Custom
8
11. Issue nr 1 - solution 3
Write custom splitter - goals:
1. Split big test files (one big file takes many minutes to run)
2. Keep small files together if possible
3. Have control on what is “big” and what is “small”
4. Other benefits:
a. One time cost - implementation
b. No monthly payments
c. We don’t add third party dependencies
i. Reliability
ii. Data/code privacy
11
12. Issue nr 1 - solution 3
Write custom splitter - goals:
1. Split big test files (one big file takes many minutes to run)
2. Keep small files together if possible
3. Have control on what is “big” and what is “small”
4. Other benefits:
a. One time cost - implementation
b. No monthly payments
c. We don’t add third party dependencies
i. Reliability
ii. Data/code privacy
12
13. Issue nr 1 - solution 3
Write custom splitter - implementation plan:
1. Exporter - we need to have a “list” of all tests that we want to run
a. Custom RSpec formatter
2. Splitter script - main splitting logic based on exported list
a. Can be pure ruby, it will be really fast this way
3. Hook exporter and splitter into Circle CI
13
34. Recommendations
● Don’t write separate “it” blocks in tests, put them into bigger block so
that “before” runs as little as possible
● In other projects if there is one big test file, either split it into smaller files
or use “custom” splitter
● Be careful when writing tests that operate on dates/time
○ Better to use “20.days.ago” than “2022-10-13”
■ Wrap it up into timecop or other “time travel” solution
○ They can also fail because test scenarios are intended to operate on
specific days like weekdays, end of month or last 30 days span only 2
months - these can fail very unexpectedly if run on weekend, in the
middle of night, just after midnight or around February
34