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              
3 2     2   88264 use Mojo::Base -base;
  2         4  
  2         14  
4              
5             our $VERSION = '0.06';
6              
7             # scalar values: counter
8             # hashref values: timings (keys: samples[] avg min max)
9             has stats => sub {
10             {}
11             };
12              
13             sub timing {
14 5     5 1 37 my ($self, $names, $time, $sample_rate) = @_;
15              
16 5 50 50     31 if (($sample_rate // 1) != 1) {
17 0 0       0 return unless rand() <= $sample_rate;
18             }
19              
20 5         17 my $stats = $self->stats;
21 5         28 for my $key (@$names) {
22 5   100     21 my $timing = $stats->{$key} //= {};
23              
24 5   100     21 ($timing->{samples} //= 0) += 1;
25              
26             $timing->{avg} =
27 5   100     22 (($timing->{avg} // 0) + $time) / $timing->{samples};
28              
29 5 50 66     27 if (!exists $timing->{min} or $timing->{min} > $time) {
30 5         9 $timing->{min} = $time;
31             }
32              
33 5 100 66     30 if (!exists $timing->{max} or $timing->{max} < $time) {
34 3         8 $timing->{max} = $time;
35             }
36             }
37 5         23 return 1;
38             }
39              
40             sub counter {
41 9     9 1 64 my ($self, $counters, $delta, $sample_rate) = @_;
42              
43 9 50 50     51 if (($sample_rate // 1) != 1) {
44 0 0       0 return unless rand() <= $sample_rate;
45             }
46              
47 9         26 my $stats = $self->stats;
48              
49 9         49 for my $name (@$counters) {
50 11   100     47 ($stats->{$name} //= 0) += $delta;
51             }
52 9         47 return 1;
53             }
54              
55             1;
56              
57             # ABSTRACT In-Memory stat recording
58              
59             __END__