shotgunefx has asked for the wisdom of the Perl Monks concerning the following question:

While on a tangent, I've made a OO module that takes a coderef, and some other properties.

The point being you can "execute" the object and keep some statistics over the lifetime of the function. Such as how many times you called it, how often it succeded, how long it takes to execute, etc.

It also will keep track of the stats for the last N specified calls. So you have another set of stats. The idea being you might be concerned more with the current "going ons". The main use for me would be in monitoring resources over time. As I write this, I realize I don't keep the last exec stats so I'll add that too. I'll post the code in a bit.

My questions are...
Here's a little example.
use Net::Ping; use vars qw($STATUSMSG); my @ips = qw (255.255.255.x 255.255.255.x 255.255.255.x 255.255.255.x +); sub pinger { my ($host,$timeout) = @_; $timeout = 5 unless $timeout; my $p = Net::Ping->new("icmp"); my $res = $p->ping($host,$timeout); $p->close(); return $res; } foreach my $ip (@ips) { $ip = Function::Stats->new( label => $ip , # The name of the inst +ance latest => 10, # Keep track of the l +ast 10 calls. coderef => sub { pinger($ip,3) }, # The code t +o execute # boolean basically lets you preprocess the return + from coderef. boolean => sub { my $r = shift; $STATUSMSG = $r ? "$ip is up" : "$ip +is down!"; $r; } ); } # Yes infinite loops may be hazardous to your health. I know... while (1){ foreach my $ip (@ips){ print "Host: $ip\tInterval:",$ip->iterations,"\n"; # Print +the name and current iteration my $res = $ip->execute; # Call the function + print $STATUSMSG,"\n"; # Print the status if ($ip->current_avg_failed > 0.49){ # Connection g +etting bad. print "\tConnection lacking for $ip... ",$ip->current_avg_ +failed,"% failure\n"; print $ip->dumpstats("\t"); } if ($ip->current_avg_runtime > 3){ print "Connection slow for $ip... ",$ip->current_avg_runti +me," seconds\n"; } print "-" x 30 ,"\n" ; sleep(15); } } __END__ # Sample output of dumpstats ------------------------------ Summary 255.255.255.255 Succeded: 72 Failed: 6 Iterations: 78 ------------------------------ Current Runtime Avg%: 0.861059010028839 seconds Current Success Avg%: 0.7000 Current Failure Avg%: 0.3000 ------------------------------ Overall Runtime Avg%: 0.226245646293347 seconds Overall Success Avg%: 0.9231 Overall Failure Avg%: 0.0769


-Lee

"To be civilized is to deny one's nature."

Replies are listed 'Best First'.
Re: OO lifetime function stats mod, is this useful?
by Aristotle (Chancellor) on Jun 28, 2002 at 14:59 UTC
    The way you play with coderefs and for loops is vaguely reminiscent of map.. I'm not sure what I want to get at, since I don't have a deep CS theory background, but my intuition tells me you're doing something that could be generalized better in the functional programming paradigma.

    Makeshifts last the longest.

      The for loops were just for example, it could easily be one function you want to track. The coderefs are needed because I'm basically trying to wrap a function call with args and track it over time (actually intervals. Not neccassarily the same thing.) The coderef provides a generic hook for performing an action. Now some functions might be builtins or return "0 but true" so the boolean is a generic way to let you defined the truth test. It is an optional argument.

      I could see several uses. You could use it to track and monitor the performance of a different aspects of a server (HTTP Requests,IO latency, memory usage, etc). Mainly what it's for.

      -Lee

      "To be civilized is to deny one's nature."
        Then I think what you really want to do is to provide a generic statskeeper hook to use with Hook::WrapSub, as that will allow one to directly call the initial function, rather than creating an object that wraps a coderef and then calling execute() on it.

        Makeshifts last the longest.

Re: OO lifetime function stats code
by shotgunefx (Parson) on Jun 28, 2002 at 16:20 UTC
    Code posted HERE.

    -Lee

    "To be civilized is to deny one's nature."