http://www.perlmonks.org?node_id=1037763

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

Greetings everyone,

I thought I knew exactly how to accomplish this task. But apparently I don't.
I tried to add a timer that would measure the time it took Perl to produce (process) rendering any of my web pages -- not how long the Client (browser) took to load the page, or the server to send it.

Because Perl is so great, and because my functions, and subs are so effecient; I chose Time::HiRes, knowing we'd be talking milliseconds. Here's what I'm using:

#!/usr/bin/perl -Tw # heavily commented for the sake of this post # presents time in my chosen format use POSIX qw/strftime/; # used for the Perl page rendered time I'm trying to produce use Time::HiRes qw/gettimeofday tv_interval/; # trickery to send the page as application/xhtml+xml, # if the client supports it if ($ENV{'HTTP_ACCEPT'} =~ /application\/xhtml\+xml/) { print "content-type:application/xhtml+xml; charset=utf-8\n\n"; } else { print "content-type:text/html; charset=utf-8\n\n"; } # here's where I start my timer my $start_time = [gettimeofday]; # just to add some of my local routines to my @INC use lib './localincs/'; # again, my preferred time format (has nothing to do with my timer) my $utc_time = strftime('%F %T %z',localtime); ...yadda,yadda,yadda... # bottom of web page, just before </body></html> my $total_time = tv_interval($start_time); # the following line should present something like this: # Page processed by Perl <my Perl version number> in 0.00001 seconds # But only produces the Perl version number printf('Page processed by Perl %s in %.02f seconds',$],$total_time);
Can anyone see why the "elapsed/process time" isn't being produced?
Currently, it shows up as
in 0.00 seconds



Thanks for taking the time.

--chris

#!/usr/bin/perl -Tw
use perl::always;
my $perl_version = "5.12.4";
print $perl_version;

Replies are listed 'Best First'.
Re: What's wrong with my Perl process timer?
by tinita (Parson) on Jun 07, 2013 at 22:57 UTC
    just an addition, because I'm doing something similar: If you are printing the content before measuring the time this can depend on the network connection. So if you are doing this also for statistical reasons and only want to measure how long it took to produce the content you could move the measuring before the print (if possible).
    I'm monitoring the request durations of my web application with a munin plugin and recognized the runtime difference between a local call on the server and through the network, if I include the print time.
Re: What's wrong with my Perl process timer? [SOLVED]
by taint (Chaplain) on Jun 07, 2013 at 22:20 UTC
    Greetings, and apologies,
    OK. I just figured out my own answer -- I overestimated the time it would take, see; use higher precision.
    So changing the last line from:
    printf('Page processed by Perl %s in %.02f seconds',$],$total_time);
    to
    printf('Page processed by Perl %s in %.6f seconds',$],$total_time);
    Note the %.02f to %.6f
    also worked with %.5f

    Sorry for the noise

    For the record, here it is without the other stuff
    just the interesting/required parts:
    #!/usr/bin/perl -Tw # at the top of your page(s) use Time::HiRes qw/gettimeofday tv_interval/; my $start_time = [gettimeofday]; # at the very bottom of your page(s) my $total_time = tv_interval($start_time); printf('Page processed by Perl %s in %.6f seconds',$],$total_time); # note: could also have used sprintf


    --chris

    #!/usr/bin/perl -Tw
    use perl::always;
    my $perl_version = "5.12.4";
    print $perl_version;