/
measurement_set.rb
128 lines (116 loc) · 2.66 KB
/
measurement_set.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module Yardstick
class MeasurementSet < OrderedSet
# The total number of measurements
#
# @example
# measurements.total # => 570
#
# @return [Integer]
# total measurements
#
# @api public
def total
length
end
# The number of successful measurements
#
# @example
# measurements.successful # => 561
#
# @return [Integer]
# successful measurements
#
# @api public
def successful
select { |measurement| measurement.ok? }.length
end
# The number of failed measurements
#
# @example
# measurements.failed # => 9
#
# @return [Integer]
# failed measurements
#
# @api public
def failed
total - successful
end
# The percentage of successful measurements
#
# @example
# coverage = measurements.coverage # => Rational(561, 570)
# '%.1f%%' % (coverage * 100) # => "98.4%"
#
# @return [Integer, Rational]
# the coverage percentage
#
# @api public
def coverage
empty? ? 0 : Rational(successful, total)
end
# Warn the unsuccessful measurements and a summary
#
# @example
# measurements.puts # (outputs measurements results and summary)
#
# @param [#puts] io
# optional object to puts the summary
#
# @return [undefined]
#
# @api public
def puts(io = $stdout)
each { |measurement| measurement.puts(io) }
puts_summary(io)
end
private
# Warn the summary of all measurements
#
# @param [#puts] io
# object to puts the summary
#
# @return [undefined]
#
# @api private
def puts_summary(io)
io.puts("\n#{[ coverage_text, successful_text, failed_text, total_text ].join(' ')}")
end
# The text for the coverage percentage to include in the summary
#
# @return [String]
# the coverage text
#
# @api private
def coverage_text
'Coverage: %.1f%%' % (coverage * 100)
end
# The text for the successful measurements to include in the summary
#
# @return [String]
# the successful text
#
# @api private
def successful_text
'Success: %d' % successful
end
# The text for the failed measurements to include in the summary
#
# @return [String]
# the failed text
#
# @api private
def failed_text
'Failed: %d' % failed
end
# The text for the total measurements to include in the summary
#
# @return [String]
# the total text
#
# @api private
def total_text
'Total: %d' % total
end
end # class MeasurementSet
end # module Yardstick