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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4412575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:59:01  来源:igfitidea点击:

PHP Get Page Load Stats - How to measure php script execution / load time

phptimeloadpageload

提问by ATLChris

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.

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

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

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

采纳答案by John Gardner

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

microtime()microseconds返回当前的Unix 时间戳。我没有看到任何数学运算可以将微秒转换为秒。

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

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

回答by GhostInTheSecureShell

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.

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

回答by István Ujj-Mészáros

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

回答by Mizhar

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

回答by Sebastian

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

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

回答by Jeremy Muckel

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.

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

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

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

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

And in the footer:

在页脚中:

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

回答by SeriesBlue

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:

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

register_shutdown_function ( 'Your_function_name' );

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.

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

回答by profitphp

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');