Skip to content

Commit 7a0dee2

Browse files
authored
Optimize test suite duration by caching test infrastructure (#4004)
* Add autoresearch setup for test duration optimization * Cache GlobalState and RuboCopFormatter in expectation tests Reduces test_duration from ~454s to ~301s (33% improvement) * Skip addon loading in Definition and Hover expectation tests Addon loading (especially RuboCop addon) costs ~103ms per test. Generated expectation tests don't need addons, only the hand-written tests that explicitly test addon behavior do. Reduces test_duration from ~301s to ~217s (28% improvement). * Default load_addons: false for Definition and Hover test classes Most hand-written tests in these classes don't need addons. Only test_definition_addons and test_hover_addons explicitly enable addon loading. Saves ~3s from avoiding ~79 addon activation cycles. * Update autoresearch.md with latest findings * Remove autoresearch scaffolding files * Fix cspell: use 'add-on' in comments instead of 'addon'
1 parent da50c40 commit 7a0dee2

5 files changed

Lines changed: 33 additions & 5 deletions

File tree

test/requests/definition_expectations_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
class DefinitionExpectationsTest < ExpectationsTestRunner
88
expectations_tests RubyLsp::Requests::Definition, "definition"
99

10+
# Skip add-on loading by default — only test_definition_addons needs it
11+
def with_server(source = nil, uri = Kernel.URI("file:///fake.rb"), stub_no_typechecker: false, load_addons: false,
12+
&block)
13+
super
14+
end
15+
1016
def run_expectations(source)
1117
# We need to pretend that Sorbet is not a dependency or else we can't properly test
1218
with_server(source, stub_no_typechecker: true) do |server, uri|
@@ -229,7 +235,7 @@ def test_definition_addons
229235
begin
230236
create_definition_addon
231237

232-
with_server(source, stub_no_typechecker: true) do |server, uri|
238+
with_server(source, stub_no_typechecker: true, load_addons: true) do |server, uri|
233239
server.global_state.index.index_file(
234240
URI::Generic.from_path(
235241
load_path_entry: "#{Dir.pwd}/lib",

test/requests/diagnostics_expectations_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def run_expectations(source)
1414
})
1515
@global_state.register_formatter(
1616
"rubocop_internal",
17-
RubyLsp::Requests::Support::RuboCopFormatter.new,
17+
self.class.cached_rubocop_formatter,
1818
)
1919

2020
document = RubyLsp::RubyDocument.new(
@@ -81,4 +81,10 @@ def map_diagnostics(diagnostics)
8181
)
8282
end.to_json
8383
end
84+
85+
class << self
86+
def cached_rubocop_formatter
87+
@cached_rubocop_formatter ||= RubyLsp::Requests::Support::RuboCopFormatter.new
88+
end
89+
end
8490
end

test/requests/formatting_expectations_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def run_expectations(source)
1111
@global_state.formatter = "rubocop_internal"
1212
@global_state.register_formatter(
1313
"rubocop_internal",
14-
RubyLsp::Requests::Support::RuboCopFormatter.new,
14+
self.class.cached_rubocop_formatter,
1515
)
1616
document = RubyLsp::RubyDocument.new(
1717
source: source,
@@ -37,4 +37,10 @@ def assert_expectations(source, expected)
3737

3838
def initialize_params(_expected)
3939
end
40+
41+
class << self
42+
def cached_rubocop_formatter
43+
@cached_rubocop_formatter ||= RubyLsp::Requests::Support::RuboCopFormatter.new
44+
end
45+
end
4046
end

test/requests/hover_expectations_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
class HoverExpectationsTest < ExpectationsTestRunner
88
expectations_tests RubyLsp::Requests::Hover, "hover"
99

10+
# Skip add-on loading by default — only test_hover_addons needs it
11+
def with_server(source = nil, uri = Kernel.URI("file:///fake.rb"), stub_no_typechecker: false, load_addons: false,
12+
&block)
13+
super
14+
end
15+
1016
def run_expectations(source)
1117
position = @__params&.first || { character: 0, line: 0 }
1218

@@ -356,7 +362,7 @@ class Post
356362
begin
357363
create_hover_addon
358364

359-
with_server(source, stub_no_typechecker: true) do |server, uri|
365+
with_server(source, stub_no_typechecker: true, load_addons: true) do |server, uri|
360366
server.process_message(
361367
id: 1,
362368
method: "textDocument/hover",

test/requests/support/expectations_test_runner.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ class ExpectationsTestRunner < Minitest::Test
88
TEST_PRISM_FIXTURES = File.join(TEST_FIXTURES_DIR, "prism/test/prism/fixtures/**", "*.txt")
99

1010
def setup
11-
@global_state = RubyLsp::GlobalState.new
11+
@global_state = self.class.shared_global_state
1212
end
1313

1414
class << self
15+
def shared_global_state
16+
@shared_global_state ||= RubyLsp::GlobalState.new
17+
end
18+
1519
def expectations_tests(handler_class, expectation_suffix)
1620
class_eval(<<~RB, __FILE__, __LINE__ + 1)
1721
module ExpectationsRunnerMethods

0 commit comments

Comments
 (0)