2012-04-02 21 views
0

有没有一种方法可以计算在单个页面上运行的查询总数以及在Zend Framework中生成该页面需要多长时间?在Zend Framework中跟踪页面加载时间和总查询次数

我们希望能够记录这些信息,以便我们可以看到我们的瓶颈和需要大量资源生成的页面。

感谢,

+0

这个答案可能是你的兴趣,以及:http://stackoverflow.com/questions/9468922/zend-db-profiler-log-to-file/9471309#9471309它显示了一个' dispatchLoopShutdown'插件,它使用'Zend_Db_Profiler'来记录查询信息。我在'index.php'文件中添加'$ GLOBALS ['_ T0'] = microtime(true);'',这样我就可以从'time()'中减去它以获得总时间。我在我的应用程序中使用类似的代码来生成页脚中显示的查询和页面生成时间统计信息。在我的情况下,我使用'$ this-> getResponse() - > setBody()'来替换一个占位符与我生成的实际统计数据。 – drew010 2012-04-02 21:50:49

回答

3

确保有可用Db的性能分析工具,它是叫Zend_Db_Profiler,你可以了解如何通过阅读这里的官方文档中使用它:http://framework.zend.com/manual/en/zend.db.profiler.html#zend.db.profiler.using

基本上,你必须做这样的事情:

$db = Zend_Db_Table::getDefaultAdapter(); 
$profiler = $db->getProfiler(); 
$totalTime = $profiler->getTotalElapsedSecs(); 
$queryCount = $profiler->getTotalNumQueries(); 

至于你总页面加载时间,如果你不使用布局(S),那么它的最好创建一个控制器基类中predispatch和执行postDispatch方法获取当前(微)时,那么你做它是所有控制器的基类。也许这样的事情可以做的工作:

class Myapp_Controller_Action extends Zend_Controller_Action 
{ 
    protected $_startTime; 
    protected $_endTime; 
    protected $_totalTime; 

    public function preDispatch() 
    { 
    parent::preDispatch(); 
    $this->_startTime = microtime(true); 
    } 

    public function postDispatch() 
    { 
    parent::postDispatch(); 
    $this->_endTime = microtime(true); 
    $this->_totalTime = $this->_endTime - $this->_startTime; 
    // do something with $this->_totalTime; 
    } 
} 

而在你的应用程序中每个控制器应改为延长化Zend_Controller_Action的Myapp_Controller_Action:

class IndexController extends Myapp_Controller_Action 
{ 
    ... 
} 
0

要查看查询,可以使你的数据库适配器的探查。要做到这一点,你必须写下面一行是应为你的软件的开头:

$Db->getProfiler()->setEnabled(TRUE); 

当然$Db应该是你的数据库适配器的一个实例。要检索所有你能做的疑问:

$Db->getProfiler()->getQueryProfiles() 

当然你也可以阅读更多有关探查从官方文档。

0

您可以挂接插件的前后调度挂钩,并将时间戳记记录到数据库表中。在发布后,您可以查找相应的发货前邮票并计算总额。

对于查询,最简单的方法可能是在每次调用查询方法时向包装类中的查询添加一个包装器,并增加一个静态计数器。在post dispatch钩子中,你也可以将它记录到数据库中。

开发插件起诉前/后调度挂钩,看http://devzone.zend.com/1224/front-controller-plugins-in-zend-framework/

好运。

2

此外,这里是调试建议。当然,它不适用于伐木。 您可以使用Zend_Db_Profiler +萤火虫+ FirePhp与Firefox这是真正非常好的在一起: 的配置应该是(我使用的.ini格式,但可以通过代码或XML来实现):

database.params.profiler.enabled = true 
database.params.profiler.class = Zend_Db_Profiler_Firebug 

你”会得到这样的结果:在数据库中花费的总时间+查询细节和花费的时间(以及可选参数)。伟大的每一天使用工具!

enter image description here