Any/Adapter/Test.pm

filter: Statementstmt 95.7% 67 / 70 Branchbran 68.7% 11 / 16 Conditioncond 76.9% 10 / 13 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 93.3% 14 / 15 Podpod 71.4% 5 / 7 totaltotal 85.2% 110 / 129 CC 16 SCAR 13.0
CC
16
Cov
89%
__ANON__
8.1
store_distribution
4.0
use_full_distributions
2.1
CRAP 3.7
« Any.pm ↑ Summary Any/Adapter/Tee.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2020-2022 -- leonerd@leonerd.org.uk
5  
6 package Metrics::Any::Adapter::Test 0.10;
7  
8 5 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use base qw( Metrics::Any::AdapterBase::Stored );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use List::Util 1.29 qw( pairs );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 C<Metrics::Any::Adapter::Test> - a metrics reporting adapter for unit testing
20  
21 =head1 SYNOPSIS
22  
23 use Test2::V0;
24 use Metrics::Any::Adapter 'Test';
25  
26 {
27 Metrics::Any::Adapter::Test->clear;
28  
29 # perform some work in the code under test
30  
31 is( Metrics::Any::Adapter::Test->metrics,
32 "an_expected_metric = 1\n",
33 'Metrics were reported while doing something'
34 );
35 }
36  
37 =head1 DESCRIPTION
38  
39 This L<Metrics::Any> adapter type stores reported metrics locally, allowing
40 access to them by the L</metrics> method. This is useful to use in a unit test
41 to check that the code under test reports the correct metrics.
42  
43 This adapter supports timer metrics by storing as distributions. By default,
44 distributions store only a summary, giving the count and total duration. If
45 required, the full values can be stored by setting L</use_full_distributions>.
46  
47 For predictable output of timer metrics in unit tests, a unit test may wish to
48 use the L</override_timer_duration> method.
49  
50 This adapter type supports batch mode reporting. Callbacks are invoked at the
51 beginning of the L</metrics> method.
52  
53 =cut
54  
55 5 my $singleton;
56  
57 sub new
58 33 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
59 33 my $class = shift;
60 33 ▶ return $singleton //= $class->SUPER::new( @_ );
Condition66%2/3
$singleton //= $class->SUPER::new(@_)
l!l&&r!l&&!r
2940
MC/DC0%0/2
$singleton //= $class->SUPER::new(@_)
$singleton $class->SUPER::new(@_)
Truth table66%2/3
A //= B
A= $singletonB= $class->SUPER::new(@_)
ABresult
1X1
011
000
61 }
62  
63 # We're a singleton instance, so we can just store state in file-scoped
64 # lexicals
65  
66 my $timer_duration;
67 5 my $use_full_distributions;
68  
69 =head1 METHODS
70  
71 =cut
72  
73 =head2 metrics
74  
75 $result = Metrics::Any::Adapter::Test->metrics
76  
77 This class method returns a string describing all of the stored metric values.
78 Each is reported on a line formatted as
79  
80 name = value
81  
82 Each line, including the final one, is terminated by a linefeed. The metrics
83 are sorted alphabetically. Any multi-part metric names will be joined with
84 underscores (C<_>).
85  
86 Metrics that have additional labels are formatted with additional label names
87 and label values in declared order after the name and before the C<=> symbol:
88  
89 name l1:v1 l2:v2 = value
90  
91 =cut
92  
93 =head2 use_full_distributions
94  
95 Metrics::Any::Adapter::Test->use_full_distributions; # enables the option
96  
97 Metrics::Any::Adapter::Test->use_full_distributions( $enable );
98  
99 I<Since version 0.08.>
100  
101 If enabled, this option stores the full value of every reported observation
102 into distributions, rathr than just the count-and-total summary.
103  
104 Full value distributions will be formatted as a sequence of lines containing
105 the count of observations at that particular value, in square brackets,
106 followed by the summary count.
107  
108 name[v1] = c1
109 name[v2] = c2
110 ...
111 name_count = c
112  
113 In order not to be too sensitive to numerical rounding errors, values are
114 stored to only 3 decimal places.
115  
116 =cut
117  
118 sub use_full_distributions
119 1 ▶{
Subroutine use_full_distributions: called CC=2 SCAR=7.2
Pod documented
120 1 shift;
121 1 ▶ ( $use_full_distributions ) = @_ ? @_ : ( 1 );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
122 }
123  
124 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
125 5 DIST_COUNT => 0,
126 DIST_TOTAL => 1,
127 5 };
128  
129 sub store_distribution
130 11 ▶{
Subroutine store_distribution: called CC=4 SCAR=13.9
Pod documented
131 11 shift;
132 11 my ( $storage, $amount ) = @_;
133  
134 11 ▶ if( $use_full_distributions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_full_distributions) { } 3 8 11
135 3 ▶ $storage //= {};
Condition100%2/2
$storage //= {}
l!l
21
MC/DC100%1/1
$storage //= {}
$storage
Truth table100%2/2
A
A= $storage
Aresult
11
00
136  
137 3 $storage->{ sprintf "%.3f", $amount }++;
138 }
139 else {
140 8 ▶ $storage //= [ 0, 0 ];
Condition50%1/2
$storage //= [0, 0]
l!l
08
MC/DC0%0/1
$storage //= [0, 0]
$storage
Truth table50%1/2
A
A= $storage
Aresult
11
00
141 8 $storage->[DIST_COUNT] += 1;
142 8 $storage->[DIST_TOTAL] += $amount;
143 }
144  
145 11 return $storage;
146 }
147  
148 5 *store_timer = \&store_distribution;
149  
150 sub metrics
151 18 ▶{
Subroutine metrics: called CC=2 SCAR=7.0
Pod documented
152 18 my $self = shift;
153 18 ▶ ref $self or $self = Metrics::Any::Adapter::Test->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = "Metrics::Any::Adapter::Test"->new 0 18 18
154  
155 18 my @ret;
156  
157 30 ▶ $self->walk( sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
158 30 my ( $type, $name, $labels, $value ) = @_;
159  
160 30 $name .= sprintf " %s:%s", $_->key, $_->value for pairs @$labels;
161  
162 30 ▶ if( $type eq "counter" or $type eq "gauge" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'counter' or $type eq 'gauge') { } 21 9 30
elsif ($type eq 'distribution' or $type eq 'timer') { } 9 0 9
Condition100%3/3
$type eq "counter" or $type eq "gauge"
l!l&&r!l&&!r
1569
MC/DC100%2/2
$type eq "counter" or $type eq "gauge"
$type eq "counter" $type eq "gauge"
Truth table100%3/3
A or B
A= $type eq "counter"B= $type eq "gauge"
ABresult
1X1
011
000
163 21 push @ret, "$name = $value";
164 }
165 ▶ elsif( $type eq "distribution" or $type eq "timer" ) {
Condition66%2/3
$type eq "distribution" or $type eq "timer"
l!l&&r!l&&!r
540
MC/DC0%0/2
$type eq "distribution" or $type eq "timer"
$type eq "distribution" $type eq "timer"
Truth table66%2/3
A or B
A= $type eq "distribution"B= $type eq "timer"
ABresult
1X1
011
000
166 9 ▶ if( reftype $value eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($value) eq 'HASH') { } 1 8 9
167 1 my $total = 0;
168 1 foreach my $k ( sort { $a <=> $b } keys %$value ) {
169 3 my $v = $value->{$k};
170 # Trim trailing zeroes for neatness
171 3 $k =~ s/\.0+$/./; $k =~ s/\.$//;
172  
173 3 push @ret, "$name\[$k] = $v";
174 3 $total += $v;
175 }
176 1 push @ret, "${name}_count = $total";
177 }
178 else {
179 8 push @ret, "${name}_count = " . $value->[DIST_COUNT];
180 8 push @ret, "${name}_total = " . $value->[DIST_TOTAL];
181 }
182 }
183 else {
184 0 warn "Unsure how to handle metric of type $type\n";
185 }
186 18 } );
187  
188 18 return join "", map { "$_\n" } @ret;
189 }
190  
191 =head2 clear
192  
193 Metrics::Any::Adapter::Test->clear
194  
195 This class method removes all of the stored values of reported metrics.
196  
197 =cut
198  
199 sub clear
200 11 ▶{
Subroutine clear: called CC=2 SCAR=7.0
Pod documented
201 11 my $self = shift;
202 11 ▶ ref $self or $self = Metrics::Any::Adapter::Test->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = "Metrics::Any::Adapter::Test"->new 0 11 11
203  
204 11 $self->clear_values;
205  
206 11 undef $timer_duration;
207 }
208  
209 =head2 override_timer_duration
210  
211 Metrics::Any::Adapter::Test->override_timer_duration( $duration )
212  
213 This class method sets a duration value, that any subsequent call to
214 C<inc_timer> will use instead of the value the caller actually passed in. This
215 will ensure reliably predictable output in unit tests.
216  
217 Any value set here will be cleared by L</clear>.
218  
219 =cut
220  
221 sub override_timer_duration
222 0 ▶{
Subroutine override_timer_duration: not called CC=1 SCAR=6.9
Pod documented
223 0 shift;
224 0 ( $timer_duration ) = @_;
225 }
226  
227 sub report_timer
228 4 ▶{
Subroutine report_timer: called CC=2 SCAR=7.0
Pod undocumented
229 4 my $self = shift;
230 4 my ( $handle, $duration, @labelvalues ) = @_;
231  
232 4 ▶ $duration = $timer_duration if defined $timer_duration;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timer_duration 0 4 4
233  
234 4 $self->SUPER::report_timer( $handle, $duration, @labelvalues );
235 }
236  
237 =head1 AUTHOR
238  
239 Paul Evans <leonerd@leonerd.org.uk>
240  
241 =cut
242  
243 5 0x55AA;
« Any.pm ↑ Summary Any/Adapter/Tee.pm »