Skip to content

Commit 675d9ff

Browse files
Add an option threshold: to .parallel() setup method (rails#42789)
This adds an additional method to configure the parallelization threshold. Before this, the only way of configuring the threshold was via an option: ``` config.active_support.test_parallelization_minimum_number_of_tests ```
1 parent 52e3dc2 commit 675d9ff

File tree

7 files changed

+45
-19
lines changed

7 files changed

+45
-19
lines changed

activesupport/CHANGELOG.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
* Parallelize tests only when overhead is justified by the number of them
1+
* Faster tests by parallelizing only when overhead is justified by the number
2+
of them.
23

34
Running tests in parallel adds overhead in terms of database
45
setup and fixture loading. Now, Rails will only parallelize test executions when
56
there are enough tests to make it worth it.
67

7-
This threshold is 50 by default, and is configurable via:
8+
This threshold is 50 by default, and is configurable via config setting in
9+
your test.rb:
810

911
```ruby
10-
config.active_support.test_parallelization_minimum_number_of_tests = 100
12+
config.active_support.test_parallelization_threshold = 100
13+
```
14+
15+
It's also configurable at the test case level:
16+
17+
```ruby
18+
class ActiveSupport::TestCase
19+
parallelize threshold: 100
20+
end
1121
```
1222
1323
*Jorge Manrubia*

activesupport/lib/active_support.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def self.eager_load!
8888

8989
cattr_accessor :test_order # :nodoc:
9090
cattr_accessor :test_parallelization_disabled, default: false # :nodoc:
91-
cattr_accessor :test_parallelization_minimum_number_of_tests, default: 50 # :nodoc:
91+
cattr_accessor :test_parallelization_threshold, default: 50 # :nodoc:
9292

9393
def self.disable_test_parallelization!
9494
self.test_parallelization_disabled = true unless ENV["PARALLEL_WORKERS"]

activesupport/lib/active_support/test_case.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@ def test_order
7272
#
7373
# The threaded parallelization uses minitest's parallel executor directly.
7474
# The processes parallelization uses a Ruby DRb server.
75-
def parallelize(workers: :number_of_processors, with: :processes)
75+
#
76+
# Because parallelization presents an overhead, it is only enabled when the
77+
# number of tests to run is above the +threshold+ param. The default value is
78+
# 50, and it's configurable via +config.active_support.test_parallelization_threshold+.
79+
def parallelize(workers: :number_of_processors, with: :processes, threshold: ActiveSupport.test_parallelization_threshold)
7680
workers = Concurrent.physical_processor_count if workers == :number_of_processors
7781
workers = ENV["PARALLEL_WORKERS"].to_i if ENV["PARALLEL_WORKERS"]
7882

7983
return if workers <= 1 || ActiveSupport.test_parallelization_disabled
8084

81-
Minitest.parallel_executor = ActiveSupport::Testing::ParallelizeExecutor.new(size: workers, with: with)
85+
Minitest.parallel_executor = ActiveSupport::Testing::ParallelizeExecutor.new(size: workers, with: with, threshold: threshold)
8286
end
8387

8488
# Set up hook for parallel testing. This can be used if you have multiple

activesupport/lib/active_support/testing/parallelize_executor.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
module ActiveSupport
44
module Testing
55
class ParallelizeExecutor # :nodoc:
6-
attr_reader :size, :parallelize_with, :parallel_executor
6+
attr_reader :size, :parallelize_with, :threshold
77

8-
def initialize(size:, with:)
8+
def initialize(size:, with:, threshold: ActiveSupport.test_parallelization_threshold)
99
@size = size
1010
@parallelize_with = with
11-
@parallel_executor = build_parallel_executor
11+
@threshold = threshold
1212
end
1313

1414
def start
@@ -27,6 +27,10 @@ def shutdown
2727
end
2828

2929
private
30+
def parallel_executor
31+
@parallel_executor ||= build_parallel_executor
32+
end
33+
3034
def build_parallel_executor
3135
case parallelize_with
3236
when :processes
@@ -49,7 +53,7 @@ def parallelized?
4953
end
5054

5155
def should_parallelize?
52-
ENV["PARALLEL_WORKERS"] || tests_count > ActiveSupport.test_parallelization_minimum_number_of_tests
56+
ENV["PARALLEL_WORKERS"] || tests_count > threshold
5357
end
5458

5559
def tests_count
@@ -64,7 +68,7 @@ def execution_info
6468
if parallelized?
6569
"Running #{tests_count} tests in parallel using #{parallel_executor.size} #{parallelize_with}"
6670
else
67-
"Running #{tests_count} tests in a single process (parallelization threshold is #{ActiveSupport.test_parallelization_minimum_number_of_tests})"
71+
"Running #{tests_count} tests in a single process (parallelization threshold is #{threshold})"
6872
end
6973
end
7074
end

guides/source/testing.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,20 @@ create as changes are not automatically rolled back after the test completes.
573573

574574
Running tests in parallel adds an overhead in terms of database setup and
575575
fixture loading. Because of this, Rails won't parallelize executions that involve
576-
fewer than 50 tests. You can configure this threshold in your `test.rb`:
576+
fewer than 50 tests.
577+
578+
You can configure this threshold in your `test.rb`:
577579

578580
```ruby
579-
config.active_support.test_parallelization_minimum_number_of_tests = 100
581+
config.active_support.test_parallelization_threshold = 100
582+
```
583+
584+
And also when setting up parallelization at the test case level:
585+
586+
```ruby
587+
class ActiveSupport::TestCase
588+
parallelize threshold: 100
589+
end
580590
```
581591

582592
The Test Database

railties/test/application/configuration_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,18 +2421,18 @@ class D < C
24212421
assert_equal OpenSSL::Digest::SHA256, ActiveSupport::KeyGenerator.hash_digest_class
24222422
end
24232423

2424-
test "ActiveSupport.test_parallelization_minimum_number_of_tests can be configured via config.active_support.test_parallelization_minimum_number_of_tests" do
2424+
test "ActiveSupport.test_parallelization_threshold can be configured via config.active_support.test_parallelization_threshold" do
24252425
remove_from_config '.*config\.load_defaults.*\n'
24262426

24272427
app_file "config/environments/test.rb", <<-RUBY
24282428
Rails.application.configure do
2429-
config.active_support.test_parallelization_minimum_number_of_tests = 1234
2429+
config.active_support.test_parallelization_threshold = 1234
24302430
end
24312431
RUBY
24322432

24332433
app "test"
24342434

2435-
assert_equal 1234, ActiveSupport.test_parallelization_minimum_number_of_tests
2435+
assert_equal 1234, ActiveSupport.test_parallelization_threshold
24362436
end
24372437

24382438
test "custom serializers should be able to set via config.active_job.custom_serializers in an initializer" do

railties/test/application/test_runner_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,16 +1166,14 @@ def exercise_parallelization_regardless_of_machine_core_count(with:, force: true
11661166
require_relative "../config/environment"
11671167
require "rails/test_help"
11681168
1169-
ActiveSupport.test_parallelization_minimum_number_of_tests = #{threshold}
1170-
11711169
class ActiveSupport::TestCase
11721170
<%- if force -%>
11731171
# Force parallelization, even with single files
11741172
ActiveSupport.test_parallelization_disabled = false
11751173
<%- end -%>
11761174
11771175
# Run tests in parallel with specified workers
1178-
parallelize(workers: 2, with: :<%= with %>)
1176+
parallelize(workers: 2, with: :<%= with %>, threshold: #{threshold})
11791177
11801178
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
11811179
fixtures :all

0 commit comments

Comments
 (0)