Skip to content

Commit 05887d3

Browse files
committed
Setup bootsnap to speedup TestRunnerTest
When running `rails t` we skip the "fork" optimization. Because of this Rails is loaded from scratch. However the vast majority of the code is Rails and other gems so it's the same for all tests. By seting up bootsnap we can dramatically speed it up. main: ``` $ bin/test test/application/test_runner_test.rb -n test_run_single_file Run options: -n test_run_single_file --seed 8478 . Finished in 7.313216s, 0.1367 runs/s, 0.2735 assertions/s. ``` This branch: ``` $ bin/test test/application/test_runner_test.rb -n test_run_single_file Run options: -n test_run_single_file --seed 60063 . Finished in 2.751576s, 0.3634 runs/s, 0.7269 assertions/s. 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips ```
1 parent a93a8f1 commit 05887d3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

railties/test/isolation/abstract_unit.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def app_template_path
3838
File.join RAILS_FRAMEWORK_ROOT, "tmp/templates/app_template"
3939
end
4040

41+
def bootsnap_cache_path
42+
File.join RAILS_FRAMEWORK_ROOT, "tmp/templates/bootsnap"
43+
end
44+
4145
def tmp_path(*args)
4246
@tmp_path ||= File.realpath(Dir.mktmpdir(nil, File.join(RAILS_FRAMEWORK_ROOT, "tmp")))
4347
File.join(@tmp_path, *args)
@@ -368,7 +372,12 @@ def rails(*args, allow_failure: false, stderr: false)
368372
Process.waitpid pid
369373

370374
else
371-
output = `cd #{app_path}; #{command}`
375+
ENV["BOOTSNAP_CACHE_DIR"] = bootsnap_cache_path
376+
begin
377+
output = `cd #{app_path}; #{command}`
378+
ensure
379+
ENV.delete("BOOTSNAP_CACHE_DIR")
380+
end
372381
end
373382

374383
raise "rails command failed (#{$?.exitstatus}): #{command}\n#{output}" unless allow_failure || $?.success?
@@ -513,6 +522,7 @@ def self.sh(cmd)
513522

514523
sh "#{Gem.ruby} #{RAILS_FRAMEWORK_ROOT}/railties/exe/rails new #{app_template_path} --skip-bundle --no-rc --quiet"
515524
File.open("#{app_template_path}/config/boot.rb", "w") do |f|
525+
f.puts 'require "bootsnap/setup" if ENV["BOOTSNAP_CACHE_DIR"]'
516526
f.puts 'require "rails/all"'
517527
end
518528

0 commit comments

Comments
 (0)