1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Quick question on timers (with $5 bonus for best answer)

Discussion in 'PHP' started by Jeffr2014, Mar 29, 2015.

  1. #1
    Hi there,
    I was complaining about page load speed in another thread and @billzo recommended "You could start a timer in your Wordpress index.php file and stop the timer in your footer.php file and output the difference in an HTML comment at the bottom of your footer."

    I can probably look it up myself (how to start and stop timers in PHP and how to output value as comments) but I rather ask the experts...

    I will buy a coffee ($5 over PayPal) to the first person who posts working code here :)

    Thanks in advance.
     
    Solved! View solution.
    Jeffr2014, Mar 29, 2015 IP
  2. PDD

    PDD Greenhorn

    Messages:
    67
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #2
    Put this near the top of index.php:
    <?php
    $start_time = time();
    ?>
    Code (php):
    Put this near the end, preferably somewhere in the footer after all other php has been executed:
    
    <?php
    $end_time = time();
    echo 'Page generated in ' . ($end_time - $start_time) . 'ms';
    ?>
    
    Code (php):
     
    PDD, Mar 29, 2015 IP
  3. Jeffr2014

    Jeffr2014 Active Member

    Messages:
    254
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Thanks @PDD
    I tried your code but it looks like start_time value doesn't get carried over. The output is
    "Page generated in 1427664726ms"
    I tried inserting the first code fragment into index.php, header.php and the result is the same. When I insert it at the top of footer.php I get output
    "Page generated in 0ms"

    Any ideas? Signing off for today (Sunday:)) but will check tomorrow.
     
    Jeffr2014, Mar 29, 2015 IP
  4. PDD

    PDD Greenhorn

    Messages:
    67
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #4
    My bad, that would give seconds
    header:
    <?php
    $start_time = round(microtime(true) * 1000);
    ?>
    Code (php):
    footer:
    <?php
    $end_time = round(microtime(true) * 1000);
    echo 'Page generated in ' . ($end_time - $start_time) . 'ms';
    ?>
    Code (php):
     
    PDD, Mar 29, 2015 IP
  5. Jeffr2014

    Jeffr2014 Active Member

    Messages:
    254
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    55
    #5
    Still the same problem, it doesn't carry over start_time value, it's value is zero when the second code fragment gets evaluated. The output is "Page generated in 1427716739299ms", 1427716739299 is the current system time in milliseconds.
     
    Jeffr2014, Mar 30, 2015 IP
  6. #6
    Try making the variables global. In your index.php...

    
    global $timer_start;
    $timer_start = microtime();
    
    Code (markup):
    Then in your footer...

    
    global $timer_start;
    $timer_stop = microtime();
    
    $total_time = $timer_stop - $timer_start;
    
    echo 'Page generation time is ' . $total_time . ' seconds.';
    
    Code (markup):
    Something like that.

    http://php.net/manual/en/function.microtime.php
     
    Last edited: Mar 30, 2015
    billzo, Mar 30, 2015 IP
  7. Jeffr2014

    Jeffr2014 Active Member

    Messages:
    254
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    55
    #7
    @billzo , you are the man:) You get the best answer and please PM me for coffee. Thanks!
    I used your code (with argument true) and got this output: Page generation time is 0.4955940246582 seconds.
    When check page loading time in Chrome (using Dev Tools) I see 1.21 sec. Now I have to figure out why I get this extra 0.7sec, it proves my suspicions re network ...
     
    Jeffr2014, Mar 30, 2015 IP
  8. billzo

    billzo Well-Known Member

    Messages:
    961
    Likes Received:
    278
    Best Answers:
    15
    Trophy Points:
    113
    #8
    Run a tools.pingdom.com test on your website. Look at the output, in particular the "receive" time for images and other files. If you have a long "receive" time, that is, it is taking a long time for data to be transmitted, it is possible the network could be overloaded as is discussed in this post:

    https://forums.digitalpoint.com/threads/cloudflare-question.2750041/#post-19147127

    The page generation time you have may not necessarily be bad if you are running plugins and stuff. Even on my localhost XAMPP development server, the first load of Wordpress takes a long time.

    I don't know about nginx, but if you don't have enough servers started with Apache, it can result in a long "wait" time for file requests as there are not enough Apache processes to deal with the nearly parallel file requests.
     
    billzo, Mar 30, 2015 IP
  9. Jeffr2014

    Jeffr2014 Active Member

    Messages:
    254
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    55
    #9
    I tested with the page that has 700K image. The "receive" time for this image is 300ms when testing from NYC, 330ms from Dallas, and whopping 2.34sec from Melbourne Australia...

    I have Apache settings: StartServers = 5, MinSpareServers = 5, MaxSpareServers = 10, MaxRequestWorkers = 150
    I would think it's enough...? There is only a single website and email there...
     
    Jeffr2014, Mar 31, 2015 IP