Throttler/Rate/AnyEvent.pm

filter: Statementstmt 87.1% 251 / 288 Branchbran 67.6% 69 / 102 Conditioncond 37.5% 12 / 32 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 97.3% 36 / 37 Podpod 100% 9 / 9 totaltotal 78.2% 381 / 487 CC 78 SCAR 29.0
CC
78
Cov
79%
_binsearch
90.0
del
57.0
new
8.3
CRAP 18.3
↑ Summary Throttler.pm »
1 package Sub::Throttler::Rate::AnyEvent;
2 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our @CARP_NOT = qw( Sub::Throttler );
8  
9 3 our $VERSION = 'v0.2.10';
10  
11 3 ▶use parent qw( Sub::Throttler::algo );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Sub::Throttler qw( throttle_flush );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Time::HiRes qw( clock_gettime CLOCK_MONOTONIC time sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use List::Util qw( min );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 40 ▶sub new {
Subroutine new: called CC=8 SCAR=21.2
Pod documented
21 3 ▶ use warnings FATAL => qw( misc );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 my ($class, %opt) = @_;
23 my $self = bless {
24 limit => delete $opt{limit} // 1,
25 39 ▶ period => delete $opt{period} // 1,
Condition71%5/7
delete $opt{"limit"} // 1
l!l
2811
delete $opt{"period"} // 1
l!l
1623
ref $class || $class
l!l&&r!l&&!r
0390
MC/DC100%1/1
delete $opt{"limit"} // 1
delete $opt{"limit"}
MC/DC100%1/1
delete $opt{"period"} // 1
delete $opt{"period"}
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table100%2/2
A
A= delete $opt{"limit"}
Aresult
11
00
Truth table100%2/2
A
A= delete $opt{"period"}
Aresult
11
00
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
26 acquired=> {}, # { $id => { $key => [$time, $quantity], … }, … }
27 used => {}, # { $key => { next => $idx, data => [ $time, … ] }, … }
28 _cb => undef, # callback for timer
29 _t => undef, # undef or AE::timer
30 }, ref $class || $class;
31 39 ▶ croak 'limit must be an unsigned integer' if $self->{limit} !~ /\A\d+\z/ms;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"limit"} =~ /\A\d+\z/ms 2 37 39
32 37 ▶ croak 'period must be a positive number' if $self->{period} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"period"} <= 0 0 37 37
33 37 ▶ croak 'period is too large' if $self->{period} >= -Sub::Throttler::Rate::rr::EMPTY();
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"period"} >= -&Sub::Throttler::Rate::rr::EMPTY() 0 37 37
34 37 ▶ croak 'bad param: '.(keys %opt)[0] if keys %opt;
Branch: 1 branch
BranchTrueFalseTotal
if keys %opt 2 35 37
35 35 weaken(my $this = $self);
36 35 ▶ $self->{_cb} = sub { $this && $this->_tick() };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$this and $this->_tick 13 0 13
37 35 return $self;
38 }
39  
40 11 ▶sub acquire {
Subroutine acquire: called CC=5 SCAR=17.0
Pod documented
41 11 my ($self, $id, $key, $quantity) = @_;
42 11 ▶ if (!$self->try_acquire($id, $key, $quantity)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->try_acquire($id, $key, $quantity)) 11 0 11
43 11 ▶ if ($quantity <= $self->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quantity <= $self->{"limit"}) 11 0 11
44 11 my $now = clock_gettime(CLOCK_MONOTONIC);
45 11 my $delay = $self->{used}{$key}->get($quantity) + $self->{period} - $now;
46 11 $delay = sprintf '%.6f', $delay; # work around floating point precision issues
47 # resource may expire between try_acquire() and clock_gettime()
48 11 ▶ if ($delay > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delay > 0) 11 0 11
49 11 sleep $delay;
50 }
51 }
52 11 ▶ if (!$self->try_acquire($id, $key, $quantity)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->try_acquire($id, $key, $quantity)) 0 11 11
53 0 croak "$self: unable to acquire $quantity of resource '$key'";
54 }
55 }
56 11 return $self;
57 }
58  
59 11 ▶sub limit {
Subroutine limit: called CC=5 SCAR=16.1
Pod documented
60 11 my ($self, $limit) = @_;
61 11 ▶ if (1 == @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @_) 2 9 11
62 2 return $self->{limit};
63 }
64 9 ▶ croak 'limit must be an unsigned integer' if $limit !~ /\A\d+\z/ms;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit =~ /\A\d+\z/ms 0 9 9
65 # OPTIMIZATION call throttle_flush() only if amount of available
66 # resources increased (i.e. limit was increased)
67 9 my $resources_increases = $self->{limit} < $limit;
68 9 $self->{limit} = $limit;
69 9 for my $rr (values %{ $self->{used} }) {
70 7 $rr->resize($self->{limit});
71 }
72 9 ▶ if ($resources_increases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resources_increases) 2 7 9
73 2 throttle_flush();
74 }
75 9 return $self;
76 }
77  
78 5 ▶sub load {
Subroutine load: called CC=8 SCAR=20.8
Pod documented
79 5 my ($class, $state) = @_;
80 5 ▶ croak 'bad state: wrong algorithm' if $state->{algo} ne __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"algo"} ne __PACKAGE__ 1 4 5
81 4 my $v = version->parse($state->{version});
82 4 ▶ if ($v > $VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v > $VERSION) 1 3 4
83 1 carp 'restoring state saved by future version';
84 }
85 4 my $self = $class->new(limit=>$state->{limit}, period=>$state->{period});
86 4 $self->{used} = dclone($state->{used});
87 4 my ($time, $now) = (time, clock_gettime(CLOCK_MONOTONIC));
88 # time jump backward, no matter how much, handled like we still is in
89 # current period, to be safe
90 4 ▶ if ($state->{at} > $time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"at"} > $time) 2 2 4
91 2 $time = $state->{at};
92 }
93 4 my $diff = $time - $now;
94 4 for my $data (map {$_->{data}} values %{ $self->{used} }) {
95 4 for (@{ $data }) {
96 12 ▶ if ($_ != Sub::Throttler::Rate::rr::EMPTY()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ != &Sub::Throttler::Rate::rr::EMPTY()) 12 0 12
97 12 $_ -= $diff;
98 12 $_ = sprintf '%.6f', $_; # work around floating point precision issues
99 }
100 }
101 }
102 4 for (values %{ $self->{used} }) {
103 4 bless $_, 'Sub::Throttler::Rate::rr';
104 }
105 4 $self->{_t} = AE::timer 0, 0, $self->{_cb};
106 4 return $self;
107 }
108  
109 8 ▶sub period {
Subroutine period: called CC=6 SCAR=18.2
Pod documented
110 8 my ($self, $period) = @_;
111 8 ▶ if (1 == @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @_) 5 3 8
112 5 return $self->{period};
113 }
114 3 ▶ croak 'period must be a positive number' if $period <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $period <= 0 0 3 3
115 3 ▶ croak 'period is too large' if $self->{period} >= -Sub::Throttler::Rate::rr::EMPTY();
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"period"} >= -&Sub::Throttler::Rate::rr::EMPTY() 0 3 3
116 # OPTIMIZATION call throttle_flush() only if amount of available
117 # resources increased (i.e. period was decreased)
118 3 my $resources_increases = $self->{period} > $period;
119 3 $self->{period} = $period;
120 3 ▶ if ($resources_increases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resources_increases) 1 2 3
121 1 ▶ if ($self->{_t}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_t'}) { } 1 0 1
122 1 $self->{_t} = undef;
123 1 $self->_tick();
124 }
125 else {
126 0 throttle_flush();
127 }
128 }
129 3 return $self;
130 }
131  
132 28 ▶sub release {
Subroutine release: called CC=2 SCAR=6.9
Pod documented
133 28 my ($self, $id) = @_;
134 28 ▶ croak sprintf '%s not acquired anything', $id if !$self->{acquired}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"acquired"}{$id} 3 25 28
135 25 delete $self->{acquired}{$id};
136 25 return $self;
137 }
138  
139 26 ▶sub release_unused {
Subroutine release_unused: called CC=5 SCAR=16.1
Pod documented
140 26 my ($self, $id) = @_;
141 26 ▶ croak sprintf '%s not acquired anything', $id if !$self->{acquired}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"acquired"}{$id} 3 23 26
142  
143 23 my $now = clock_gettime(CLOCK_MONOTONIC);
144 23 for my $key (grep {$self->{used}{$_}} keys %{ $self->{acquired}{$id} }) {
145 22 my ($time, $quantity) = @{ $self->{acquired}{$id}{$key} };
146 22 $self->{used}{$key}->del($time, $quantity);
147 # clean up (avoid memory leak in long run with unique keys)
148 22 ▶ if ($self->{used}{$key}->get($self->{limit}) + $self->{period} <= $now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"used"}{$key}->get($self->{"limit"}) + $self->{"period"} <= $now) 16 6 22
149 16 delete $self->{used}{$key};
150 }
151 }
152 23 delete $self->{acquired}{$id};
153 23 throttle_flush();
154 23 ▶ if (!keys %{ $self->{used} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$$self{"used"};}) 14 9 23
155 14 $self->{_t} = undef;
156 }
157 23 return $self;
158 }
159  
160 1 ▶sub save {
Subroutine save: called CC=5 SCAR=16.1
Pod documented
161 1 my ($self) = @_;
162 1 my ($time, $now) = (time, clock_gettime(CLOCK_MONOTONIC));
163 1 my $diff = $time - $now;
164 my $state = {
165 algo => __PACKAGE__,
166 version => version->declare($VERSION)->numify,
167 limit => $self->{limit},
168 period => $self->{period},
169 1 used => dclone($self->{used}),
170 at => $time,
171 };
172 1 for my $data (map {$_->{data}} values %{ $state->{used} }) {
173 1 for (@{ $data }) {
174 3 ▶ if ($_ != Sub::Throttler::Rate::rr::EMPTY()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ != &Sub::Throttler::Rate::rr::EMPTY()) 3 0 3
175 3 $_ += $diff;
176 3 $_ = sprintf '%.6f', $_; # work around floating point precision issues
177 }
178 }
179 }
180 1 for (values %{ $state->{used} }) {
181 1 $_ = {%{ $_ }}; # unbless
182 }
183 1 return $state;
184 }
185  
186 166 ▶sub try_acquire {
Subroutine try_acquire: called CC=7 SCAR=19.5
Pod documented
187 166 my ($self, $id, $key, $quantity) = @_;
188 croak sprintf '%s already acquired %s', $id, $key
189 166 ▶ if $self->{acquired}{$id} && exists $self->{acquired}{$id}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"acquired"}{$id} and exists $self->{"acquired"}{$id}{$key} 2 164 166
Condition100%3/3
$self->{"acquired"}{$id} and exists $self->{"acquired"}{$id}{$key}
!ll&&!rl&&r
147172
MC/DC100%2/2
$self->{"acquired"}{$id} and exists $self->{"acquired"}{$id}{$key}
$self->{"acquired"}{$id} exists $self->{"acquired"}{$id}{$key}
Truth table100%3/3
A and B
A= $self->{"acquired"}{$id}B= exists $self->{"acquired"}{$id}{$key}
ABresult
0X0
100
111
190 164 ▶ croak 'quantity must be positive' if $quantity <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $quantity <= 0 2 162 164
191  
192 162 my $now = clock_gettime(CLOCK_MONOTONIC);
193  
194 162 ▶ $self->{used}{$key} ||= Sub::Throttler::Rate::rr->new($self->{limit});
Condition66%2/3
$self->{"used"}{$key} ||= "Sub::Throttler::Rate::rr"->new($self->{"limit"})
l!l&&r!l&&!r
99630
MC/DC0%0/2
$self->{"used"}{$key} ||= "Sub::Throttler::Rate::rr"->new($self->{"limit"})
$self->{"used"}{$key} "Sub::Throttler::Rate::rr"->new($self->{"limit"})
Truth table66%2/3
A ||= B
A= $self->{"used"}{$key}B= "Sub::Throttler::Rate::rr"->new($self->{"limit"})
ABresult
1X1
011
000
195 162 ▶ if (!$self->{used}{$key}->add($self->{period}, $now, $quantity)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"used"}{$key}->add($self->{"period"}, $now, $quantity)) 68 94 162
196 68 return;
197 }
198  
199 94 $self->{acquired}{$id}{$key} = [$now, $quantity];
200 94 ▶ if (!$self->{_t}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_t"}) 45 49 94
201 45 $self->{_t} = AE::timer $self->{period}, 0, $self->{_cb};
202 }
203 94 return 1;
204 }
205  
206 14 ▶sub _tick {
Subroutine _tick: called CC=6 SCAR=18.1
207 14 my $self = shift;
208 14 my $now = clock_gettime(CLOCK_MONOTONIC);
209 14 my $when = 0;
210 14 for my $key (keys %{ $self->{used} }) {
211 20 my $after = $self->{used}{$key}->after($now - $self->{period});
212 20 ▶ if (!$after) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $after) { } 17 3 20
elsif (not $when or $when > $after) { } 3 0 3
213 17 delete $self->{used}{$key};
214 }
215 ▶ elsif (!$when || $when > $after) {
Condition33%1/3
not $when or $when > $after
l!l&&r!l&&!r
300
MC/DC0%0/2
not $when or $when > $after
not $when $when > $after
Truth table33%1/3
not(A) or B
A= not $whenB= $when > $after
ABresult
1X1
011
000
216 3 $when = $after;
217 }
218 }
219 14 ▶ $self->{_t} = !$when ? undef : AE::timer $when + $self->{period} - $now, 0, $self->{_cb};
Branch: 1 branch
BranchTrueFalseTotal
!$when ? : 11 3 14
220 14 throttle_flush();
221 14 return;
222 }
223  
224  
225 package Sub::Throttler::Rate::rr; ## no critic (ProhibitMultiplePackages)
226 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
231  
232 3 ▶use constant EMPTY => -1_000_000_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234  
235 63 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
236 63 my ($class, $len) = @_;
237 63 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0630
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
238 next => 0,
239 data => [ (EMPTY) x $len ],
240 }, ref $class || $class;
241 63 return $self;
242 }
243  
244 162 ▶sub add {
Subroutine add: called CC=4 SCAR=13.9
245 162 my ($self, $period, $time, $quantity) = @_;
246 162 $time = sprintf '%.6f', $time; # work around floating point precision issues
247 162 my $len = @{ $self->{data} };
248 # try_acquire() guarantee $quantity > 0, so we continue only if $len > 0
249 # (thus avoid division by zero on % $len) and there is a chance to add
250 # $quantity elements
251 162 ▶ if ($quantity > $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quantity > $len) 3 159 162
252 3 return;
253 }
254 159 my $required = ($self->{next} + $quantity - 1) % $len;
255 # {data} is sorted, last added element ($self->{next}-1) is guaranteed
256 # to be largest of all elements, so all elements between (inclusive)
257 # $self->{next} and $required are guaranteed to be either EMPTY
258 # or <= $self->{next}-1 element, and $required element is largest of them
259 159 my $since = sprintf '%.6f', $time - $period; # work around floating point precision issues
260 159 ▶ if ($self->{data}[$required] > $since) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}[$required] > $since) 65 94 159
261 65 return;
262 }
263 94 for (1 .. $quantity) {
264 188 $self->{data}[ $self->{next} ] = $time;
265 188 ($self->{next} += 1) %= $len;
266 }
267 94 return 1;
268 }
269  
270 # Return time of acquiring first resource after $time or nothing.
271 20 ▶sub after {
Subroutine after: called CC=3 SCAR=11.0
272 20 my ($self, $time) = @_;
273 20 $time = sprintf '%.6f', $time; # work around floating point precision issues
274 # _tick() guarantee $time > EMPTY
275 20 my $len = @{ $self->{data} };
276 20 for (1 .. $len) {
277 46 $_ = ($self->{next} + $_ - 1) % $len;
278 46 ▶ return $self->{data}[ $_ ] if $self->{data}[ $_ ] > $time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}[$_] > $time 3 43 46
279 }
280 17 return;
281 }
282  
283 22 ▶sub del {
Subroutine del: called CC=15 SCAR=40.4
284 22 my ($self, $time, $quantity) = @_;
285 22 $time = sprintf '%.6f', $time; # work around floating point precision issues
286 # try_acquire() guarantee $quantity > 0
287 # even if $time is already outdated, these elements should be removed
288 # anyway in case {period} will be increased later
289 22 my $len = @{ $self->{data} };
290 22 ▶ if (!$len) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len) 0 22 22
291 0 return;
292 }
293 22 ▶ if ($quantity > $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quantity > $len) 0 22 22
294 0 $quantity = $len;
295 }
296 # OPTIMIZATION not in {data}
297 22 ▶ if ($self->{data}[ $self->{next} ] > $time) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'data'}[$self->{'next'}] > $time) { } 3 19 22
elsif ($self->{'data'}[$self->{'next'}] == $time) { } 16 3 19
elsif ($self->{'data'}[$self->{'next'} - 1] == $time) { } 3 0 3
298 3 return;
299 }
300 # OPTIMIZATION oldest
301 elsif ($self->{data}[ $self->{next} ] == $time) {
302 16 for (map { ($self->{next} + $_ - 1) % $len } 1 .. $quantity) {
303 # part of $quantity may be not in {data} (if {limit} was decreased)
304 26 ▶ return if $self->{data}[ $_ ] != $time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}[$_] != $time 0 26 26
305 26 $self->{data}[ $_ ] = EMPTY;
306 }
307 }
308 # OPTIMIZATION newest
309 elsif ($self->{data}[ $self->{next} - 1 ] == $time) {
310 3 for (map { $self->{next} - $_ } 1 .. $quantity) {
311 6 ▶ croak 'assert: newest: no time' if $self->{data}[ $_ ] != $time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}[$_] != $time 0 6 6
312 6 $self->{data}[ $_ ] = EMPTY;
313 }
314 3 $self->{next} = ($self->{next} - $quantity) % $len;
315 }
316 # middle (actually it support any case, not just middle)
317 else {
318 my $i = _binsearch($time, $self->{data}, $self->{next}, $len - 1)
319 0 ▶ // _binsearch($time, $self->{data}, 0, $self->{next} - 1);
Condition0%0/3
_binsearch($time, $self->{"data"}, $self->{"next"}, $len - 1) // _binsearch($time, $self->{"data"}, 0, $self->{"next"} - 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
_binsearch($time, $self->{"data"}, $self->{"next"}, $len - 1) // _binsearch($time, $self->{"data"}, 0, $self->{"next"} - 1)
_binsearch($time, $self->{"data"}, $self->{"next"}, $len - 1) _binsearch($time, $self->{"data"}, 0, $self->{"next"} - 1)
Truth table0%0/3
A // B
A= _binsearch($time, $self->{"data"}, $self->{"next"}, $len - 1)B= _binsearch($time, $self->{"data"}, 0, $self->{"next"} - 1)
ABresult
1X1
011
000
320 0 ▶ croak 'assert: middle: not found' if !defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 0 0
321 0 for (map { ($i + $_ - 1) % $len } 1 .. $quantity) {
322 0 ▶ croak 'assert: middle: no time' if $self->{data}[ $_ ] != $time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}[$_] != $time 0 0 0
323 0 $self->{data}[ $_ ] = EMPTY;
324 }
325 # OPTIMIZATION move minimum amount of elements
326 0 my $count_rew = ($self->{next} - $i) % $len;
327 0 my $count_fwd = ($i + $quantity - $self->{next}) % $len;
328 # move oldest elements forward
329 0 ▶ if ($count_fwd <= $count_rew) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count_fwd <= $count_rew) { } 0 0 0
330 0 @{ $self->{data} }[ map { ($self->{next}+$_-1) % $len } 1 .. $count_fwd ] =
331 0 @{ $self->{data} }[ map { ($self->{next}+$_-1) % $len } $count_fwd-$quantity+1 .. $count_fwd, 1 .. $count_fwd-$quantity ];
332 }
333 # move newest elements backward
334 else {
335 0 @{ $self->{data} }[ map { ($i+$_-1) % $len } 1 .. $count_rew ] =
336 0 @{ $self->{data} }[ map { ($i+$_-1) % $len } 1+$quantity .. $count_rew, 1 .. $quantity];
337 0 $self->{next} = ($self->{next} - $quantity) % $len;
338 }
339 }
340 19 return;
341 }
342  
343 33 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
344 33 my ($self, $id) = @_;
345 # $id is number of required element, counting from oldest one ($id = 1)
346 33 my $len = @{ $self->{data} };
347 # acquire() guarantee 0 < $id <= $len
348 33 my $i = ($self->{next} + $id - 1) % $len;
349 33 return $self->{data}[$i];
350 }
351  
352 7 ▶sub resize {
Subroutine resize: called CC=2 SCAR=6.9
353 7 my ($self, $newlen) = @_;
354 # limit() guarantee $newlen >= 0
355 7 my $len = @{ $self->{data} };
356 7 my $d = $self->{data};
357 7 $self->{data} = [ @{$d}[ $self->{next} .. $#{$d} ], @{$d}[ 0 .. $self->{next} - 1 ] ];
358 7 ▶ if ($newlen < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newlen < $len) { } 6 1 7
359 6 $self->{next} = 0;
360 6 splice @{ $self->{data} }, 0, $len - $newlen;
361 } else {
362 1 $self->{next} = $len % $newlen;
363 1 push @{ $self->{data} }, (EMPTY) x ($newlen - $len);
364 }
365 7 return $self;
366 }
367  
368 # From List::BinarySearch::PP version 0.23.
369 # Modified to support slices and work with array of numbers, without callback.
370 0 ▶sub _binsearch {
Subroutine _binsearch: not called CC=9 SCAR=45.0
371 0 my ( $target, $aref, $min, $max ) = @_;
372 0 ▶ $min //= 0;
Condition0%0/2
$min //= 0
l!l
00
MC/DC0%0/1
$min //= 0
$min
Truth table0%0/2
A
A= $min
Aresult
11
00
373 0 ▶ $max //= $#{$aref};
Condition0%0/2
$max //= $#{$aref;}
l!l
00
MC/DC0%0/1
$max //= $#{$aref;}
$max
Truth table0%0/2
A
A= $max
Aresult
11
00
374 0 ▶ croak 'bad slice' if $min < 0 || $#{$aref} < $max || $min > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $min < 0 or $#{$aref;} < $max or $min > $max 0 0 0
Condition0%0/6
$min < 0 or $#{$aref;} < $max or $min > $max
l!l&&r!l&&!r
000
$min < 0 or $#{$aref;} < $max
l!l&&r!l&&!r
000
MC/DC0%0/3
$min < 0 or $#{$aref;} < $max or $min > $max
$min < 0 $#{$aref;} < $max $min > $max
Truth table0%0/4
A or B or C
A= $min < 0B= $#{$aref;} < $maxC= $min > $max
ABCresult
1XX1
01X1
0011
0000
375 0 ▶ while ( $max > $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max > $min) 0 0 0
376 0 my $mid = int( ( $min + $max ) / 2 );
377 0 ▶ if ( $target > $aref->[$mid] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target > $aref->[$mid]) { } 0 0 0
378 0 $min = $mid + 1;
379 } else {
380 0 $max = $mid;
381 }
382 }
383 0 ▶ return $min if $target == $aref->[$min];
Branch: 1 branch
BranchTrueFalseTotal
if $target == $aref->[$min] 0 0 0
384 0 return;
385 }
386  
387  
388 3 1; # Magic true value required at end of module
389 __END__
↑ Summary Throttler.pm »