Benchmarking PHP page load times

前端 未结 10 2027
借酒劲吻你
借酒劲吻你 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:37

    You can use microtime() at the start of processing and at end of output, then calculate the difference and convert it to seconds if wanted.

    This will only measure the execution time for PHP side, not the whole page load time at it seems to be in your link, but it will able you to compare various methods performance, for instance.

    0 讨论(0)
  • 2021-01-31 11:38
      $ time curl http://www.example.com/
    

    Note that it times the whole request, including network latency. But that may be want you want?

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

    The most simple too is Apache Bench (called ab), which is provided with Apache :

    • It's a command-line tool
    • That can send many requests, in parallel, to and URL
    • And reports timings, errors, ...

    Seems to fit the kind of very simple reporting that's presented on your slide.
    (It does actually report more than that)


    If your needs ar ea bit more complex, Siege can be a good alternative.

    An interesting thing with Siege is that it can take a list of URLs from a file, instead of working with just one.


    An interesting thing with those tools is that you are not measuring only the time taken to execute a specific portion of code (like you would if using microtime directly in your PHP code), but you're getting the whole time that was required to serve the page.

    Also, it can benchmark more than just PHP code, as it's working on the HTTP request, and not the code itself.

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

    Put the following code at the top of your PHP page.

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

    The following code has to be put at the end of your page.

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

    Note: If you measure the time for particular part of the code put this right start and last PHP code part.

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