| 1 | package Sub::Throttle; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use List::Util qw(max); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Time::HiRes qw(time sleep); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | require Exporter; | |||||||||
| 11 | |||||||||||
| 12 | 1 | our @ISA = qw(Exporter); | |||||||||
| 13 | 1 | our @EXPORT_OK = qw(throttle); | |||||||||
| 14 | 1 | our %EXPORT_TAGS = ( | |||||||||
| 15 | all => [ @EXPORT_OK ], | ||||||||||
| 16 | ); | ||||||||||
| 17 | 1 | our @EXPORT = (); | |||||||||
| 18 | 1 | our $VERSION = '0.02'; | |||||||||
| 19 | |||||||||||
| 20 | 4 | ▶ | sub throttle { | ||||||||
|
Subroutine
throttle: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 21 | 4 | ▶ | croak "too few arguments to throttle\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | if @_ < 2; | ||||||||||
| 23 | 4 | my ($load, $func, @args) = @_; | |||||||||
| 24 | 4 | my @ret; | |||||||||
| 25 | 4 | my $start = time; | |||||||||
| 26 | 4 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 2 | @ret = $func->(@args); | |||||||||
| 28 | } else { | ||||||||||
| 29 | 2 | $ret[0] = $func->(@args); | |||||||||
| 30 | } | ||||||||||
| 31 | 4 | sleep(_sleep_secs($load, time - $start)); | |||||||||
| 32 | 4 | ▶ | wantarray ? @ret : $ret[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 8 | ▶ | sub _sleep_secs { | ||||||||
|
Subroutine
_sleep_secs: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 8 | my ($load, $elapsed) = @_; | |||||||||
| 37 | 8 | max($elapsed, 0) * (1 - $load) / $load; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | 1; | |||||||||
| 41 | =head1 NAME | ||||||||||
| 42 | |||||||||||
| 43 | Sub::Throttle - Throttle load of perl function | ||||||||||
| 44 | |||||||||||
| 45 | =head1 SYNOPSIS | ||||||||||
| 46 | |||||||||||
| 47 | use Sub::Throttle qw(throttle); | ||||||||||
| 48 | |||||||||||
| 49 | my $load = 0.1; | ||||||||||
| 50 | |||||||||||
| 51 | throttle($load, sub { ... }); | ||||||||||
| 52 | throttle($load, \&subref, @args); | ||||||||||
| 53 | |||||||||||
| 54 | =head1 DESCRIPTION | ||||||||||
| 55 | |||||||||||
| 56 | Throttles the load of perl function by calling L<sleep>. | ||||||||||
| 57 | |||||||||||
| 58 | =head1 METHODS | ||||||||||
| 59 | |||||||||||
| 60 | =head2 throttle($load, $subref [, @subargs]) | ||||||||||
| 61 | |||||||||||
| 62 | Calls L<sleep> after executing $subref with given @subargs so that the ratio of execution time becomes equal to $load. | ||||||||||
| 63 | |||||||||||
| 64 | =head1 AUTHOR | ||||||||||
| 65 | |||||||||||
| 66 | Kazuho Oku E<lt>kazuhooku at gmail.comE<gt> | ||||||||||
| 67 | |||||||||||
| 68 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 69 | |||||||||||
| 70 | Copyright (C) 2008 by Cybozu Labs, Inc. | ||||||||||
| 71 | |||||||||||
| 72 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 73 | it under the same terms as Perl itself, either Perl version 5.8.6 or, | ||||||||||
| 74 | at your option, any later version of Perl 5 you may have available. | ||||||||||
| 75 | |||||||||||
| 76 | =cut | ||||||||||