Add profile information to suite summary

This commit is contained in:
Michael Miller 2019-03-25 16:44:20 -06:00
parent 34b0399654
commit 291a927f1e
4 changed files with 74 additions and 1 deletions

View file

@ -0,0 +1,28 @@
module Spectator::Formatting
# Contents of a profile block.
private struct ProfileBlock
# Creates the block.
def initialize(@profile : Profile)
end
# Appends the block to the output.
def to_s(io)
io.puts(ProfileSummary.new(@profile))
indent = Indent.new(io)
indent.increase do
@profile.each do |result|
entry(indent, result)
end
end
end
# Adds a result entry to the output.
private def entry(indent, result)
indent.line(result.example)
indent.increase do
indent.line(SourceTiming.new(result.elapsed, result.example.source))
end
end
end
end

View file

@ -0,0 +1,29 @@
module Spectator::Formatting
# Top line of a profile block which gives a summary.
private struct ProfileSummary
# Creates the summary line.
def initialize(@profile : Profile)
end
# Appends the summary to the output.
def to_s(io)
io << "Top "
io << @profile.size
io << " slowest examples ("
io << human_time
io << ", "
io.printf("%.2f", percentage)
io << "% of total time):"
end
# Creates a human-friendly string for the total time.
private def human_time
HumanTime.new(@profile.total_time)
end
# Percentage (0 to 100) of total time.
private def percentage
@profile.percentage * 100
end
end
end

View file

@ -0,0 +1,16 @@
module Spectator::Formatting
# Produces the timing line in a profile block.
# This contains the length of time, and the example's source.
private struct SourceTiming
# Creates the source timing line.
def initialize(@span : Time::Span, @source : Source)
end
# Appends the source timing information to the output.
def to_s(io)
io << HumanTime.new(@span).colorize.bold
io << ' '
io << @source
end
end
end

View file

@ -40,7 +40,7 @@ module Spectator::Formatting
# Produces the profiling section of the summary. # Produces the profiling section of the summary.
private def profile(profile) private def profile(profile)
raise NotImplementedError.new("profile") @io.puts ProfileBlock.new(profile)
end end
# Produces the statistical section of the summary. # Produces the statistical section of the summary.