Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit c67a168

Browse files
committed
setup profile notification to delegate slowest_examples groups to the profiler
1 parent 928767e commit c67a168

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed

lib/rspec/core/formatters/json_formatter.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def dump_profile(profile)
6060
# @api private
6161
def dump_profile_slowest_examples(profile)
6262
@output_hash[:profile] = {}
63-
sorted_examples = profile.slowest_examples
64-
@output_hash[:profile][:examples] = sorted_examples.map do |example|
63+
@output_hash[:profile][:examples] = profile.slowest_examples.map do |example|
6564
format_example(example).tap do |hash|
6665
hash[:run_time] = example.execution_result.run_time
6766
end

lib/rspec/core/notifications.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ def duplicate_rerun_locations
496496
# @attr duration [Float] the time taken (in seconds) to run the suite
497497
# @attr examples [Array<RSpec::Core::Example>] the examples run
498498
# @attr number_of_examples [Fixnum] the number of examples to profile
499-
ProfileNotification = Struct.new(:duration, :examples, :number_of_examples)
499+
# @attr profiler [RSpec::Core::Profiler] the profiler used to capture examples
500+
ProfileNotification = Struct.new(:duration, :examples, :number_of_examples, :profiler)
500501
class ProfileNotification
501502
# @return [Array<RSpec::Core::Example>] the slowest examples
502503
def slowest_examples
@@ -524,8 +525,14 @@ def percentage
524525
end
525526

526527
# @return [Array<RSpec::Core::Example>] the slowest example groups
527-
# todo rename this method or move it's code to the json_formater.rb and profile.formater.rb
528-
def calculate_slowest_groups(example_groups)
528+
def slowest_groups
529+
@slowest_groups ||= calculate_slowest_groups
530+
end
531+
532+
private
533+
534+
def calculate_slowest_groups
535+
example_groups = profiler.example_groups
529536

530537
# stop if we've only one example group
531538
return {} if example_groups.keys.length <= 1

lib/rspec/core/reporter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def finish
150150
notify :deprecation_summary, Notifications::NullNotification
151151
unless mute_profile_output?
152152
notify :dump_profile, Notifications::ProfileNotification.new(@duration, @examples,
153-
@configuration.profile_examples)
153+
@configuration.profile_examples,
154+
@configuration.profiler)
154155
end
155156
notify :dump_summary, Notifications::SummaryNotification.new(@duration, @examples, @failed_examples,
156157
@pending_examples, @load_time)

spec/rspec/core/formatters/json_formatter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def profile *groups
133133
end
134134

135135
before do
136+
setup_profiler
136137
formatter
137-
config.profile_examples = 10
138138
end
139139

140140
context "with one example group" do

spec/rspec/core/formatters/profile_formatter_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
include FormatterSupport
55

66
def profile *groups
7+
setup_profiler
78
groups.each { |group| group.run(reporter) }
89
examples = groups.map(&:examples).flatten
910
total_time = examples.map { |e| e.execution_result.run_time }.inject(&:+)

spec/support/formatter_support.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ def setup_reporter(*streams)
185185
@reporter = config.reporter
186186
end
187187

188+
def setup_profiler
189+
config.profile_examples = true
190+
config.profiler
191+
end
192+
188193
def formatter_output
189194
@formatter_output ||= StringIO.new
190195
end
@@ -274,7 +279,7 @@ def summary_notification(duration, examples, failed, pending, time)
274279
end
275280

276281
def profile_notification(duration, examples, number)
277-
::RSpec::Core::Notifications::ProfileNotification.new duration, examples, number
282+
::RSpec::Core::Notifications::ProfileNotification.new duration, examples, number, config.profiler
278283
end
279284

280285
end

0 commit comments

Comments
 (0)