ecc5afed30
Parallelizing tests has a cost in terms of database setup and fixture loading. This change makes Rails disable parallelization when the number of tests is below a configurable threshold. When running tests in parallel each process gets its own database instance. On each execution, each process will update each database schema (if needed) and load all the fixtures. This can be very expensive for non trivial datasets. As an example, for HEY, when running a single file with 18 tests, running tests in parallel in my box adds an overhead of 13 seconds versus not parallelizing them. Of course parallelizing is totally worthy when there are many tests to run, but not when running just a few tests. The threshold is configurable via config.active_support.test_parallelization_minimum_number_of_tests, which is 30 50 by default. This also adds some tracing to know how tests are being executed: When in parallel: ``` Running 2829 tests in parallel in 8 processes ``` When not in parallel: ``` Running 15 tests in a single process (parallelization threshold is 30) ```
56 lines
1.2 KiB
Ruby
56 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "drb"
|
|
require "drb/unix" unless Gem.win_platform?
|
|
require "active_support/core_ext/module/attribute_accessors"
|
|
require "active_support/testing/parallelization/server"
|
|
require "active_support/testing/parallelization/worker"
|
|
|
|
module ActiveSupport
|
|
module Testing
|
|
class Parallelization # :nodoc:
|
|
@@after_fork_hooks = []
|
|
|
|
def self.after_fork_hook(&blk)
|
|
@@after_fork_hooks << blk
|
|
end
|
|
|
|
cattr_reader :after_fork_hooks
|
|
|
|
@@run_cleanup_hooks = []
|
|
|
|
def self.run_cleanup_hook(&blk)
|
|
@@run_cleanup_hooks << blk
|
|
end
|
|
|
|
cattr_reader :run_cleanup_hooks
|
|
|
|
def initialize(worker_count)
|
|
@worker_count = worker_count
|
|
@queue_server = Server.new
|
|
@worker_pool = []
|
|
@url = DRb.start_service("drbunix:", @queue_server).uri
|
|
end
|
|
|
|
def start
|
|
@worker_pool = @worker_count.times.map do |worker|
|
|
Worker.new(worker, @url).start
|
|
end
|
|
end
|
|
|
|
def <<(work)
|
|
@queue_server << work
|
|
end
|
|
|
|
def size
|
|
@worker_count
|
|
end
|
|
|
|
def shutdown
|
|
@queue_server.shutdown
|
|
@worker_pool.each { |pid| Process.waitpid pid }
|
|
end
|
|
end
|
|
end
|
|
end
|