File Coverage

blib/lib/Mojolicious/Plugin/Statsd/Adapter/Memory.pm
Criterion Covered Total %
statement 21 23 91.3
branch 5 12 41.6
condition 14 18 77.7
subroutine 3 3 100.0
pod 2 2 100.0
total 45 58 77.5


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Statsd::Adapter::Memory;
2             $Mojolicious::Plugin::Statsd::Adapter::Memory::VERSION = '0.04';
3 2     2   209211 use Mojo::Base -base;
  2         11  
  2         16  
4              
5             # scalar values: counter
6             # hashref values: timings (keys: samples[] avg min max)
7             has stats => sub {
8             {}
9             };
10              
11             sub timing {
12 5     5 1 33 my ($self, $names, $time, $sample_rate) = @_;
13              
14 5 50 50     26 if (($sample_rate // 1) != 1) {
15 0 0       0 return unless rand() <= $sample_rate;
16             }
17              
18 5         13 my $stats = $self->stats;
19 5         26 for my $key (@$names) {
20 5   100     22 my $timing = $stats->{$key} //= {};
21              
22 5   100     18 ($timing->{samples} //= 0) += 1;
23              
24             $timing->{avg} =
25 5   100     23 (($timing->{avg} // 0) + $time) / $timing->{samples};
26              
27 5 50 66     22 if (!exists $timing->{min} or $timing->{min} > $time) {
28 5         9 $timing->{min} = $time;
29             }
30              
31 5 100 66     18 if (!exists $timing->{max} or $timing->{max} < $time) {
32 3         8 $timing->{max} = $time;
33             }
34             }
35 5         24 return 1;
36             }
37              
38             sub counter {
39 9     9 1 54 my ($self, $counters, $delta, $sample_rate) = @_;
40              
41 9 50 50     47 if (($sample_rate // 1) != 1) {
42 0 0       0 return unless rand() <= $sample_rate;
43             }
44              
45 9         20 my $stats = $self->stats;
46              
47 9         42 for my $name (@$counters) {
48 11   100     37 ($stats->{$name} //= 0) += $delta;
49             }
50 9         38 return 1;
51             }
52              
53             1;
54             __END__