2019-03-25 21:54:51 +00:00
|
|
|
module Spectator
|
|
|
|
# Information about the runtimes of examples.
|
|
|
|
class Profile
|
2021-04-27 00:47:11 +00:00
|
|
|
include Indexable(Example)
|
2019-03-25 21:54:51 +00:00
|
|
|
|
|
|
|
# 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.
|
2021-04-27 00:47:11 +00:00
|
|
|
private def initialize(@slowest : Array(Example), @total_time)
|
2019-03-25 21:54:51 +00:00
|
|
|
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
|
2021-04-27 00:47:11 +00:00
|
|
|
@slowest.sum(&.result.elapsed)
|
2019-03-25 21:54:51 +00:00
|
|
|
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)
|
2021-04-27 00:47:11 +00:00
|
|
|
examples = report.to_a
|
|
|
|
sorted_examples = examples.sort_by(&.result.elapsed)
|
|
|
|
slowest = sorted_examples.last(size).reverse
|
2019-03-25 22:31:01 +00:00
|
|
|
self.new(slowest, report.example_runtime)
|
2019-03-25 21:54:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|