| 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.08; | ||||||||||||||||||||||||||||||||
| 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 Test::More; | ||||||||||||||||||||||||||||||||
| 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 | =cut | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 5 | my $singleton; | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | sub new | ||||||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||||||
| 56 | 31 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 57 | 31 | ▶ | return $singleton //= $class->SUPER::new( @_ ); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$singleton //= $class->SUPER::new(@_)
MC/DC0%0/2
$singleton //= $class->SUPER::new(@_)
$singleton
$class->SUPER::new(@_)
Truth table66%2/3
A //= B
A= $singletonB= $class->SUPER::new(@_)
| |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | # We're a singleton instance, so we can just store state in file-scoped | ||||||||||||||||||||||||||||||||
| 61 | # lexicals | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | my $timer_duration; | ||||||||||||||||||||||||||||||||
| 64 | 5 | my $use_full_distributions; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | =head2 metrics | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | $result = Metrics::Any::Adapter::Test->metrics | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | This class method returns a string describing all of the stored metric values. | ||||||||||||||||||||||||||||||||
| 75 | Each is reported on a line formatted as | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | name = value | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | Each line, including the final one, is terminated by a linefeed. The metrics | ||||||||||||||||||||||||||||||||
| 80 | are sorted alphabetically. Any multi-part metric names will be joined with | ||||||||||||||||||||||||||||||||
| 81 | underscores (C<_>). | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | Metrics that have additional labels are formatted with additional label names | ||||||||||||||||||||||||||||||||
| 84 | and label values in declared order after the name and before the C<=> symbol: | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | name l1:v1 l2:v2 = value | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | =head2 use_full_distributions | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | Metrics::Any::Adapter::Test->use_full_distributions; # enables the option | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | Metrics::Any::Adapter::Test->use_full_distributions( $enable ); | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | I<Since version 0.08.> | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | If enabled, this option stores the full value of every reported observation | ||||||||||||||||||||||||||||||||
| 99 | into distributions, rathr than just the count-and-total summary. | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | Full value distributions will be formatted as a sequence of lines containing | ||||||||||||||||||||||||||||||||
| 102 | the count of observations at that particular value, in square brackets, | ||||||||||||||||||||||||||||||||
| 103 | followed by the summary count. | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | name[v1] = c1 | ||||||||||||||||||||||||||||||||
| 106 | name[v2] = c2 | ||||||||||||||||||||||||||||||||
| 107 | ... | ||||||||||||||||||||||||||||||||
| 108 | name_count = c | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | In order not to be too sensitive to numerical rounding errors, values are | ||||||||||||||||||||||||||||||||
| 111 | stored to only 3 decimal places. | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | sub use_full_distributions | ||||||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | shift; | ||||||||||||||||||||||||||||||
|
Subroutine
use_full_distributions: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | ( $use_full_distributions ) = @_ ? @_ : ( 1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | use constant { | ||||||||||||||||||||||||||||||||
| 122 | 5 | DIST_COUNT => 0, | |||||||||||||||||||||||||||||||
| 123 | DIST_TOTAL => 1, | ||||||||||||||||||||||||||||||||
| 124 | 5 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | sub store_distribution | ||||||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||||||
| 128 | 11 | ▶ | shift; | ||||||||||||||||||||||||||||||
|
Subroutine
store_distribution: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 129 | 11 | my ( $storage, $amount ) = @_; | |||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 11 | ▶ | if( $use_full_distributions ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | $storage //= {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$storage //= {}
MC/DC100%1/1
$storage //= {}
$storage
Truth table100%2/2
A
A= $storage
| |||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 3 | $storage->{ sprintf "%.3f", $amount }++; | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||||||
| 137 | 8 | ▶ | $storage //= [ 0, 0 ]; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$storage //= [0, 0]
MC/DC0%0/1
$storage //= [0, 0]
$storage
Truth table50%1/2
A
A= $storage
| |||||||||||||||||||||||||||||||||
| 138 | 8 | $storage->[DIST_COUNT] += 1; | |||||||||||||||||||||||||||||||
| 139 | 8 | $storage->[DIST_TOTAL] += $amount; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 11 | return $storage; | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 5 | *store_timer = \&store_distribution; | |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | sub metrics | ||||||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||||||
| 149 | 17 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
metrics: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 150 | 17 | ▶ | ref $self or $self = Metrics::Any::Adapter::Test->new; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 17 | my @ret; | |||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | $self->walk( sub { | ||||||||||||||||||||||||||||||||
| 155 | 28 | ▶ | my ( $type, $name, $labels, $value ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 28 | $name .= sprintf " %s:%s", $_->key, $_->value for pairs @$labels; | |||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 28 | ▶ | if( $type eq "counter" or $type eq "gauge" ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$type eq "counter" or $type eq "gauge"
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"
| |||||||||||||||||||||||||||||||||
| 160 | 19 | push @ret, "$name = $value"; | |||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | ▶ | elsif( $type eq "distribution" or $type eq "timer" ) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$type eq "distribution" or $type eq "timer"
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"
| |||||||||||||||||||||||||||||||||
| 163 | 9 | ▶ | if( reftype $value eq "HASH" ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | 1 | my $total = 0; | |||||||||||||||||||||||||||||||
| 165 | 1 | foreach my $k ( sort { $a <=> $b } keys %$value ) { | |||||||||||||||||||||||||||||||
| 166 | 3 | my $v = $value->{$k}; | |||||||||||||||||||||||||||||||
| 167 | # Trim trailing zeroes for neatness | ||||||||||||||||||||||||||||||||
| 168 | 3 | $k =~ s/\.0+$/./; $k =~ s/\.$//; | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 3 | push @ret, "$name\[$k] = $v"; | |||||||||||||||||||||||||||||||
| 171 | 3 | $total += $v; | |||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | 1 | push @ret, "${name}_count = $total"; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||||||
| 176 | 8 | push @ret, "${name}_count = " . $value->[DIST_COUNT]; | |||||||||||||||||||||||||||||||
| 177 | 8 | push @ret, "${name}_total = " . $value->[DIST_TOTAL]; | |||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||||||
| 181 | 0 | warn "Unsure how to handle metric of type $type\n"; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | 17 | } ); | |||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 17 | return join "", map { "$_\n" } @ret; | |||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | =head2 clear | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | Metrics::Any::Adapter::Test->clear | ||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | This class method removes all of the stored values of reported metrics. | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | sub clear | ||||||||||||||||||||||||||||||||
| 197 | { | ||||||||||||||||||||||||||||||||
| 198 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
clear: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 199 | 10 | ▶ | ref $self or $self = Metrics::Any::Adapter::Test->new; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 10 | $self->clear_values; | |||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | 10 | undef $timer_duration; | |||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | =head2 override_timer_duration | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | Metrics::Any::Adapter::Test->override_timer_duration( $duration ) | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | This class method sets a duration value, that any subsequent call to | ||||||||||||||||||||||||||||||||
| 211 | C<inc_timer> will use instead of the value the caller actually passed in. This | ||||||||||||||||||||||||||||||||
| 212 | will ensure reliably predictable output in unit tests. | ||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | Any value set here will be cleared by L</clear>. | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | sub override_timer_duration | ||||||||||||||||||||||||||||||||
| 219 | { | ||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | shift; | ||||||||||||||||||||||||||||||
|
Subroutine
override_timer_duration: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 221 | 0 | ( $timer_duration ) = @_; | |||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | sub report_timer | ||||||||||||||||||||||||||||||||
| 225 | { | ||||||||||||||||||||||||||||||||
| 226 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
report_timer: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 227 | 4 | my ( $handle, $duration, @labelvalues ) = @_; | |||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 4 | ▶ | $duration = $timer_duration if defined $timer_duration; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | 4 | $self->SUPER::report_timer( $handle, $duration, @labelvalues ); | |||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | =cut | ||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | 5 | 0x55AA; | |||||||||||||||||||||||||||||||