Hello All...


I have a simple script that calculates the average values from a number of RRD files. I would now like to discard all data returned by RRDs::fetch that does not fall within working hours.


So, after some reading - and some trial and error - the following works:
use strict; use warnings; use DateTime::Event::Recurrence; use DateTime::SpanSet; use RRDs; # # Set up paths, config data etc... # foreach $file ( @array_of_RRD_filenames ) { my ( $metric, $interval, $begin, $end ) = ( 'AVERAGE', '1800', '-800h', 'now' ); my $start_span = DateTime::Event::Recurrence->weekly( days => [ 1 .. 5 ], hours = +> 8 ); my $end_span = DateTime::Event::Recurrence->weekly( days => [ 1 .. 5 ], hours = +> 18 ); my $span_set = DateTime::SpanSet->from_sets( start_set => $start_s +pan, end_set => $end_spa +n ); push @opts, $file; push @opts, $metric; push @opts, "-r $interval"; push @opts, "-s $begin"; push @opts, "-e $end"; my ( $start, $step, $names, $data ) = RRDs::fetch @opts;
For those unfamiliar with RRDs.pm, $data is an AoA ref.
my ( $intotal, $outtotal, $i ); foreach my $value ( @{ $data } ) { $start += $step; my ( $minute, $hour, $day, $month, $year ) = ( localtime( $start ) )[ 1, 2, 3, 4, 5 ]; $month += 1; # Why does localtime give month -1? my $dt = DateTime->new( year => $year, month => $month, day => $day, hour => $hour, minute => $minute ); next unless $span_set->contains( $dt ); $i++; $in_total += $value->[ 0 ]; $out_total += $value->[ 1 ]; } # Now go off and work out averages / percentages etc... # Build a HoH and exit the for loop. } # Output some purty HTML.

The problem is, this is incredibly slow! I've qualitatively isolated the major slowdown to the "next unless $span_set->contains ( $dt )" -- this slows things down by a factor of 5.

dprofpp just segfaults when I try to profile the script.

Anyone have any suggestions as to how to optimise this? Is there another way rather than using Date::Time?


Many thanks in advance for any pointers / help / critisicism / suggestions.

Cheers

SM


In reply to Questions on Optimisation (DateTime::SpanSet and RRDs.pm) by smullis

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.