From 12f06abf1126697f61359f1e8e82ba1f20f7daa6 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 2 Jun 2021 23:51:53 -0600 Subject: [PATCH] Move profile JSON formatting into Profile class --- src/spectator/formatting/json_formatter.cr | 14 +------------- src/spectator/profile.cr | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/spectator/formatting/json_formatter.cr b/src/spectator/formatting/json_formatter.cr index d7f87a0..a0de70f 100644 --- a/src/spectator/formatting/json_formatter.cr +++ b/src/spectator/formatting/json_formatter.cr @@ -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 diff --git a/src/spectator/profile.cr b/src/spectator/profile.cr index e6bc612..382b85a 100644 --- a/src/spectator/profile.cr +++ b/src/spectator/profile.cr @@ -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