您的位置:首页 > 其它

magento优化-性能-程序运行时间监测

2013-01-25 11:27 447 查看
在网站的优化过程中,通常需要查看程序的运行时间,mangento为我们提供了查看的方法。

这里用到的magento核心类是Varien_Profiler

用Varien_Profiler来监测程序运行时间的使用方法如下:

Varien_Profiler::start('self::app::init');
self::$_app->init($code, $type, $options);//初始化APP
Varien_Profiler::stop('self::app::init');


start和stop函数的参数是监测这段程序的名字,名字可以随便起,只要保证全局唯一就ok了

这样在程序中写上代码后,监测的结果并不能直接显示,要显示结果需要有两个条件:

1、修改seystem/config/developer/调试/配置器 为true

2、在index.php中去掉下面这段程序的注释

Varien_Profiler::enable();//启用Varien_Profiler


当上面这些都配置好后,刷新网页会在网页的底部显示如下的一个表格:



这样的表格是如何显示出来的呢,这里用到一个Block类/app/code/core/Mage/Core/Block/Profiler.php

class Mage_Core_Block_Profiler extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
if (!$this->_beforeToHtml()
|| !Mage::getStoreConfig('dev/debug/profiler')
|| !Mage::helper('core')->isDevAllowed()) {
return '';
}

$timers = Varien_Profiler::getTimers();

#$out = '<div style="position:fixed;bottom:5px;right:5px;opacity:.1;background:white" onmouseover="this.style.opacity=1" onmouseout="this.style.opacity=.1">';
#$out = '<div style="opacity:.1" onmouseover="this.style.opacity=1" onmouseout="this.style.opacity=.1">';
$out = "<a href=\"javascript:void(0)\" onclick=\"$('profiler_section').style.display=$('profiler_section').style.display==''?'none':''\">[profiler]</a>";
$out .= '<div id="profiler_section" style="background:white; display:block">';
$out .= '<pre>Memory usage: real: '.memory_get_usage(true).', emalloc: '.memory_get_usage().'</pre>';
$out .= '<table border="1" cellspacing="0" cellpadding="2" style="width:auto">';
$out .= '<tr><th>Code Profiler</th><th>Time</th><th>Cnt</th><th>Emalloc</th><th>RealMem</th></tr>';
foreach ($timers as $name=>$timer) {
$sum = Varien_Profiler::fetch($name,'sum');
$count = Varien_Profiler::fetch($name,'count');
$realmem = Varien_Profiler::fetch($name,'realmem');
$emalloc = Varien_Profiler::fetch($name,'emalloc');
if ($sum<.0010 && $count<10 && $emalloc<10000) {
continue;
}
$out .= '<tr>'
.'<td align="left">'.$name.'</td>'
.'<td>'.number_format($sum,4).'</td>'
.'<td align="right">'.$count.'</td>'
.'<td align="right">'.number_format($emalloc).'</td>'
.'<td align="right">'.number_format($realmem).'</td>'
.'</tr>'
;
}
$out .= '</table>';
$out .= '<pre>';
$out .= print_r(Varien_Profiler::getSqlProfiler(Mage::getSingleton('core/resource')->getConnection('core_write')), 1);
$out .= '</pre>';
$out .= '</div>';

return $out;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: