Sign in to PrePAN

Guard::Stat Generate guard objects and gather overall usage statistics from them



use strict;
use warnings;

use Guard::Stat;

my $stat = Guard::Stat->new;

my $guard = $stat->guard; # increase alive counter
print "Total active objects: ".$stat->running;
$guard->end("foo"); # increase finished counter
print "Finished, but not reaped: ". $stat->zombie;
undef $guard; # increase dead counter

my $hash = $stat->get_stat; # alive = 0, dead = 1, total = 1, etc

# more advanced stuff

$stat->on_level( 20, sub { print "Count increased to 20!" } );
$stat->on_level( -10, sub { print "Count dropped to 10!" } );
$stat->on_level( 0, sub { print "Nobody left!" });



I'm willing to release another module to CPAN: Guard::Stat.

It allows to create guard objects and gather overall usage statistics of those: how many are still alive, how many are gone etc.

It was initially created for tracking down callback subroutines usage in an AnyEvent application but can really deal with any kind of activities, like closures or plain objects.

The interface is simple: create guards, incorporate them into the tracked objects, get the stats (see synopsis).

Other features include:

  • If needed, time statistics can also be gathered through an external class (like Statistics::Descriptive::Sparse). Such class is only expected to provide add_data() method.

  • If needed, a on_level callback can be provided to do some action whenever running() counter goes above or below certain threshold. (E.g. defer incoming requests if load gets too high).

The module is already used in-house and is more or less tested.

My main concern is the name - is Guard::Stat a good one? Is it clear what the module does? Doesn't it occupy a sweet spot where another future module could fit much better? (Looks like "stat()" system call is not a resource one would build guard for).

If no objections follow, I'll probably release it around July, 10.


Please sign up to post a review.