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

在网站的优化过程中,通常需要查看程序的运行时间,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" οnmοuseοver="this.style.opacity=1" οnmοuseοut="this.style.opacity=.1">';
        #$out = '<div style="opacity:.1" οnmοuseοver="this.style.opacity=1" οnmοuseοut="this.style.opacity=.1">';
        $out = "<a href=\"javascript:void(0)\" οnclick=\"$('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;
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值