Skip to content

Commit def1448

Browse files
authored
Add basic internal testing for memory allocations (#2097)
* Add basic internal testing for memory allocations * add RUBY_VERSION to debug output * fix allocation counts * fix allocation counts * use compile cache helper * try using cache clearing * try using more specific ruby version targets and ensure compilation * add allocation counts for CI ruby versions * use counts from CI * move require to top of file * try removing component from compile cache to stabilize allocations * another stabilization attempt
1 parent fe39b4d commit def1448

File tree

5 files changed

+46
-23
lines changed

5 files changed

+46
-23
lines changed

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ GEM
7676
tzinfo (~> 2.0)
7777
addressable (2.8.7)
7878
public_suffix (>= 2.0.2, < 7.0)
79+
allocation_stats (0.1.5)
7980
ansi (1.5.0)
8081
appraisal (2.5.0)
8182
bundler
@@ -330,6 +331,7 @@ PLATFORMS
330331
ruby
331332

332333
DEPENDENCIES
334+
allocation_stats (~> 0.1.5)
333335
appraisal (~> 2.4)
334336
benchmark-ips (~> 2.13.0)
335337
better_html

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Add basic internal testing for memory allocations.
14+
15+
*Joel Hawksley*
16+
1317
* Add support for request formats.
1418

1519
*Joel Hawksley*

test/sandbox/test/rendering_test.rb

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ def test_render_inline
99
assert_selector("div", text: "hello,world!")
1010
end
1111

12+
def test_render_inline_allocations
13+
# Stabilize compilation status ahead of testing allocations to simulate rendering
14+
# performance with compiled component
15+
ViewComponent::CompileCache.cache.delete(MyComponent)
16+
MyComponent.ensure_compiled
17+
18+
assert_allocations("3.4.0" => 107, "3.3.5" => 116, "3.3.0" => 129, "3.2.5" => 115, "3.1.6" => 115, "3.0.7" => 125) do
19+
render_inline(MyComponent.new)
20+
end
21+
22+
assert_selector("div", text: "hello,world!")
23+
end
24+
1225
def test_render_in_view_context
1326
render_in_view_context { render(MyComponent.new) }
1427

@@ -717,34 +730,28 @@ def test_collection_component_present_custom_parameter_name_with_activemodel
717730
end
718731

719732
def test_component_with_invalid_parameter_names
720-
old_cache = ViewComponent::CompileCache.cache
721-
ViewComponent::CompileCache.cache = Set.new
722-
723-
exception =
724-
assert_raises ViewComponent::ReservedParameterError do
725-
InvalidParametersComponent.compile(raise_errors: true)
726-
end
733+
with_new_cache do
734+
exception =
735+
assert_raises ViewComponent::ReservedParameterError do
736+
InvalidParametersComponent.compile(raise_errors: true)
737+
end
727738

728-
assert_match(/InvalidParametersComponent initializer can't accept the parameter/, exception.message)
729-
ensure
730-
ViewComponent::CompileCache.cache = old_cache
739+
assert_match(/InvalidParametersComponent initializer can't accept the parameter/, exception.message)
740+
end
731741
end
732742

733743
def test_component_with_invalid_named_parameter_names
734-
old_cache = ViewComponent::CompileCache.cache
735-
ViewComponent::CompileCache.cache = Set.new
736-
737-
exception =
738-
assert_raises ViewComponent::ReservedParameterError do
739-
InvalidNamedParametersComponent.compile(raise_errors: true)
740-
end
744+
with_new_cache do
745+
exception =
746+
assert_raises ViewComponent::ReservedParameterError do
747+
InvalidNamedParametersComponent.compile(raise_errors: true)
748+
end
741749

742-
assert_match(
743-
/InvalidNamedParametersComponent initializer can't accept the parameter `content`/,
744-
exception.message
745-
)
746-
ensure
747-
ViewComponent::CompileCache.cache = old_cache
750+
assert_match(
751+
/InvalidNamedParametersComponent initializer can't accept the parameter `content`/,
752+
exception.message
753+
)
754+
end
748755
end
749756

750757
def test_collection_component_with_trailing_comma_attr_reader

test/test_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "allocation_stats"
34
require "simplecov"
45
require "simplecov-console"
56
require "rails/version"
@@ -187,3 +188,11 @@ def capture_warnings(&block)
187188
end
188189
end
189190
end
191+
192+
def assert_allocations(count_map, &block)
193+
trace = AllocationStats.trace(&block)
194+
total = trace.allocations.all.size
195+
count = count_map[RUBY_VERSION]
196+
197+
assert_equal count, total, "Expected #{count} allocations, got #{total} allocations for Ruby #{RUBY_VERSION}"
198+
end

view_component.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
3232
spec.add_runtime_dependency "activesupport", [">= 5.2.0", "< 8.0"]
3333
spec.add_runtime_dependency "method_source", "~> 1.0"
3434
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
35+
spec.add_development_dependency "allocation_stats", "~> 0.1.5"
3536
spec.add_development_dependency "appraisal", "~> 2.4"
3637
spec.add_development_dependency "benchmark-ips", "~> 2.13.0"
3738
spec.add_development_dependency "better_html"

0 commit comments

Comments
 (0)