Skip to content

Commit

Permalink
fix: use stable ids in memprof tracker
Browse files Browse the repository at this point in the history
- Drop excess MemProf tests
  • Loading branch information
palkan committed Apr 18, 2024
1 parent 6c8f17a commit 531a4d1
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 404 deletions.
11 changes: 7 additions & 4 deletions lib/test_prof/memory_prof/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@ def initialize
@tracker = MemoryProf.tracker
@printer = MemoryProf.printer(tracker)

@current_group = nil
@current_example = nil

@tracker.start
end

def example_started(notification)
tracker.example_started(example(notification))
tracker.example_started(notification.example, example(notification))
end

def example_finished(notification)
tracker.example_finished(example(notification))
tracker.example_finished(notification.example)
end

def example_group_started(notification)
tracker.group_started(group(notification))
tracker.group_started(notification.group, group(notification))
end

def example_group_finished(notification)
tracker.group_finished(group(notification))
tracker.group_finished(notification.group)
end

def report
Expand Down
24 changes: 14 additions & 10 deletions lib/test_prof/memory_prof/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@ def finish
@total_memory = node.total_memory
end

def example_started(example)
list.add_node(example, track)
def example_started(id, example = id)
list.add_node(id, example, track)
end

def example_finished(example)
node = list.remove_node(example, track)
examples << {**example, memory: node.total_memory}
def example_finished(id)
node = list.remove_node(id, track)
return unless node

examples << {**node.item, memory: node.total_memory}
end

def group_started(group)
list.add_node(group, track)
def group_started(id, group = id)
list.add_node(id, group, track)
end

def group_finished(group)
node = list.remove_node(group, track)
groups << {**group, memory: node.hooks_memory}
def group_finished(id)
node = list.remove_node(id, track)
return unless node

groups << {**node.item, memory: node.hooks_memory}
end
end

Expand Down
14 changes: 8 additions & 6 deletions lib/test_prof/memory_prof/tracker/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,20 @@ class LinkedList
attr_reader :head

def initialize(memory_at_start)
add_node(:total, memory_at_start)
add_node(:total, :total, memory_at_start)
end

def add_node(item, memory_at_start)
def add_node(id, item, memory_at_start)
@head = LinkedListNode.new(
id: id,
item: item,
previous: head,
memory_at_start: memory_at_start
)
end

def remove_node(item, memory_at_finish)
return if head.item != item
def remove_node(id, memory_at_finish)
return if head.id != id
head.finish(memory_at_finish)

current = head
Expand All @@ -75,9 +76,10 @@ def remove_node(item, memory_at_finish)
end

class LinkedListNode
attr_reader :item, :previous, :memory_at_start, :memory_at_finish, :nested_memory
attr_reader :id, :item, :previous, :memory_at_start, :memory_at_finish, :nested_memory

def initialize(item:, memory_at_start:, previous:)
def initialize(id:, item:, memory_at_start:, previous:)
@id = id
@item = item
@previous = previous

Expand Down
3 changes: 1 addition & 2 deletions spec/integrations/fixtures/rspec/memory_prof_fixture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
@array = 500.times.map { SecureRandom.hex }
end

it "does not allocate anything" do
end
it { 1 }
end

context "with 1000 allocations" do
Expand Down
58 changes: 18 additions & 40 deletions spec/integrations/memory_prof_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,52 @@

describe "MemoryProf RSpec" do
specify "with default options", :aggregate_failures do
output = run_rspec("memory_prof", env: {"TEST_MEM_PROF" => "test"})
output = run_rspec("memory_prof", env: {"TEST_MEM_PROF" => "test", "TEST_MEM_PROF_COUNT" => "3"})

expect(output).to include("MemoryProf results")
expect(output).to match(/Final RSS: #{memory_human_regex}/)

expect(output).to include("Top 5 groups (by RSS):")
expect(output).to include("Top 5 examples (by RSS):")

expect(output).to match(/with 10_000 allocations \(\.\/memory_prof_fixture.rb:39\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 1000 allocations \(\.\/memory_prof_fixture.rb:30\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 500 allocations \(\.\/memory_prof_fixture.rb:21\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/Groups Allocations \(\.\/memory_prof_fixture.rb:20\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/Examples allocations \(\.\/memory_prof_fixture.rb:6\)\+#{memory_human_regex} \(#{percent_regex}\)/)

expect(output).to match(/allocates 10_000 objects \(\.\/memory_prof_fixture.rb:15\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 1000 objects \(\.\/memory_prof_fixture.rb:11\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 500 objects \(\.\/memory_prof_fixture.rb:7\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to include("Top 3 groups (by RSS):")
expect(output).to include("Top 3 examples (by RSS):")
end

specify "in RSS mode", :aggregate_failures do
output = run_rspec("memory_prof", env: {"TEST_MEM_PROF" => "rss"})
output = run_rspec("memory_prof", env: {"TEST_MEM_PROF" => "rss", "TEST_MEM_PROF_COUNT" => "3"})

expect(output).to include("MemoryProf results")
expect(output).to match(/Final RSS: #{memory_human_regex}/)

expect(output).to include("Top 5 groups (by RSS):")
expect(output).to include("Top 5 examples (by RSS):")

expect(output).to match(/with 10_000 allocations \(\.\/memory_prof_fixture.rb:39\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 1000 allocations \(\.\/memory_prof_fixture.rb:30\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 500 allocations \(\.\/memory_prof_fixture.rb:21\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/Groups Allocations \(\.\/memory_prof_fixture.rb:20\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/Examples allocations \(\.\/memory_prof_fixture.rb:6\)\+#{memory_human_regex} \(#{percent_regex}\)/)

expect(output).to match(/allocates 10_000 objects \(\.\/memory_prof_fixture.rb:15\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 1000 objects \(\.\/memory_prof_fixture.rb:11\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 500 objects \(\.\/memory_prof_fixture.rb:7\)\+#{memory_human_regex} \(#{percent_regex}\)/)
expect(output).to include("Top 3 groups (by RSS):")
expect(output).to include("Top 3 examples (by RSS):")
end

if RUBY_ENGINE != "jruby"
specify "in allocations mode", :aggregate_failures do
output = run_rspec("memory_prof", env: {"TEST_MEM_PROF" => "alloc"})
output = run_rspec("memory_prof", env: {"TEST_MEM_PROF" => "alloc", "TEST_MEM_PROF_COUNT" => "3"})

expect(output).to include("MemoryProf results")
expect(output).to match(/Total allocations: #{number_regex}/)

expect(output).to include("Top 5 groups (by allocations):")
expect(output).to include("Top 5 examples (by allocations):")
expect(output).to include("Top 3 groups (by allocations):")
expect(output).to include("Top 3 examples (by allocations):")

expect(output).to match(/with 10_000 allocations \(\.\/memory_prof_fixture.rb:39\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 1000 allocations \(\.\/memory_prof_fixture.rb:30\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 500 allocations \(\.\/memory_prof_fixture.rb:21\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/Groups Allocations \(\.\/memory_prof_fixture.rb:20\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/Examples allocations \(\.\/memory_prof_fixture.rb:6\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 10_000 allocations \(\.\/memory_prof_fixture.rb:\d+\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 1000 allocations \(\.\/memory_prof_fixture.rb:\d+\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/with 500 allocations \(\.\/memory_prof_fixture.rb:\d+\)\+#{number_regex} \(#{percent_regex}\)/)

expect(output).to match(/allocates 10_000 objects \(\.\/memory_prof_fixture.rb:15\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 1000 objects \(\.\/memory_prof_fixture.rb:11\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 500 objects \(\.\/memory_prof_fixture.rb:7\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 10_000 objects \(\.\/memory_prof_fixture.rb:\d+\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 1000 objects \(\.\/memory_prof_fixture.rb:\d+\)\+#{number_regex} \(#{percent_regex}\)/)
expect(output).to match(/allocates 500 objects \(\.\/memory_prof_fixture.rb:\d+\)\+#{number_regex} \(#{percent_regex}\)/)
end
end

specify "with top_count", :aggregate_failures do
output = run_rspec("memory_prof", env: {"TEST_MEM_PROF" => "rss", "TEST_MEM_PROF_COUNT" => "3"})
output = run_rspec("memory_prof", env: {"TEST_MEM_PROF" => "rss", "TEST_MEM_PROF_COUNT" => "4"})

expect(output).to include("MemoryProf results")
expect(output).to match(/Final RSS: #{memory_human_regex}/)

expect(output).to include("Top 3 groups (by RSS):")
expect(output).to include("Top 3 examples (by RSS):")
expect(output).to include("Top 4 groups (by RSS):")
expect(output).to include("Top 4 examples (by RSS):")
end
end
68 changes: 0 additions & 68 deletions spec/test_prof/memory_prof/minitest_spec.rb

This file was deleted.

Loading

0 comments on commit 531a4d1

Please sign in to comment.