Current file: htdocs/lib/Varien/Profiler.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
0.00%0.00%
0.00% 0 / 1
20.00%20.00%
20.00% 2 / 10 CRAP
8.51%8.51%
8.51% 8 / 94
 
Varien_Profiler
0.00%0.00%
0.00% 0 / 1
20.00%20.00%
20.00% 2 / 10
8.51%8.51%
8.51% 8 / 94
 enable()
0.00%0.00%
0.00% 0 / 1 2
0.00%0.00%
0.00% 0 / 3
 disable()
0.00%0.00%
0.00% 0 / 1 2
0.00%0.00%
0.00% 0 / 2
 reset($timerName)
0.00%0.00%
0.00% 0 / 1 2
0.00%0.00%
0.00% 0 / 7
 resume($timerName)
0.00%0.00%
0.00% 0 / 1 13.26
16.67%16.67%
16.67% 2 / 12
 start($timerName)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 2 / 2
 pause($timerName)
0.00%0.00%
0.00% 0 / 1 21.27
13.33%13.33%
13.33% 2 / 15
 stop($timerName)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 2 / 2
 fetch($timerName, $key='sum')
0.00%0.00%
0.00% 0 / 1 132
0.00%0.00%
0.00% 0 / 28
 getTimers()
0.00%0.00%
0.00% 0 / 1 2
0.00%0.00%
0.00% 0 / 1
 getSqlProfiler($res)
0.00%0.00%
0.00% 0 / 1 30
0.00%0.00%
0.00% 0 / 22


       1                 : <?php                                                                                                                       
       2                 : /**                                                                                                                         
       3                 :  * Magento                                                                                                                  
       4                 :  *                                                                                                                          
       5                 :  * NOTICE OF LICENSE                                                                                                        
       6                 :  *                                                                                                                          
       7                 :  * This source file is subject to the Open Software License (OSL 3.0)                                                       
       8                 :  * that is bundled with this package in the file LICENSE.txt.                                                               
       9                 :  * It is also available through the world-wide-web at this URL:                                                             
      10                 :  * http://opensource.org/licenses/osl-3.0.php                                                                               
      11                 :  * If you did not receive a copy of the license and are unable to                                                           
      12                 :  * obtain it through the world-wide-web, please send an email                                                               
      13                 :  * to license@magentocommerce.com so we can send you a copy immediately.                                                    
      14                 :  *                                                                                                                          
      15                 :  * DISCLAIMER                                                                                                               
      16                 :  *                                                                                                                          
      17                 :  * Do not edit or add to this file if you wish to upgrade Magento to newer                                                  
      18                 :  * versions in the future. If you wish to customize Magento for your                                                        
      19                 :  * needs please refer to http://www.magentocommerce.com for more information.                                               
      20                 :  *                                                                                                                          
      21                 :  * @category   Varien                                                                                                       
      22                 :  * @package    Varien_Profiler                                                                                              
      23                 :  * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)                                 
      24                 :  * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)                                  
      25                 :  */                                                                                                                         
      26                 :                                                                                                                             
      27                 :                                                                                                                             
      28                 : class Varien_Profiler                                                                                                       
      29                 : {                                                                                                                           
      30                 :                                                                                                                             
      31                 :     /**                                                                                                                     
      32                 :      * Timers for code profiling                                                                                            
      33                 :      *                                                                                                                      
      34                 :      * @var array                                                                                                           
      35                 :      */                                                                                                                     
      36                 :     static private $_timers = array();                                                                                      
      37                 :     static private $_enabled = false;                                                                                       
      38                 :     static private $_memory_get_usage = false;                                                                              
      39                 :                                                                                                                             
      40                 :     public static function enable()                                                                                         
      41                 :     {                                                                                                                       
      42               0 :         self::$_enabled = true;                                                                                             
      43               0 :         self::$_memory_get_usage = function_exists('memory_get_usage');                                                     
      44               0 :     }                                                                                                                       
      45                 :                                                                                                                             
      46                 :     public static function disable()                                                                                        
      47                 :     {                                                                                                                       
      48               0 :         self::$_enabled = false;                                                                                            
      49               0 :     }                                                                                                                       
      50                 :                                                                                                                             
      51                 :     public static function reset($timerName)                                                                                
      52                 :     {                                                                                                                       
      53               0 :         self::$_timers[$timerName] = array(                                                                                 
      54               0 :             'start'=>false,                                                                                                 
      55               0 :             'count'=>0,                                                                                                     
      56               0 :             'sum'=>0,                                                                                                       
      57               0 :             'realmem'=>0,                                                                                                   
      58               0 :             'emalloc'=>0,                                                                                                   
      59                 :         );                                                                                                                  
      60               0 :     }                                                                                                                       
      61                 :                                                                                                                             
      62                 :     public static function resume($timerName)                                                                               
      63                 :     {                                                                                                                       
      64              18 :         if (!self::$_enabled) {                                                                                             
      65              18 :             return;                                                                                                         
      66                 :         }                                                                                                                   
      67                 :                                                                                                                             
      68               0 :         if (empty(self::$_timers[$timerName])) {                                                                            
      69               0 :             self::reset($timerName);                                                                                        
      70               0 :         }                                                                                                                   
      71               0 :         if (self::$_memory_get_usage) {                                                                                     
      72               0 :             self::$_timers[$timerName]['realmem_start'] = memory_get_usage(true);                                           
      73               0 :             self::$_timers[$timerName]['emalloc_start'] = memory_get_usage();                                               
      74               0 :         }                                                                                                                   
      75               0 :         self::$_timers[$timerName]['start'] = microtime(true);                                                              
      76               0 :         self::$_timers[$timerName]['count'] ++;                                                                             
      77               0 :     }                                                                                                                       
      78                 :                                                                                                                             
      79                 :     public static function start($timerName)                                                                                
      80                 :     {                                                                                                                       
      81              18 :         self::resume($timerName);                                                                                           
      82              18 :     }                                                                                                                       
      83                 :                                                                                                                             
      84                 :     public static function pause($timerName)                                                                                
      85                 :     {                                                                                                                       
      86              18 :         if (!self::$_enabled) {                                                                                             
      87              18 :             return;                                                                                                         
      88                 :         }                                                                                                                   
      89                 :                                                                                                                             
      90               0 :         $time = microtime(true); // Get current time as quick as possible to make more accurate calculations                
      91                 :                                                                                                                             
      92               0 :         if (empty(self::$_timers[$timerName])) {                                                                            
      93               0 :             self::reset($timerName);                                                                                        
      94               0 :         }                                                                                                                   
      95               0 :         if (false!==self::$_timers[$timerName]['start']) {                                                                  
      96               0 :             self::$_timers[$timerName]['sum'] += $time-self::$_timers[$timerName]['start'];                                 
      97               0 :             self::$_timers[$timerName]['start'] = false;                                                                    
      98               0 :             if (self::$_memory_get_usage) {                                                                                 
      99               0 :                 self::$_timers[$timerName]['realmem'] += memory_get_usage(true)-self::$_timers[$timerName]['realmem_start'];
     100               0 :                 self::$_timers[$timerName]['emalloc'] += memory_get_usage()-self::$_timers[$timerName]['emalloc_start'];    
     101               0 :             }                                                                                                               
     102               0 :         }                                                                                                                   
     103               0 :     }                                                                                                                       
     104                 :                                                                                                                             
     105                 :     public static function stop($timerName)                                                                                 
     106                 :     {                                                                                                                       
     107              18 :         self::pause($timerName);                                                                                            
     108              18 :     }                                                                                                                       
     109                 :                                                                                                                             
     110                 :     public static function fetch($timerName, $key='sum')                                                                    
     111                 :     {                                                                                                                       
     112               0 :         if (empty(self::$_timers[$timerName])) {                                                                            
     113               0 :             return false;                                                                                                   
     114               0 :         } elseif (empty($key)) {                                                                                            
     115               0 :             return self::$_timers[$timerName];                                                                              
     116                 :         }                                                                                                                   
     117                 :         switch ($key) {                                                                                                     
     118               0 :             case 'sum':                                                                                                     
     119               0 :                 $sum = self::$_timers[$timerName]['sum'];                                                                   
     120               0 :                 if (self::$_timers[$timerName]['start']!==false) {                                                          
     121               0 :                     $sum += microtime(true)-self::$_timers[$timerName]['start'];                                            
     122               0 :                 }                                                                                                           
     123               0 :                 return $sum;                                                                                                
     124                 :                                                                                                                             
     125               0 :             case 'count':                                                                                                   
     126               0 :                 $count = self::$_timers[$timerName]['count'];                                                               
     127               0 :                 return $count;                                                                                              
     128                 :                                                                                                                             
     129               0 :             case 'realmem':                                                                                                 
     130               0 :                 if (!isset(self::$_timers[$timerName]['realmem'])) {                                                        
     131               0 :                     self::$_timers[$timerName]['realmem'] = -1;                                                             
     132               0 :                 }                                                                                                           
     133               0 :                 return self::$_timers[$timerName]['realmem'];                                                               
     134                 :                                                                                                                             
     135               0 :             case 'emalloc':                                                                                                 
     136               0 :                 if (!isset(self::$_timers[$timerName]['emalloc'])) {                                                        
     137               0 :                     self::$_timers[$timerName]['emalloc'] = -1;                                                             
     138               0 :                 }                                                                                                           
     139               0 :                 return self::$_timers[$timerName]['emalloc'];                                                               
     140                 :                                                                                                                             
     141               0 :             default:                                                                                                        
     142               0 :                 if (!empty(self::$_timers[$timerName][$key])) {                                                             
     143               0 :                     return self::$_timers[$timerName][$key];                                                                
     144                 :                 }                                                                                                           
     145               0 :         }                                                                                                                   
     146               0 :         return false;                                                                                                       
     147                 :     }                                                                                                                       
     148                 :                                                                                                                             
     149                 :     public static function getTimers()                                                                                      
     150                 :     {                                                                                                                       
     151               0 :         return self::$_timers;                                                                                              
     152                 :     }                                                                                                                       
     153                 :                                                                                                                             
     154                 :     /**                                                                                                                     
     155                 :      * Output SQl Zend_Db_Profiler                                                                                          
     156                 :      *                                                                                                                      
     157                 :      */                                                                                                                     
     158                 :     public static function getSqlProfiler($res) {                                                                           
     159               0 :         if(!$res){                                                                                                          
     160               0 :             return '';                                                                                                      
     161                 :         }                                                                                                                   
     162               0 :         $out = '';                                                                                                          
     163               0 :         $profiler = $res->getProfiler();                                                                                    
     164               0 :         if($profiler->getEnabled()) {                                                                                       
     165               0 :             $totalTime    = $profiler->getTotalElapsedSecs();                                                               
     166               0 :             $queryCount   = $profiler->getTotalNumQueries();                                                                
     167               0 :             $longestTime  = 0;                                                                                              
     168               0 :             $longestQuery = null;                                                                                           
     169                 :                                                                                                                             
     170               0 :             foreach ($profiler->getQueryProfiles() as $query) {                                                             
     171               0 :                 if ($query->getElapsedSecs() > $longestTime) {                                                              
     172               0 :                     $longestTime  = $query->getElapsedSecs();                                                               
     173               0 :                     $longestQuery = $query->getQuery();                                                                     
     174               0 :                 }                                                                                                           
     175               0 :             }                                                                                                               
     176                 :                                                                                                                             
     177               0 :             $out .= 'Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "<br>";                          
     178               0 :             $out .= 'Average query length: ' . $totalTime / $queryCount . ' seconds' . "<br>";                              
     179               0 :             $out .= 'Queries per second: ' . $queryCount / $totalTime . "<br>";                                             
     180               0 :             $out .= 'Longest query length: ' . $longestTime . "<br>";                                                       
     181               0 :             $out .= 'Longest query: <br>' . $longestQuery . "<hr>";                                                         
     182               0 :         }                                                                                                                   
     183               0 :         return $out;                                                                                                        
     184                 :     }                                                                                                                       

Generated by PHP_CodeCoverage 1.0.4 using PHP 5.3.4 and PHPUnit 3.5.13 at Tue Jul 5 9:07:16 UTC 2011.