Move profile JSON formatting into Profile class

This commit is contained in:
Michael Miller 2021-06-02 23:51:53 -06:00
parent bd34b87e22
commit 12f06abf11
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
2 changed files with 19 additions and 13 deletions

View file

@ -33,20 +33,8 @@ module Spectator::Formatting
# Adds the profiling information to the document.
def dump_profile(notification)
profile = notification.profile
@json.field("profile") do
@json.object do
@json.field("examples") do
@json.array do
profile.each(&.to_json(@json))
end
end
@json.field("slowest", profile.max_of(&.result.elapsed).total_seconds)
@json.field("total", profile.time.total_seconds)
@json.field("percentage", profile.percentage)
end
notification.profile.to_json(@json)
end
end

View file

@ -1,3 +1,6 @@
require "json"
require "./example"
module Spectator
# Information about the runtimes of examples.
class Profile
@ -39,5 +42,20 @@ module Spectator
def percentage
time / @total_time * 100
end
# Produces a JSON fragment containing the profiling information.
def to_json(json : JSON::Builder)
json.object do
json.field("examples") do
json.array do
@slowest.each(&.to_json(json))
end
end
json.field("slowest", @slowest.max_of(&.result.elapsed).total_seconds)
json.field("total", time.total_seconds)
json.field("percentage", percentage)
end
end
end
end