shard-spectator/src/spectator/profile.cr
Michael Miller 02b98ea61b
Remove reference to example from result
Pass examples instead of results into formatters.
2021-04-26 18:47:11 -06:00

42 lines
1.1 KiB
Crystal

module Spectator
# Information about the runtimes of examples.
class Profile
include Indexable(Example)
# Total length of time it took to run all examples in the test suite.
getter total_time : Time::Span
# Creates the profiling information.
# The *slowest* results must already be sorted, longest time first.
private def initialize(@slowest : Array(Example), @total_time)
end
# Number of results in the profile.
def size
@slowest.size
end
# Retrieves a result at the specified index.
def unsafe_fetch(index)
@slowest.unsafe_fetch(index)
end
# Length of time it took to run the results in the profile.
def time
@slowest.sum(&.result.elapsed)
end
# Percentage (from 0 to 1) of time the results in this profile took compared to all examples.
def percentage
time / @total_time
end
# Produces the profile from a report.
def self.generate(report, size = 10)
examples = report.to_a
sorted_examples = examples.sort_by(&.result.elapsed)
slowest = sorted_examples.last(size).reverse
self.new(slowest, report.example_runtime)
end
end
end