Benchmarking PHP page load times

前端 未结 10 2079
借酒劲吻你
借酒劲吻你 2021-01-31 10:56

How could I measure the time taken to load a page (with various different PHP statements)?

Somewhat like the stats available here - http://talks.php.net/show/drupal08/24

相关标签:
10条回答
  • 2021-01-31 11:17

    Try https://github.com/fotuzlab/appgati

    It allows to define steps in the code and reports time, memory usage, server load etc between two steps.

    Something like:

        $appgati->Step('1');
    
        // Do some code ...
    
        $appgati->Step('2');
    
        $report = $appgati->Report('1', '2');
        print_r($report);
    

    Sample output array:

    Array
    (
        [Clock time in seconds] => 1.9502429962158
        [Time taken in User Mode in seconds] => 0.632039
        [Time taken in System Mode in seconds] => 0.024001
        [Total time taken in Kernel in seconds] => 0.65604
        [Memory limit in MB] => 128
        [Memory usage in MB] => 18.237907409668
        [Peak memory usage in MB] => 19.579357147217
        [Average server load in last minute] => 0.47
        [Maximum resident shared size in KB] => 44900
        [Integral shared memory size] => 0
        [Integral unshared data size] => 0
        [Integral unshared stack size] => 
        [Number of page reclaims] => 12102
        [Number of page faults] => 6
        [Number of block input operations] => 192
        [Number of block output operations] => 
        [Number of messages sent] => 0
        [Number of messages received] => 0
        [Number of signals received] => 0
        [Number of voluntary context switches] => 606
        [Number of involuntary context switches] => 99
    )
    
    0 讨论(0)
  • 2021-01-31 11:21

    You say you want to measure "Page load times" which is completely different from

    1. the time it takes to generate the page (as measured by an internal timer in your PHP code)

    2. and offload it from the server (which is measured by ab)

    A page load time should include the time taken to parse the HTML and to make subsequent requests to the server to fetch all related content (javascript files, css files, images etc).

    Measuring this is actually quite difficult. To do it properly, you need to push all the logic client side - drop a timestamped javascript cookie when the user clicks on a link or submits a form, then in the subsequent page, using the onload method (which fires after everything has loaded) compared this time with the current time. You then need a method of reporting this metric back to the server - you can use an Ajax request, or store the time in another cookie to be presented in the subsequent request.

    Note that the files required by each client will depend on the current state of the browser side cache.

    If you can isolate click streams from your logs, then you can get a good approximation by checking the interval between a request for a text/html content type and the last consecutive request for content type other than text/html. But your stats will get skewed if users interact usnig more than one browser window simultaeneously.

    0 讨论(0)
  • 2021-01-31 11:23

    Using microtime() PHP function you will know exactly how much time is needed for your PHP code to be executed. Follow the steps below to put the PHP code on your web page:

    Put the following code at the very top of your PHP page (if you measure the time needed for particular part of the code put this right before that PHP code part)

    <?php
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $start = $time;
    ?>
    

    The following code has to be put at the very end of the web page (or the end of the PHP code part)

    <?php
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;
    $total_time = round(($finish - $start), 4);
    echo 'Page generated in '.$total_time.' seconds.';
    

    If not works use microtime(true) instead of microtime()

    0 讨论(0)
  • 2021-01-31 11:25
    1. method one: use xdebug.
    2. method two: Put these statements all around your your scripts

      $TIMER['label']=microtime(1);
      /* some code */
      $TIMER['sql1_before']=microtime(1);
      a/* some code */
      $TIMER['sql1_after']=microtime(1);
      /* some code */

    and then output it, with code like this:

      echo "<table border=1><tr><td>name</td><td>so far</td><td>delta</td><td>per cent</td></tr>";
      reset($TIMER);
      $start=$prev=current($TIMER);
      $total=end($TIMER)-$start;
      foreach($TIMER as $name => $value) {
        $sofar=round($value-$start,3);
        $delta=round($value-$prev,3);
        $percent=round($delta/$total*100);
        echo "<tr><td>$name</td><td>$sofar</td><td>$delta</td><td>$percent</td></tr>";
        $prev=$value;
      }
        echo "</table>";
    

    Thus, you'll get tetailed report on how your code goes. this action called profiling and take most important place in the optimization process.

    0 讨论(0)
  • 2021-01-31 11:25

    The Output in that presentation seem to be copied from Siege (http://www.joedog.org/index/siege-home).

    Another quite useful tool for "real world" performance testing your whole application stack is Firebug (http://getfirebug.com/) and YSlow (http://developer.yahoo.com/yslow/)

    0 讨论(0)
  • 2021-01-31 11:37

    There are many ways to do this. I've personally been a fan of using microtime in the following way:

    // Start of code
    $time = microtime(true); // Gets microseconds
    
    // Rest of code
    
    // End of code
    echo "Time Elapsed: ".(microtime(true) - $time)."s";
    

    That will give you microsecond accuracy.

    If you are writing command-line scripts (like Facebook puzzles), you can use just time.

    time php dancebattle.php ~/input.dat
    Win
    
    real    0m0.152s
    user    0m0.142s
    sys     0m0.012s
    

    I forgot about the method of monitoring page load times (from a browser). You can use the NET tab from Firebug (for Firefox) to do just that. It will let you watch the various file loads (AJAX, JS, CSS, Images, etc.).

    0 讨论(0)
提交回复
热议问题