| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage util | 
					
						
							|  |  |  |  * @since 2010-03-09 | 
					
						
							|  |  |  |  * @version SVN: $Id$ | 
					
						
							|  |  |  |  * @filesource $URL$ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Profiler | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     protected $start = null; | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     protected $end = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     protected $queries = array(); | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     static protected $instance = null; | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     private function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-04 17:30:05 +04:00
										 |  |  |         if (Config::get('PROFILER') == false) { | 
					
						
							|  |  |  |             throw new GeneralException('Need to turn on PROFILER before use.'); | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Refuse cloning | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  |     private function __clone() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Profiler | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     static public function getInstance() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!isset(self::$instance)) { | 
					
						
							|  |  |  |             self::$instance = new self(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return self::$instance; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  |      * @param string $type | 
					
						
							|  |  |  |      * @param string $command | 
					
						
							|  |  |  |      * @return CommandProfiler | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  |     public function profilerCommand($type, $command) | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  |         $profiler = new CommandProfiler($type, $command); | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |         $this->queries[] = $profiler; | 
					
						
							|  |  |  |         return $profiler; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     public function start() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->start = microtime(true); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     public function end($html) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->end = microtime(true); | 
					
						
							|  |  |  |         if (stripos($html, '</body>') == False) { | 
					
						
							|  |  |  |             return $html; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return str_ireplace('</body>', $this->getOutput() . '</body>', $html); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |     protected function getOutput() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  |         $temp = ''; | 
					
						
							|  |  |  |         $queriesTime = 0; | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |         foreach ($this->queries as $query) { | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  |             $temp .= '(' . $query->getType() . ') [' . round($query->getElapsed() * 1000, 2) . 'ms] ' . $query->getCommand() . '<br/>'; | 
					
						
							|  |  |  |             $queriesTime += $query->getElapsed(); | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  |         $html = '<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">' | 
					
						
							|  |  |  |                 . 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.<br/>' | 
					
						
							|  |  |  |                 . 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]<br/>'; | 
					
						
							|  |  |  |         $html .= $temp; | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  |         $html .= '</div>'; | 
					
						
							|  |  |  |         return $html; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-07-29 13:05:23 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function getJson() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->end = microtime(true); | 
					
						
							|  |  |  |         FB::info(round(($this->end - $this->start) * 1000, 2) . ' ms', 'Elapsed time'); | 
					
						
							|  |  |  |         $table = array(); | 
					
						
							|  |  |  |         $table[] = array('Type', 'Time (ms)', 'Query'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $queriesTime = 0; | 
					
						
							|  |  |  |         foreach ($this->queries as $query) { | 
					
						
							|  |  |  |             $table[] = array($query->getType(), round($query->getElapsed() * 1000, 2), $query->getCommand()); | 
					
						
							|  |  |  |             $queriesTime += $query->getElapsed(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-07 15:17:12 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function getCli() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->end = microtime(true); | 
					
						
							|  |  |  |         $queriesTime = 0; | 
					
						
							|  |  |  |         $temp = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT); | 
					
						
							|  |  |  |         foreach ($this->queries as $query) { | 
					
						
							|  |  |  |             $temp .= sprintf('%-25s[% 10sms]  %s', '(' . $query->getType() .')', round($query->getElapsed() * 1000, 2), $query->getCommand()) . PHP_EOL; | 
					
						
							|  |  |  |             $queriesTime += $query->getElapsed(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $html  = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT); | 
					
						
							|  |  |  |         $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL | 
					
						
							|  |  |  |                 . 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL; | 
					
						
							|  |  |  |         $html .= $temp; | 
					
						
							|  |  |  |         return $html; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-03-13 23:33:46 +00:00
										 |  |  | } |