PHP 获取页面加载统计信息 - 如何测量 php 脚本执行/加载时间

发布于 10-07 01:38 字数 445 浏览 8 评论 0原文

页眉中的内容:

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

页脚中的内容:

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

输出:在 1292008977.54 秒内生成页面。

有人可以帮我弄清楚为什么结果不正确吗?我正在使用PHP5。

What I have in the header:

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

What I have in the footer:

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

Output: Page generated in 1292008977.54 seconds.

Can someone please help me figure out why the result is not right?? I am using PHP5.

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

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

发布评论

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

评论(9

哑剧2024-10-14 01:38:32

看到这是谷歌的第一个结果,我想我应该分享我对这个问题的解决方案。将其放在页面顶部:

$startScriptTime=microtime(TRUE);

然后将此代码放在页面底部:

$endScriptTime=microtime(TRUE);
$totalScriptTime=$endScriptTime-$startScriptTime;
echo "\n\r".'<!-- Load time: '.number_format($totalScriptTime, 4).' seconds -->';

当您查看页面源代码时,您可以在 HTML 最后一行的注释中看到加载时间。

Seeing how this is the first result in Google I thought I'd share my solution to this problem. Put this at the top of your page:

$startScriptTime=microtime(TRUE);

And then put this code at the bottom of your page:

$endScriptTime=microtime(TRUE);
$totalScriptTime=$endScriptTime-$startScriptTime;
echo "\n\r".'<!-- Load time: '.number_format($totalScriptTime, 4).' seconds -->';

When you view source of a page you can see the load time in a comment on the last line of your HTML.

幼儿园老大2024-10-14 01:38:32

microtime() 返回当前 Unix 时间戳,单位为微秒。我没有看到任何从微秒转换为秒的数学。

microtime(true) 以浮点形式返回时间,以为单位

microtime() returns the current Unix timestamp with microseconds. i don't see any math there that does the conversion from microseconds to seconds.

microtime(true) returns the time as a float in seconds

金橙橙2024-10-14 01:38:32

您可以使用这个简单的函数来避免变量范围问题:

<?php

function timer()
{
    static $start;

    if (is_null($start))
    {
        $start = microtime(true);
    }
    else
    {
        $diff = round((microtime(true) - $start), 4);
        $start = null;
        return $diff;
    }
}

timer();

echo 'Page generated in ' . timer() . ' seconds.';

You can use this simple function to avoid the variable scope issue:

<?php

function timer()
{
    static $start;

    if (is_null($start))
    {
        $start = microtime(true);
    }
    else
    {
        $diff = round((microtime(true) - $start), 4);
        $start = null;
        return $diff;
    }
}

timer();

echo 'Page generated in ' . timer() . ' seconds.';
只等公子2024-10-14 01:38:32

页脚的一个衬里,它以float的形式返回

echo number_format(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"],4);

您不需要在脚本的开头放置任何内容,如>$_SERVER["REQUEST_TIME_FLOAT"] 在 PHP 脚本启动时会注意捕获这个时间...然后您只需要在 PHP 的最后计算已经过去了多少时间。

One liner for the footer, which returns seconds as float:

echo number_format(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"],4);

You do not need to put anything at the start of your script, as $_SERVER["REQUEST_TIME_FLOAT"] takes care to capture this time when the PHP script starts... Then you only need to calculate at the very end of the PHP how much time has passed.

等数载,海棠开2024-10-14 01:38:32

我喜欢用这样的东西。可以轻松地对多个代码块进行计时,而无需处理变量名称等。必须启用会话。

function code_timer ($name) {

    $mtime = explode(' ',microtime());
    $time = $mtime[1] + $mtime[0];

    //determine if we're starting the timer or ending it
    if ($_SESSION["timer-$name"]) {
      $stime=$_SESSION["timer-$name"];
      unset($_SESSION["timer-$name"]);
      return ($time - $stime);
    } else {
      $_SESSION["timer-$name"]=$time;
      return(true);  
    }
}

用法:

code_timer ('a');
//do stuff
echo "page generated in " . code_timer('a');

I like to use something like this. Makes it easy to time multiple code blocks without juggling variable names and such. sessions have to be enabled.

function code_timer ($name) {

    $mtime = explode(' ',microtime());
    $time = $mtime[1] + $mtime[0];

    //determine if we're starting the timer or ending it
    if ($_SESSION["timer-$name"]) {
      $stime=$_SESSION["timer-$name"];
      unset($_SESSION["timer-$name"]);
      return ($time - $stime);
    } else {
      $_SESSION["timer-$name"]=$time;
      return(true);  
    }
}

usage:

code_timer ('a');
//do stuff
echo "page generated in " . code_timer('a');
从来不烧饼2024-10-14 01:38:32
$page_loadtime_in_millisec = ($page_loadtime / 1000);
echo '<pre>Page Infor:
Page Load Time : ' . $page_loadtime.' <b>Microseconds</b><br/>
Page Load Time : ' . $page_loadtime_in_millisec.' <b>Milliseconds</b><br/>
Page Load Time : ' . number_format(($page_loadtime_in_millisec/1000),18) . ' <b>Seconds</b></pre>';
$page_loadtime_in_millisec = ($page_loadtime / 1000);
echo '<pre>Page Infor:
Page Load Time : ' . $page_loadtime.' <b>Microseconds</b><br/>
Page Load Time : ' . $page_loadtime_in_millisec.' <b>Milliseconds</b><br/>
Page Load Time : ' . number_format(($page_loadtime_in_millisec/1000),18) . ' <b>Seconds</b></pre>';
花期渐远2024-10-14 01:38:32

将其放在页眉中

<?php

$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];

?>

,并将其放在页脚中,

<html><center>Page generated in <?php $load = microtime();print (number_format($load,2));?> seconds. <?php
$loadtime = explode(' ', microtime()); $loadtime = $loadtime[0]+$loadtime[1]-$starttime; echo 'Peak memory usage: ',round(memory_get_peak_usage()/1048576, 2), 'MB';
?></center></html>

这将告诉您生成网站页面需要多长时间以及加载页面使用了多少内存

Put this in your Header

<?php

$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];

?>

and this in your footer

<html><center>Page generated in <?php $load = microtime();print (number_format($load,2));?> seconds. <?php
$loadtime = explode(' ', microtime()); $loadtime = $loadtime[0]+$loadtime[1]-$starttime; echo 'Peak memory usage: ',round(memory_get_peak_usage()/1048576, 2), 'MB';
?></center></html>

this will tell you how long it took your sites page to generate and how much Memory was used to load the page

等待圉鍢2024-10-14 01:38:32

问题在于变量范围。您在页眉中设置 $start 变量,但在页脚中,该变量将为空。所以 $total_time 只是当前时间 - 0,为您提供当前时间。

一个解决方案是使用 php 的 GLOBALS。在页眉中:

$GLOBALS['time_start'] = microtime(true);

在页脚中:

$total_time = round(($finish - $GLOBALS['time_start']), 4);

The issue is with variable scope. You set your $start variable in the header, but in footer, this variable will be empty. So $total_time would just be current time - 0, giving you the current time.

A solution is to use php's GLOBALS. In the header:

$GLOBALS['time_start'] = microtime(true);

And in the footer:

$total_time = round(($finish - $GLOBALS['time_start']), 4);
老旧海报2024-10-14 01:38:32

我要做的不是在每个页面中使用 microtime,而是将 microtime 插入到 $_REQUEST 中,然后从函数内的当前时间中减去该时间,并设置在脚本执行终止时调用该函数:

register_shutdown_function ( 'Your_function_name' );

我认为使用全局脚本很有用,该脚本将包含在整个应用程序中每个脚本/类的开头,它可以帮助我处理错误、管理会话等。 ..

将 microtime 添加到 $_REQUEST 将是该脚本中的第一行,您也可以在其中包含终止函数。

Instead of using microtime in every single page, what i would do is to insert microtime into $_REQUEST and then subtract that time from the current time inside a function and set that function to be called when the script execution is terminated using:

register_shutdown_function ( 'Your_function_name' );

I think that it is useful to use a global script which will be included at the beginning of every script / class throughout the application, it helps me in handling errors, managing sessions, etc...

Adding microtime to $_REQUEST would be the first line in that script and you can include your terminating function there too.

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