Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

What's wrong with my Perl process timer?

by taint (Chaplain)
on Jun 07, 2013 at 21:41 UTC ( #1037763=perlquestion: print w/replies, xml ) Need Help??
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.


#!/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);
    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


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037763]
Approved by tinita
[tye]: Corion, I used to have my "last here" updated when I chatted. I still visit regularly but I rarely chat. So I modified my cookie today.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-04-28 14:56 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (524 votes). Check out past polls.