blib/lib/Throttle/Adaptive.pm

filter: Statementstmt 88.5% 31 / 35 Branchbran 62.5% 5 / 8 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 7 / 7 Podpod 66.6% 2 / 3 totaltotal 75.0% 48 / 64 CC 8 SCAR 12.4
CC
8
Cov
78%
_process_window
6.8
new
3.3
should_fail
2.0
CRAP 3.5
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Throttle::Adaptive;
4 1 our $AUTHORITY = 'cpan:TVDW';
5 1 $Throttle::Adaptive::VERSION = '0.001';
6 # ABSTRACT: implementation of the "adaptive throttling" algorithm by Google
7  
8 1 ▶use Time::HiRes qw/CLOCK_MONOTONIC/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
11 1 my ($class, %args)= @_;
12 return bless {
13 ratio => $args{ratio} || 2,
14 1 ▶ time => $args{time} || 120,
Condition50%2/4
$args{'ratio'} || 2
l!l
10
$args{'time'} || 120
l!l
10
MC/DC0%0/1
$args{'ratio'} || 2
$args{'ratio'}
MC/DC0%0/1
$args{'time'} || 120
$args{'time'}
Truth table50%1/2
A
A= $args{'ratio'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'time'}
Aresult
11
00
15  
16 window => [],
17 window_success => 0,
18 window_total => 0,
19 }, $class;
20 }
21  
22 400 ▶sub _process_window {
Subroutine _process_window: called CC=4 SCAR=19.2
23 400 my ($self)= @_;
24 400 my $now= Time::HiRes::clock_gettime(CLOCK_MONOTONIC);
25 400 ▶ while (@{$self->{window}} && $self->{window}[0][0] < $now) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"window"};} and $self->{"window"}[0][0] < $now) 0 400 400
Condition33%1/3
@{$self->{"window"};} and $self->{"window"}[0][0] < $now
!ll&&!rl&&r
04000
MC/DC0%0/2
@{$self->{"window"};} and $self->{"window"}[0][0] < $now
@{$self->{"window"};} $self->{"window"}[0][0] < $now
Truth table33%1/3
A and B
A= @{$self->{"window"};}B= $self->{"window"}[0][0] < $now
ABresult
0X0
100
111
26 0 my $entry= shift @{$self->{window}};
27 0 $self->{window_total}--;
28 0 ▶ $self->{window_success}-- if $entry->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $entry->[1] 0 0 0
29 }
30 400 return;
31 }
32  
33 400 ▶sub should_fail {
Subroutine should_fail: called CC=2 SCAR=6.9
Pod documented
34 400 my ($self)= @_;
35 400 $self->_process_window;
36  
37 400 my $fail= ( rand() < (($self->{window_total} - ($self->{ratio} * $self->{window_success})) / ($self->{window_total} + 1)) );
38 400 ▶ $self->count(1) if $fail;
Branch: 1 branch
BranchTrueFalseTotal
if $fail 70 330 400
39 400 return $fail;
40 }
41  
42 670 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
43 670 my ($self, $error)= @_;
44 670 my $success= !$error;
45  
46 670 $self->{window_total}++;
47 670 push @{$self->{window}}, [ Time::HiRes::clock_gettime(CLOCK_MONOTONIC)+$self->{time}, $success ];
48 670 ▶ $self->{window_success}++ if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 200 470 670
49 670 return;
50 }
51  
52 1 1;
53  
54 __END__
↑ Summary