@@ -15,8 +15,10 @@ class Result
15
15
# Returns all files that are applicable to this result (sans filters!) as instances of SimpleCov::SourceFile. Aliased as :source_files
16
16
attr_reader :files
17
17
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
19
19
attr_writer :created_at
20
+ # Explicitly set the Time (Monotonic) this result has been created for elapsed time calculations
21
+ attr_writer :started_at
20
22
# Explicitly set the command name that was used for this coverage result. Defaults to SimpleCov.command_name
21
23
attr_writer :command_name
22
24
@@ -25,11 +27,12 @@ class Result
25
27
26
28
# Initialize a new SimpleCov::Result from given Coverage.result (a Hash of filenames each containing an array of
27
29
# 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 )
29
31
result = original_result
30
32
@original_result = result . freeze
31
33
@command_name = command_name
32
34
@created_at = created_at
35
+ @started_at = started_at
33
36
@files = SimpleCov ::FileList . new ( result . map do |filename , coverage |
34
37
SimpleCov ::SourceFile . new ( filename , JSON . parse ( JSON . dump ( coverage ) ) ) if File . file? ( filename )
35
38
end . compact . sort_by ( &:filename ) )
@@ -52,8 +55,18 @@ def format!
52
55
end
53
56
54
57
# 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
55
61
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
57
70
end
58
71
59
72
# The command name that launched this result.
@@ -67,15 +80,21 @@ def to_hash
67
80
{
68
81
command_name => {
69
82
"coverage" => coverage ,
70
- "timestamp" => created_at . to_i
83
+ "timestamp" => created_at . to_i ,
84
+ "started_at" => started_at . to_i
71
85
}
72
86
}
73
87
end
74
88
75
89
# Loads a SimpleCov::Result#to_hash dump
76
90
def self . from_hash ( hash )
77
91
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
+ )
79
98
end
80
99
end
81
100
0 commit comments