Skip to content

Commit

Permalink
When sorting methods, only sort by time if the difference is greater …
Browse files Browse the repository at this point in the history
…than 0.0001 seconds. This helps make test results more consistent.
  • Loading branch information
cfisoi committed Jan 7, 2024
1 parent d574e64 commit 9e3e903
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 43 deletions.
4 changes: 2 additions & 2 deletions lib/ruby-prof/method_info.rb
Expand Up @@ -65,9 +65,9 @@ def <=>(other)
-1
elsif self.full_name == other.full_name
0
elsif self.total_time < other.total_time
elsif self.total_time < other.total_time && (self.total_time - other.total_time).abs > 0.0001
-1
elsif self.total_time > other.total_time
elsif self.total_time > other.total_time && (self.total_time - other.total_time).abs > 0.0001
1
elsif self.call_trees.min_depth < other.call_trees.min_depth
1
Expand Down
21 changes: 9 additions & 12 deletions test/exclude_methods_test.rb
Expand Up @@ -145,9 +145,9 @@ def test_methods_can_be_profiled
assert_equal('Kernel#sleep', methods[6].full_name)
assert_equal('ExcludeMethodsModule#c', methods[7].full_name)
assert_equal('<Module::ExcludeMethodsModule>#d', methods[8].full_name)
assert_equal('Integer#succ', methods[9].full_name)
assert_equal('Integer#<', methods[10].full_name)
assert_equal('Kernel#block_given?', methods[11].full_name)
assert_equal('Kernel#block_given?', methods[9].full_name)
assert_equal('Integer#succ', methods[10].full_name)
assert_equal('Integer#<', methods[11].full_name)
assert_equal('Kernel#class', methods[12].full_name)
end

Expand All @@ -169,9 +169,9 @@ def test_methods_can_be_hidden1
assert_equal('Kernel#sleep', methods[5].full_name)
assert_equal('ExcludeMethodsModule#c', methods[6].full_name)
assert_equal('<Module::ExcludeMethodsModule>#d', methods[7].full_name)
assert_equal('Integer#succ', methods[8].full_name)
assert_equal('Integer#<', methods[9].full_name)
assert_equal('Kernel#block_given?', methods[10].full_name)
assert_equal('Kernel#block_given?', methods[8].full_name)
assert_equal('Integer#succ', methods[9].full_name)
assert_equal('Integer#<', methods[10].full_name)
assert_equal('Kernel#class', methods[11].full_name)
end

Expand All @@ -193,7 +193,7 @@ def test_methods_can_be_hidden2
assert_equal('<Class::ExcludeMethodsClass>#e', methods[3].full_name)
assert_equal('Kernel#sleep', methods[4].full_name)
assert_equal('ExcludeMethodsModule#c', methods[5].full_name)
assert_equal('Integer#succ', methods[6].full_name)
assert_equal('Kernel#block_given?', methods[6].full_name)
assert_equal('Integer#succ', methods[7].full_name)
assert_equal('Integer#<', methods[8].full_name)
assert_equal('Kernel#class', methods[9].full_name)
Expand Down Expand Up @@ -234,11 +234,8 @@ def test_exclude_common_methods2
assert_equal('<Class::ExcludeMethodsClass>#f', methods[4].full_name)
assert_equal('Kernel#sleep', methods[5].full_name)
assert_equal('ExcludeMethodsModule#c', methods[6].full_name)
assert_equal('ExcludeMethodsModule#d', methods[7].full_name)
assert_equal('ExcludeMethodsClass#b', methods[8].full_name)
assert_equal('ExcludeMethodsClass#b', methods[9].full_name)
assert_equal('ExcludeMethodsClass#b', methods[10].full_name)
assert_equal('ExcludeMethodsClass#b', methods[11].full_name)
assert_equal('<Module::ExcludeMethodsModule>#d', methods[7].full_name)
assert_equal('Kernel#class', methods[8].full_name)
end
end

Expand Down
80 changes: 61 additions & 19 deletions test/measure_process_time_test.rb
Expand Up @@ -1031,14 +1031,14 @@ def test_instance_methods_sleep
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[2]
assert_equal('Kernel#sleep', method.full_name)
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[3]
assert_equal('Class#new', method.full_name)
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
Expand Down Expand Up @@ -1083,14 +1083,14 @@ def test_instance_methods_sleep_block
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[3]
assert_equal('Kernel#sleep', method.full_name)
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[4]
assert_equal('Class#new', method.full_name)
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
Expand Down Expand Up @@ -1174,14 +1174,14 @@ def test_instance_methods_sleep_threaded
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[2]
assert_equal('Kernel#sleep', method.full_name)
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[3]
assert_equal('Class#new', method.full_name)
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
Expand Down Expand Up @@ -1523,14 +1523,14 @@ def test_module_instance_methods_sleep
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[2]
assert_equal('Kernel#sleep', method.full_name)
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[3]
assert_equal('Class#new', method.full_name)
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
Expand Down Expand Up @@ -1856,14 +1856,14 @@ def test_instance_methods_sleep
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[2]
assert_equal('Kernel#sleep', method.full_name)
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[3]
assert_equal('Class#new', method.full_name)
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
Expand All @@ -1883,7 +1883,7 @@ def test_instance_methods_sleep_block
end

methods = result.threads.first.methods.sort.reverse
assert_equal(6, methods.length)
assert_equal(9, methods.length)

# Check times
method = methods[0]
Expand All @@ -1908,20 +1908,41 @@ def test_instance_methods_sleep_block
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[3]
assert_equal('Kernel#sleep', method.full_name)
assert_equal('Kernel#block_given?', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[4]
assert_equal('Class#new', method.full_name)
assert_equal('Integer#succ', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[5]
assert_equal('Integer#<', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[6]
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[7]
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[8]
assert_equal('BasicObject#initialize', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
Expand Down Expand Up @@ -1999,14 +2020,14 @@ def test_instance_methods_sleep_threaded
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[2]
assert_equal('Kernel#sleep', method.full_name)
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[3]
assert_equal('Class#new', method.full_name)
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
Expand Down Expand Up @@ -2088,7 +2109,7 @@ def test_instance_methods_busy_block
end

methods = result.threads.first.methods.sort.reverse
assert_equal(8, methods.length)
assert_equal(11, methods.length)

# Check times
method = methods[0]
Expand Down Expand Up @@ -2134,13 +2155,34 @@ def test_instance_methods_busy_block
assert_in_delta(0.03, method.children_time, 0.03)

method = methods[6]
assert_equal('Kernel#block_given?', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[7]
assert_equal('Integer#succ', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[8]
assert_equal('Integer#<', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[9]
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.01)
assert_in_delta(0.0, method.wait_time, 0.01)
assert_in_delta(0.0, method.self_time, 0.01)
assert_in_delta(0.0, method.children_time, 0.01)

method = methods[7]
method = methods[10]
assert_equal('BasicObject#initialize', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
Expand Down Expand Up @@ -2348,14 +2390,14 @@ def test_module_instance_methods_sleep
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[2]
assert_equal('Kernel#sleep', method.full_name)
assert_equal('Class#new', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
assert_in_delta(0.0, method.children_time, 0.05)

method = methods[3]
assert_equal('Class#new', method.full_name)
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(0.0, method.total_time, 0.05)
assert_in_delta(0.0, method.wait_time, 0.05)
assert_in_delta(0.0, method.self_time, 0.05)
Expand Down
6 changes: 3 additions & 3 deletions test/measure_wall_time_test.rb
Expand Up @@ -321,14 +321,14 @@ def test_instance_methods_block
assert_in_delta(0.0, method.children_time, 0.03 * @delta_multiplier)

method = methods[4]
assert_equal("Integer#succ", method.full_name)
assert_equal("Kernel#block_given?", method.full_name)
assert_in_delta(0.0, method.total_time, 0.03 * @delta_multiplier)
assert_in_delta(0.0, method.wait_time, 0.03 * @delta_multiplier)
assert_in_delta(0.0, method.self_time, 0.03 * @delta_multiplier)
assert_in_delta(0.0, method.children_time, 0.03 * @delta_multiplier)

method = methods[5]
assert_equal("Class#new", method.full_name)
assert_equal("Integer#succ", method.full_name)
assert_in_delta(0.0, method.total_time, 0.03 * @delta_multiplier)
assert_in_delta(0.0, method.wait_time, 0.03 * @delta_multiplier)
assert_in_delta(0.0, method.self_time, 0.03 * @delta_multiplier)
Expand All @@ -342,7 +342,7 @@ def test_instance_methods_block
assert_in_delta(0.0, method.children_time, 0.03 * @delta_multiplier)

method = methods[7]
assert_equal("Kernel#block_given?", method.full_name)
assert_equal("Class#new", method.full_name)
assert_in_delta(0.0, method.total_time, 0.03 * @delta_multiplier)
assert_in_delta(0.0, method.wait_time, 0.03 * @delta_multiplier)
assert_in_delta(0.0, method.self_time, 0.03 * @delta_multiplier)
Expand Down
14 changes: 7 additions & 7 deletions test/recursive_test.rb
Expand Up @@ -556,8 +556,8 @@ def test_cycle

# Method 5
method = methods[5]
assert_equal('Integer#succ', method.full_name)
assert_equal(4, method.called)
assert_equal('Kernel#block_given?', method.full_name)
assert_equal(2, method.called)
refute(method.recursive?)
assert_in_delta(0, method.total_time, 0.1)
assert_in_delta(0, method.self_time, 0.1)
Expand All @@ -572,8 +572,8 @@ def test_cycle

# Method 6
method = methods[6]
assert_equal('Integer#<', method.full_name)
assert_equal(6, method.called)
assert_equal('Integer#succ', method.full_name)
assert_equal(4, method.called)
refute(method.recursive?)
assert_in_delta(0, method.total_time, 0.1)
assert_in_delta(0, method.self_time, 0.1)
Expand All @@ -588,8 +588,8 @@ def test_cycle

# Method 7
method = methods[7]
assert_equal('Integer#+', method.full_name)
assert_equal(2, method.called)
assert_equal('Integer#<', method.full_name)
assert_equal(6, method.called)
refute(method.recursive?)
assert_in_delta(0, method.total_time, 0.1)
assert_in_delta(0, method.self_time, 0.1)
Expand All @@ -604,7 +604,7 @@ def test_cycle

# Method 8
method = methods[8]
assert_equal('Kernel#block_given?', method.full_name)
assert_equal('Integer#+', method.full_name)
assert_equal(2, method.called)
refute(method.recursive?)
assert_in_delta(0, method.total_time, 0.1)
Expand Down

0 comments on commit 9e3e903

Please sign in to comment.