PHP 页面加载时间基准测试

发布于 2024-08-25 06:51:37 字数 178 浏览 4 评论 0原文

如何测量加载页面(使用各种不同的 PHP 语句)所需的时间?

有点像这里提供的统计数据 - http://talks.php.net/show/drupal08/24< /a>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

司马昭之心 2024-09-01 06:51:37

有很多方法可以做到这一点。我个人非常喜欢按以下方式使用 microtime

// Start of code
$time = microtime(true); // Gets microseconds

// Rest of code

// End of code
echo "Time Elapsed: ".(microtime(true) - $time)."s";

这将为您提供微秒级的精度。

如果您正在编写命令行脚本(例如 Facebook 拼图),则可以只使用时间。

time php dancebattle.php ~/input.dat
Win

real    0m0.152s
user    0m0.142s
sys     0m0.012s

我忘记了监视页面加载时间(从浏览器)的方法。您可以使用 Firebug(适用于 Firefox)中的 NET 选项卡来完成此操作。它可以让您观看各种文件加载(AJAX、JS、CSS、图像等)。

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.).

下雨或天晴 2024-09-01 06:51:37

最简单的也是 Apache Bench (称为 ab),由 Apache 提供:

  • 它是一个命令行工具
  • ,可以并行发送许多请求到 URL
  • 并报告计时、错误……

似乎适合这种类型幻灯片上呈现的非常简单的报告。

(它实际上报告的内容远不止这些)

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

Siege 的一个有趣的事情是它可以从文件中获取 URL 列表,而不是只使用一个。

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`][3] directly in your PHP code)*, but you're getting the whole time that was required to serve the page.

此外,它不仅可以对 PHP 代码进行基准测试,因为它处理的是 HTTP 请求,而不是代码本身。

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][2] 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`][3] 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.

夜还是长夜 2024-09-01 06:51:37
  $ time curl http://www.example.com/

请注意,它对整个请求进行计时,包括网络延迟。但这可能就是你想要的?

  $ time curl http://www.example.com/

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

如若梦似彩虹 2024-09-01 06:51:37

尝试 https://github.com/fotuzlab/appgati

它允许定义代码中的步骤并报告时间、两个步骤之间的内存使用情况、服务器负载等。

类似于:

    $appgati->Step('1');

    // Do some code ...

    $appgati->Step('2');

    $report = $appgati->Report('1', '2');
    print_r($report);

示例输出数组:

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
)

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
)
指尖上的星空 2024-09-01 06:51:37

您可以在处理开始和结束时使用 microtime()输出,然后计算差异并将其转换为秒(如果需要)。

这只会测量 PHP 端的执行时间,而不是链接中似乎的整个页面加载时间,但它可以让您比较各种方法的性能,例如。

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.

薄荷→糖丶微凉 2024-09-01 06:51:37

您说您想要测量“页面加载时间”,它完全不同于

  1. 生成页面所需的时间(由 PHP 代码中的内部计时器测量)

  2. 从服务器卸载它(通过 ab 测量)

页面加载时间应包括解析 HTML 以及向服务器发出后续请求以获取所有相关内容(javascript 文件、css 文件、图像等)所需的时间。

衡量这个实际上是相当困难的。为了正确执行此操作,您需要推送所有逻辑客户端 - 当用户单击链接或提交表单时放置带时间戳的 javascript cookie,然后在后续页面中使用 onload 方法(该方法在所有内容加载后触发)将此时与当前时间进行比较。然后,您需要一种将此指标报告回服务器的方法 - 您可以使用 Ajax 请求,或将时间存储在另一个 cookie 中,以便在后续请求中显示。

请注意,每个客户端所需的文件将取决于浏览器端缓存的当前状态。

如果您可以将点击流与日志隔离,那么您可以通过检查 text/html 内容类型的请求与除 text/html 以外的内容类型的最后一个连续请求之间的间隔来获得良好的近似值。但是,如果用户同时使用多个浏览器窗口进行交互,您的统计数据就会出现偏差。

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.

寄意 2024-09-01 06:51:37
  1. 方法一:使用xdebug。
  2. 方法二:
    将这些语句放在您的脚本中

    $TIMER['label']=microtime(1);
    /* 一些代码 */
    $TIMER['sql1_before']=microtime(1);
    a/* 一些代码 */
    $TIMER['sql1_after']=microtime(1);
    /* 一些代码 */

然后输出它,代码如下:

  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>";

因此,您将获得有关代码运行情况的详细报告。
此操作称为分析,在优化过程中占据最重要的位置。

  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.

白龙吟 2024-09-01 06:51:37

该演示文稿中的输出似乎是从 Siege 复制的 (http://www.joedog.org/索引/siege-home)。

另一个用于“现实世界”性能测试整个应用程序堆栈的非常有用的工具是 Firebug (http://getfirebug.com/) 和 YSlow (http://developer.yahoo.com/yslow/)

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/)

最美不过初阳 2024-09-01 06:51:37

使用 microtime() PHP 函数,您将确切地知道执行 PHP 代码需要多少时间。 步骤将 PHP 代码放在您的网页上:

将以下代码放在 PHP 页面的最顶部(如果您测量代码特定部分所需的时间,请将其放在该 PHP 代码部分之前)

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

按照以下 代码必须放在网页的最末尾(或 PHP 代码部分的末尾)

<?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.';

如果不起作用,请使用 microtime(true) 而不是 microtime()

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()

虚拟世界 2024-09-01 06:51:37

将以下代码放在 PHP 页面的顶部。

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

以下代码必须放在页面的末尾。

<?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.';
?>

注意:如果您测量代码特定部分的时间,请将其放在正确的开始和最后 PHP 代码部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文