Add Profile class

This commit is contained in:
Michael Miller 2019-03-25 15:54:51 -06:00
parent a02e2ff701
commit 6dd4c4bc2f
2 changed files with 43 additions and 0 deletions

View file

@ -40,6 +40,7 @@ require "./example_failed"
require "./expectation_failed"
require "./test_suite"
require "./report"
require "./profile"
require "./runner"
require "./result"

42
src/spectator/profile.cr Normal file
View file

@ -0,0 +1,42 @@
module Spectator
# Information about the runtimes of examples.
class Profile
include Indexable(Result)
# 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(FinishedResult), @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(&.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)
results = report.compact_map(&.as?(FinishedResult))
sorted_results = results.sort_by(&.elapsed)
slowest = sorted_results.last(size).reverse
self.new(slowest, report.runtime)
end
end
end