Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • CChainedLogFilter
  • CDbLogRoute
  • CEmailLogRoute
  • CFileLogRoute
  • CLogFilter
  • CLogger
  • CLogRoute
  • CLogRouter
  • CProfileLogRoute
  • CSysLogRoute
  • CWebLogRoute

Interfaces

  • ILogFilter
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * CProfileLogRoute class file.
  4:  *
  5:  * @author Qiang Xue <qiang.xue@gmail.com>
  6:  * @link http://www.yiiframework.com/
  7:  * @copyright 2008-2013 Yii Software LLC
  8:  * @license http://www.yiiframework.com/license/
  9:  */
 10: 
 11: /**
 12:  * CProfileLogRoute displays the profiling results in Web page.
 13:  *
 14:  * The profiling is done by calling {@link YiiBase::beginProfile()} and {@link YiiBase::endProfile()},
 15:  * which marks the begin and end of a code block.
 16:  *
 17:  * CProfileLogRoute supports two types of report by setting the {@link setReport report} property:
 18:  * <ul>
 19:  * <li>summary: list the execution time of every marked code block</li>
 20:  * <li>callstack: list the mark code blocks in a hierarchical view reflecting their calling sequence.</li>
 21:  * </ul>
 22:  *
 23:  * @property string $report The type of the profiling report to display. Defaults to 'summary'.
 24:  *
 25:  * @author Qiang Xue <qiang.xue@gmail.com>
 26:  * @package system.logging
 27:  * @since 1.0
 28:  */
 29: class CProfileLogRoute extends CWebLogRoute
 30: {
 31:     /**
 32:      * @var boolean whether to aggregate results according to profiling tokens.
 33:      * If false, the results will be aggregated by categories.
 34:      * Defaults to true. Note that this property only affects the summary report
 35:      * that is enabled when {@link report} is 'summary'.
 36:      */
 37:     public $groupByToken=true;
 38:     /**
 39:      * @var string type of profiling report to display
 40:      */
 41:     private $_report='summary';
 42: 
 43:     /**
 44:      * Initializes the route.
 45:      * This method is invoked after the route is created by the route manager.
 46:      */
 47:     public function init()
 48:     {
 49:         $this->levels=CLogger::LEVEL_PROFILE;
 50:     }
 51: 
 52:     /**
 53:      * @return string the type of the profiling report to display. Defaults to 'summary'.
 54:      */
 55:     public function getReport()
 56:     {
 57:         return $this->_report;
 58:     }
 59: 
 60:     /**
 61:      * @param string $value the type of the profiling report to display. Valid values include 'summary' and 'callstack'.
 62:      * @throws CException if given value is not "summary" or "callstack"
 63:      */
 64:     public function setReport($value)
 65:     {
 66:         if($value==='summary' || $value==='callstack')
 67:             $this->_report=$value;
 68:         else
 69:             throw new CException(Yii::t('yii','CProfileLogRoute.report "{report}" is invalid. Valid values include "summary" and "callstack".',
 70:                 array('{report}'=>$value)));
 71:     }
 72: 
 73:     /**
 74:      * Displays the log messages.
 75:      * @param array $logs list of log messages
 76:      */
 77:     public function processLogs($logs)
 78:     {
 79:         $app=Yii::app();
 80:         if(!($app instanceof CWebApplication) || $app->getRequest()->getIsAjaxRequest())
 81:             return;
 82: 
 83:         if($this->getReport()==='summary')
 84:             $this->displaySummary($logs);
 85:         else
 86:             $this->displayCallstack($logs);
 87:     }
 88: 
 89:     /**
 90:      * Displays the callstack of the profiling procedures for display.
 91:      * @param array $logs list of logs
 92:      * @throws CException if Yii::beginProfile() and Yii::endProfile() are not matching
 93:      */
 94:     protected function displayCallstack($logs)
 95:     {
 96:         $stack=array();
 97:         $results=array();
 98:         $n=0;
 99:         foreach($logs as $log)
100:         {
101:             if($log[1]!==CLogger::LEVEL_PROFILE)
102:                 continue;
103:             $message=$log[0];
104:             if(!strncasecmp($message,'begin:',6))
105:             {
106:                 $log[0]=substr($message,6);
107:                 $log[4]=$n;
108:                 $stack[]=$log;
109:                 $n++;
110:             }
111:             elseif(!strncasecmp($message,'end:',4))
112:             {
113:                 $token=substr($message,4);
114:                 if(($last=array_pop($stack))!==null && $last[0]===$token)
115:                 {
116:                     $delta=$log[3]-$last[3];
117:                     $results[$last[4]]=array($token,$delta,count($stack));
118:                 }
119:                 else
120:                     throw new CException(Yii::t('yii','CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
121:                         array('{token}'=>$token)));
122:             }
123:         }
124:         // remaining entries should be closed here
125:         $now=microtime(true);
126:         while(($last=array_pop($stack))!==null)
127:             $results[$last[4]]=array($last[0],$now-$last[3],count($stack));
128:         ksort($results);
129:         $this->render('profile-callstack',$results);
130:     }
131: 
132:     /**
133:      * Displays the summary report of the profiling result.
134:      * @param array $logs list of logs
135:      * @throws CException if Yii::beginProfile() and Yii::endProfile() are not matching
136:      */
137:     protected function displaySummary($logs)
138:     {
139:         $stack=array();
140:         $results=array();
141:         foreach($logs as $log)
142:         {
143:             if($log[1]!==CLogger::LEVEL_PROFILE)
144:                 continue;
145:             $message=$log[0];
146:             if(!strncasecmp($message,'begin:',6))
147:             {
148:                 $log[0]=substr($message,6);
149:                 $stack[]=$log;
150:             }
151:             elseif(!strncasecmp($message,'end:',4))
152:             {
153:                 $token=substr($message,4);
154:                 if(($last=array_pop($stack))!==null && $last[0]===$token)
155:                 {
156:                     $delta=$log[3]-$last[3];
157:                     if(!$this->groupByToken)
158:                         $token=$log[2];
159:                     if(isset($results[$token]))
160:                         $results[$token]=$this->aggregateResult($results[$token],$delta);
161:                     else
162:                         $results[$token]=array($token,1,$delta,$delta,$delta);
163:                 }
164:                 else
165:                     throw new CException(Yii::t('yii','CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
166:                         array('{token}'=>$token)));
167:             }
168:         }
169: 
170:         $now=microtime(true);
171:         while(($last=array_pop($stack))!==null)
172:         {
173:             $delta=$now-$last[3];
174:             $token=$this->groupByToken ? $last[0] : $last[2];
175:             if(isset($results[$token]))
176:                 $results[$token]=$this->aggregateResult($results[$token],$delta);
177:             else
178:                 $results[$token]=array($token,1,$delta,$delta,$delta);
179:         }
180: 
181:         $entries=array_values($results);
182:         $func=create_function('$a,$b','return $a[4]<$b[4]?1:0;');
183:         usort($entries,$func);
184: 
185:         $this->render('profile-summary',$entries);
186:     }
187: 
188:     /**
189:      * Aggregates the report result.
190:      * @param array $result log result for this code block
191:      * @param float $delta time spent for this code block
192:      * @return array
193:      */
194:     protected function aggregateResult($result,$delta)
195:     {
196:         list($token,$calls,$min,$max,$total)=$result;
197:         if($delta<$min)
198:             $min=$delta;
199:         elseif($delta>$max)
200:             $max=$delta;
201:         $calls++;
202:         $total+=$delta;
203:         return array($token,$calls,$min,$max,$total);
204:     }
205: }
API documentation generated by ApiGen 2.8.0