| 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
$args{'time'} || 120
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'}
Truth table50%1/2
A
A= $args{'time'}
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
@{$self->{"window"};} and $self->{"window"}[0][0] < $now
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
| |||||||||||||||||||||||||||||
| 26 | 0 | my $entry= shift @{$self->{window}}; | |||||||||||||||||||||||||||
| 27 | 0 | $self->{window_total}--; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | $self->{window_success}-- if $entry->[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 49 | 670 | return; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | 1; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | __END__ | ||||||||||||||||||||||||||||