@@ -15,8 +15,10 @@ class Result
1515 # Returns all files that are applicable to this result (sans filters!) as instances of SimpleCov::SourceFile. Aliased as :source_files
1616 attr_reader :files
1717 alias source_files files
18- # Explicitly set the Time this result has been created
18+ # Explicitly set the Time (Wall) this result has been created
1919 attr_writer :created_at
20+ # Explicitly set the Time (Monotonic) this result has been created for elapsed time calculations
21+ attr_writer :started_at
2022 # Explicitly set the command name that was used for this coverage result. Defaults to SimpleCov.command_name
2123 attr_writer :command_name
2224
@@ -25,11 +27,12 @@ class Result
2527
2628 # Initialize a new SimpleCov::Result from given Coverage.result (a Hash of filenames each containing an array of
2729 # coverage data)
28- def initialize ( original_result , command_name : nil , created_at : nil )
30+ def initialize ( original_result , command_name : nil , created_at : nil , started_at : nil )
2931 result = original_result
3032 @original_result = result . freeze
3133 @command_name = command_name
3234 @created_at = created_at
35+ @started_at = started_at
3336 @files = SimpleCov ::FileList . new ( result . map do |filename , coverage |
3437 SimpleCov ::SourceFile . new ( filename , JSON . parse ( JSON . dump ( coverage ) ) ) if File . file? ( filename )
3538 end . compact . sort_by ( &:filename ) )
@@ -52,8 +55,18 @@ def format!
5255 end
5356
5457 # Defines when this result has been created. Defaults to current truncated system monotonic uptime
58+ # Wall Clock (Realtime) is for knowing what time it is; it cannot be used for elapsed time calculations.
59+ # Ref: https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/
60+ # See: #started_at
5561 def created_at
56- @created_at ||= SimpleCov ::Timer . monotonic . truncate
62+ @created_at ||= SimpleCov ::Timer . wall . truncate
63+ end
64+
65+ # Monotonic Clock is for calculating elapsed time accurately.
66+ # Ref: https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/
67+ # See: #created_at
68+ def started_at
69+ @started_at ||= SimpleCov ::Timer . monotonic . truncate
5770 end
5871
5972 # The command name that launched this result.
@@ -67,15 +80,21 @@ def to_hash
6780 {
6881 command_name => {
6982 "coverage" => coverage ,
70- "timestamp" => created_at . to_i
83+ "timestamp" => created_at . to_i ,
84+ "started_at" => started_at . to_i
7185 }
7286 }
7387 end
7488
7589 # Loads a SimpleCov::Result#to_hash dump
7690 def self . from_hash ( hash )
7791 hash . map do |command_name , data |
78- new ( data . fetch ( "coverage" ) , command_name : command_name , created_at : Time . at ( data [ "timestamp" ] ) )
92+ new (
93+ data . fetch ( "coverage" ) ,
94+ command_name : command_name ,
95+ created_at : Time . at ( data [ "timestamp" ] ) ,
96+ started_at : data [ "started_at" ]
97+ )
7998 end
8099 end
81100
0 commit comments