| @@ -10,9 +10,9 @@ | ||
| 10 | 10 | | (c) Jerome Bruandet ~ https://code-profiler.com/ | |
| 11 | 11 | +=====================================================================+ |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); } | |
| 14 | +if (! defined('ABSPATH') ) { die('Forbidden'); } | |
| 15 | 15 | |
| 16 | 16 | // ===================================================================== |
| 17 | 17 | |
| 18 | 18 | class CodeProfiler_Profiler { |
| @@ -19,15 +19,14 @@ | ||
| 19 | 19 | |
| 20 | 20 | static $tick_list; |
| 21 | 21 | static $buffer = 10000000; |
| 22 | 22 | static $metrics; |
| 23 | - | |
| 24 | 23 | private $tmp_iostats; |
| 25 | 24 | private $tmp_summary; |
| 26 | 25 | private $tmp_diskio; |
| 27 | 26 | private $tmp_ticks; |
| 28 | 27 | |
| 29 | - /* | |
| 28 | + /** | |
| 30 | 29 | * Initialize |
| 31 | 30 | */ |
| 32 | 31 | public function __construct() { |
| 33 | 32 | |
| @@ -47,9 +46,9 @@ | ||
| 47 | 46 | if ( file_exists( $this->tmp_ticks ) ) { |
| 48 | 47 | unlink( $this->tmp_ticks ); |
| 49 | 48 | } |
| 50 | 49 | |
| 51 | - if ( version_compare( PHP_VERSION, '7.3', '<' ) ) { | |
| 50 | + if ( version_compare( PHP_VERSION, '7.3', '<') ) { | |
| 52 | 51 | self::$metrics = 'microtime'; |
| 53 | 52 | } else { |
| 54 | 53 | self::$metrics = 'hrtime'; |
| 55 | 54 | } |
| @@ -61,16 +60,16 @@ | ||
| 61 | 60 | define('CODE_PROFILER_TICKS', (int) $cp_options['accuracy'] ); |
| 62 | 61 | } |
| 63 | 62 | define('CODE_PROFILER_LENGTH', 17 + strlen( (string) CODE_PROFILER_TICKS ) ); |
| 64 | 63 | |
| 65 | - register_shutdown_function( array( $this, 'code_profiler_shutdown' ) ); | |
| 64 | + register_shutdown_function( array( $this, 'code_profiler_shutdown') ); | |
| 66 | 65 | |
| 67 | 66 | require 'class-stream.php'; |
| 68 | 67 | CodeProfiler_Stream::start(); |
| 69 | - register_tick_function( array( $this, 'code_profiler_tick_handler' ) ); | |
| 68 | + register_tick_function( array( $this, 'code_profiler_tick_handler') ); | |
| 70 | 69 | } |
| 71 | 70 | |
| 72 | - /* | |
| 71 | + /** | |
| 73 | 72 | * Save data and check for potential PHP errors. |
| 74 | 73 | */ |
| 75 | 74 | public function code_profiler_shutdown() { |
| 76 | 75 | |
| @@ -107,21 +106,16 @@ | ||
| 107 | 106 | $res = file_put_contents( |
| 108 | 107 | $this->tmp_diskio, "read\t". CodeProfiler_Stream::$io_read ."\nwrite\t". |
| 109 | 108 | CodeProfiler_Stream::$io_write |
| 110 | 109 | ); |
| 111 | - | |
| 112 | 110 | } |
| 113 | 111 | |
| 114 | - /* | |
| 112 | + /** | |
| 115 | 113 | * Our tick handler. |
| 116 | 114 | */ |
| 117 | 115 | public function code_profiler_tick_handler() { |
| 118 | 116 | |
| 119 | - if ( self::$metrics == 'hrtime' ) { | |
| 120 | - $start = hrtime( true ); | |
| 121 | - } else { | |
| 122 | - $start = microtime( true ); | |
| 123 | - } | |
| 117 | + $start = $this->time(); | |
| 124 | 118 | |
| 125 | 119 | $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ); |
| 126 | 120 | if ( isset( $backtrace[1] ) ) { |
| 127 | 121 | $el = $backtrace[1]; |
| @@ -129,9 +123,13 @@ | ||
| 129 | 123 | $el = $backtrace[0]; |
| 130 | 124 | } |
| 131 | 125 | |
| 132 | 126 | if ( empty( $el['file'] ) ) { |
| 133 | - return; | |
| 127 | + if ( isset( $backtrace[0]['file'] ) ) { | |
| 128 | + $el['file'] = $backtrace[0]['file']; | |
| 129 | + } else { | |
| 130 | + return; | |
| 131 | + } | |
| 134 | 132 | } |
| 135 | 133 | |
| 136 | 134 | self::$tick_list .= "{$el['file']}\t"; |
| 137 | 135 | if ( isset( $backtrace[0]['file'][0] ) ) { |
| @@ -139,20 +137,34 @@ | ||
| 139 | 137 | } else { |
| 140 | 138 | self::$tick_list .= '-'; |
| 141 | 139 | } |
| 142 | 140 | |
| 143 | - if ( self::$metrics == 'hrtime' ) { | |
| 144 | - self::$tick_list .="\t{$start}\t". hrtime(true) ."\n"; | |
| 145 | - } else { | |
| 146 | - self::$tick_list .="\t{$start}\t". microtime(true) ."\n"; | |
| 147 | - } | |
| 148 | - | |
| 149 | 141 | // Buffer can grow *very* big hence we flush it every 10MB by default |
| 150 | 142 | if ( strlen( self::$tick_list ) > self::$buffer ) { |
| 151 | 143 | CodeProfiler_Stream::stop(); |
| 152 | - file_put_contents( $this->tmp_ticks, self::$tick_list, FILE_APPEND ); | |
| 144 | + file_put_contents( | |
| 145 | + $this->tmp_ticks, | |
| 146 | + self::$tick_list ."\t{$start}\t". $this->time() ."\n", | |
| 147 | + FILE_APPEND | |
| 148 | + ); | |
| 153 | 149 | CodeProfiler_Stream::start(); |
| 154 | 150 | self::$tick_list = ''; |
| 151 | + | |
| 152 | + } else { | |
| 153 | + self::$tick_list .="\t{$start}\t". $this->time() ."\n"; | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Return time | |
| 160 | + */ | |
| 161 | + private function time() { | |
| 162 | + | |
| 163 | + if ( self::$metrics == 'hrtime') { | |
| 164 | + return hrtime( true ); | |
| 165 | + } else { | |
| 166 | + return microtime( true ); | |
| 155 | 167 | } |
| 156 | 168 | } |
| 157 | 169 | } |
| 158 | 170 | |