显示ZF2请求执行时间

问题描述:

我想在我的站点的每个页面(即ZF2)中打印请求执行时间。显示ZF2请求执行时间

我已经在index.php(这是请求访问的第一个文件)中定义了一个常量,其中包含请求初始值microtime(true)
现在,我应该在哪里获得最终请求microtime(true)

我的计划是有这样的事情:

$executionTime = ($finalTime - $initialTime)/1000000; // in seconds 
$executionTime = number_format($executionTime, 2); 
+0

在你的问题上的确切答案http://*.com/a/32646823/949273 – tasmaniski

+0

@tasmaniski这并没有阐明什么是我应该使用microtime的'末端'地方。 –

+0

结尾是index.php文件的结尾。因此,在文件末尾添加该行。 – tasmaniski

我的解决方案是根据this answer

我的每个视图都附有一个footer.phtml,所以如果我在那里打印它,就不需要更改很多文件。

要打印那里的时候,我首先写了一些具体的事情到footer.phtml,例如Execution time:

module/Application/Module.php我加入这个代码onBootstrap()

$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($e) { 
    $time = microtime(true) - REQUEST_MICROTIME; 

    // formatting time to be more friendly 
    if ($time <= 60) { 
     $timeF = number_format($time, 2, ',', '.').'s'; // conversion to seconds 

    } else { 
     $resto = fmod($time, 60); 
     $minuto = number_format($time/60, 0); 
     $timeF = sprintf('%dm%02ds', $minuto, $resto); // conversion to minutes and seconds 
    } 

    // Search static content and replace for execution time 
    $response = $e->getResponse(); 
    $response->setContent(str_replace(
     'Execution time:', 'Execution time: '.$timeF, $response->getContent())); 

}, 100000); 

为了达到你的要求,多一些,在开发环境中,你可以使用Zend Developer Tools模块,这些模块会自动给你关于一些信息你的申请。

在生产环境中,你可以改为听MvcEvent::EVENT_FINISH,这是Zend框架所发出的最后一个事件MVC

+0

我想在生产环境中做到这一点,以显示在每一页,作为一种向用户证明系统不慢的证据。 –

+1

好的,所以我的解决方案绝对不是你要找的。然后我建议你附加到MvcEvent :: EVENT_FINISH(http://framework.zend.com/manual/current/en/modules/zend.mvc.mvc-event.html#order-of-events) – marcosh

+0

真棒!如果你编辑你的答案,我会接受它,我也会赞成一个例子(: –

的问题是,你可能要将此信息添加到视图(因此用户可以看到它),这意味着在渲染视图之前添加时间(在MvcEvent::EVENT_RENDER之前)。问题是虽然视图在MvcEvent::EVENT_FINISH被触发之前呈现,但时间不准确。这将不容易解决...


你可以考虑在响应中添加一些时间相关的头。 Here an interesting related question about adding your custom headers

  • 有例如a $request_time variable for NGinx,你可以使用外的开箱

    $ REQUEST_TIME
    请求处理与毫秒的分辨率秒的时间(1.3。 9,1.2.6);由于第一字节经过时间是从所述客户端

    add_header X-Request-Time $request_time always; 
    
  • 读还有一个Age响应报头字段。你可以找到它here in the Header Field Definitions section 14.6 in RFC2616

    年龄响应头域传送 量的时间的发送者的估计,因为所述响应(或其再验证)是在原始服务器生成 。如果其年龄未超过其新鲜度寿命,则缓存的响应是“新鲜的”。年龄值为 ,按第13.2.3节的规定计算。

    也许你可以用它来计算在服务器上处理请求所用的时间。

你也可以通过添加一些代码到你的Module.php像这样在你的ZF2应用程序中添加自定义标题:

function onBootstrap(MvcEvent $event) { 
    $application = $event->getApplication(); 
    $eventManager = $application->getEventManager(); 
    $eventManager->attach(MvcEvent::EVENT_FINISH, [$this, 'addTimeHeader']); 

    //... 
} 

function addTimeHeader(MvcEvent $event) { 
    $time = // get your time value 
    $response = $event->getResponse(); 
    $response->getHeaders()->addHeaderLine('X-Request-Time', $time); 
} 

问题仍然会得到您的视图中该数据。如果您使用AJAX请求,将很容易从响应中获取标题,但如果您不使用AJAX,则是完全不同的故事。阅读更多here in this answer

+1

这真的很有价值的答案:) – tasmaniski

+0

@tasmaniski谢谢,它实际上也是一个有趣的问题:) – Wilt

+0

这真的是一个很有价值的答案@tasmaniski说,很好的信息,我没有标记它的唯一原因是因为我用了一些东西否则这有点不同,但谢谢你! –

如果你想成为一个真正的肮脏,在开始时添加在您的index.php文件:

$_GET['the_time'] = round(microtime(true) * 1000); 

无论你在视图或布局打印想:

echo round(microtime(true) * 1000) - $_GET['the_time']; 
// Use $_GET as global variable 

注:其余的开发者可能会讨厌你。

您可以捕获时间变量在index.php中,然后用register_shutdown_function打印时间差:

index.php

define('APP_START_TIME', microtime(true)); 

register_shutdown_function(function(){ 
echo 'Request time(sec): ' . number_format(microtime(true) - APP_START_TIME, 3); 
});