mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add profiling info
This commit is contained in:
parent
4bb4c2f16e
commit
ebf04b360e
6 changed files with 58 additions and 15 deletions
32
src/spectator/formatting/components/profile.cr
Normal file
32
src/spectator/formatting/components/profile.cr
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
require "../../profile"
|
||||||
|
require "./runtime"
|
||||||
|
|
||||||
|
module Spectator::Formatting::Components
|
||||||
|
struct Profile
|
||||||
|
def initialize(@profile : Spectator::Profile)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s(io)
|
||||||
|
io << "Top "
|
||||||
|
io << @profile.size
|
||||||
|
io << " slowest examples ("
|
||||||
|
io << Runtime.new(@profile.time)
|
||||||
|
io << ", "
|
||||||
|
io << @profile.percentage.round(2)
|
||||||
|
io.puts "% of total time):"
|
||||||
|
|
||||||
|
@profile.each do |example|
|
||||||
|
example_profile(io, example)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def example_profile(io, example)
|
||||||
|
io << " "
|
||||||
|
io.puts example
|
||||||
|
io << " "
|
||||||
|
io << Runtime.new(example.result.elapsed).colorize.bold
|
||||||
|
io << ' '
|
||||||
|
io.puts example.location
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,4 +1,6 @@
|
||||||
require "../example"
|
require "../example"
|
||||||
|
require "../profile"
|
||||||
|
require "../report"
|
||||||
|
|
||||||
module Spectator::Formatting
|
module Spectator::Formatting
|
||||||
# Structure indicating the test suite has started.
|
# Structure indicating the test suite has started.
|
||||||
|
@ -10,6 +12,9 @@ module Spectator::Formatting
|
||||||
# Structure containing a subset of examples from the test suite.
|
# Structure containing a subset of examples from the test suite.
|
||||||
record ExampleSummaryNotification, examples : Enumerable(Example)
|
record ExampleSummaryNotification, examples : Enumerable(Example)
|
||||||
|
|
||||||
|
# Structure containing profiling information.
|
||||||
|
record ProfileNotification, profile : Profile
|
||||||
|
|
||||||
# Structure containing summarized information from the outcome of the test suite.
|
# Structure containing summarized information from the outcome of the test suite.
|
||||||
record SummaryNotification, report : Report
|
record SummaryNotification, report : Report
|
||||||
end
|
end
|
||||||
|
|
|
@ -48,7 +48,8 @@ module Spectator::Formatting
|
||||||
# Invoked after testing completes with profiling information.
|
# Invoked after testing completes with profiling information.
|
||||||
# This method is only called if profiling is enabled.
|
# This method is only called if profiling is enabled.
|
||||||
# Called after `#dump_failures` and before `#dump_summary`.
|
# Called after `#dump_failures` and before `#dump_summary`.
|
||||||
def dump_profile(_notification)
|
def dump_profile(notification)
|
||||||
|
io.puts Components::Profile.new(notification.profile)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Invoked after testing completes with summarized information from the test suite.
|
# Invoked after testing completes with summarized information from the test suite.
|
||||||
|
|
|
@ -8,7 +8,16 @@ module Spectator
|
||||||
|
|
||||||
# Creates the profiling information.
|
# Creates the profiling information.
|
||||||
# The *slowest* results must already be sorted, longest time first.
|
# The *slowest* results must already be sorted, longest time first.
|
||||||
private def initialize(@slowest : Array(Example), @total_time)
|
def initialize(@slowest : Array(Example), @total_time)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Produces the profile from a report.
|
||||||
|
def self.generate(examples, size = 10)
|
||||||
|
finished = examples.select(&.finished?).to_a
|
||||||
|
total_time = finished.sum(&.result.elapsed)
|
||||||
|
sorted_examples = finished.sort_by(&.result.elapsed)
|
||||||
|
slowest = sorted_examples.last(size).reverse
|
||||||
|
new(slowest, total_time)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Number of results in the profile.
|
# Number of results in the profile.
|
||||||
|
@ -26,17 +35,9 @@ module Spectator
|
||||||
@slowest.sum(&.result.elapsed)
|
@slowest.sum(&.result.elapsed)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Percentage (from 0 to 1) of time the results in this profile took compared to all examples.
|
# Percentage (from 0 to 100) of time the results in this profile took compared to all examples.
|
||||||
def percentage
|
def percentage
|
||||||
time / @total_time
|
time / @total_time * 100
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,7 +36,7 @@ module Spectator
|
||||||
end
|
end
|
||||||
|
|
||||||
# Triggers the 'dump' events.
|
# Triggers the 'dump' events.
|
||||||
private def summarize(report)
|
private def summarize(report, profile)
|
||||||
formatter.start_dump
|
formatter.start_dump
|
||||||
|
|
||||||
notification = Formatting::ExampleSummaryNotification.new(report.pending)
|
notification = Formatting::ExampleSummaryNotification.new(report.pending)
|
||||||
|
@ -45,7 +45,10 @@ module Spectator
|
||||||
notification = Formatting::ExampleSummaryNotification.new(report.failures)
|
notification = Formatting::ExampleSummaryNotification.new(report.failures)
|
||||||
formatter.dump_failures(notification)
|
formatter.dump_failures(notification)
|
||||||
|
|
||||||
formatter.dump_profile(nil)
|
if profile
|
||||||
|
notification = Formatting::ProfileNotification.new(profile)
|
||||||
|
formatter.dump_profile(notification)
|
||||||
|
end
|
||||||
|
|
||||||
notification = Formatting::SummaryNotification.new(report)
|
notification = Formatting::SummaryNotification.new(report)
|
||||||
formatter.dump_summary(notification)
|
formatter.dump_summary(notification)
|
||||||
|
|
|
@ -31,7 +31,8 @@ module Spectator
|
||||||
stop
|
stop
|
||||||
|
|
||||||
report = Report.generate(@examples, elapsed, nil) # TODO: Provide random seed.
|
report = Report.generate(@examples, elapsed, nil) # TODO: Provide random seed.
|
||||||
summarize(report)
|
profile = Profile.generate(@examples) if @run_flags.profile? && report.counts.run > 0
|
||||||
|
summarize(report, profile)
|
||||||
|
|
||||||
false # TODO: Report real result
|
false # TODO: Report real result
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue