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."

In reply to OO lifetime function stats mod, is this useful? by shotgunefx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.