PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.9.6
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.9.6
1.9.6 1.9.5 1.9.4 1.9.3 trunk 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 All 41 releases
← All changes | lib/class-profiler.php +77 -21 1.6.31.9.6 View file →
@@ -6,9 +6,9 @@
6 6 | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| |
7 7 | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | |
8 8 | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| |
9 9 | |
10 - | (c) Jerome Bruandet ~ https://code-profiler.com/ |
10 + | (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ |
11 11 +=====================================================================+
12 12 */
13 13
14 14 if (! defined('ABSPATH') ) { die('Forbidden'); }
@@ -19,14 +19,18 @@
19 19
20 20 static $tick_list;
21 21 static $buffer;
22 22 static $metrics;
23 + static $fh;
23 24 static $connections_list = [];
24 25 static $connections_start;
26 + static $exclusions = [];
25 27 private $tmp_iostats;
26 28 private $tmp_summary;
27 29 private $tmp_diskio;
28 - private $tmp_ticks;
30 + private $tmp_calls;
31 + private $tmp_connections;
32 + private $tmp_rerun;
29 33
30 34 /**
31 35 * Initialize
32 36 */
@@ -37,21 +41,17 @@
37 41 $this->tmp_summary = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
38 42 CODE_PROFILER_TMP_SUMMARY_LOG;
39 43 $this->tmp_iostats = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
40 44 CODE_PROFILER_TMP_IOSTATS_LOG;
41 - $this->tmp_ticks = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
42 - CODE_PROFILER_TMP_TICKS_LOG;
45 + $this->tmp_calls = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
46 + CODE_PROFILER_TMP_CALLS_LOG;
43 47 $this->tmp_diskio = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
44 48 CODE_PROFILER_TMP_DISKIO_LOG;
45 49 $this->tmp_connections = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
46 50 CODE_PROFILER_TMP_CONNECTIONS_LOG;
51 + $this->tmp_rerun = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
52 + CODE_PROFILER_TMP_RERUN_LOG;
47 53
48 - // Clear the temporary log because the buffer will be written
49 - // with the FILE_APPEND flag
50 - if ( file_exists( $this->tmp_ticks ) ) {
51 - unlink( $this->tmp_ticks );
52 - }
53 -
54 54 if ( function_exists('hrtime') ) {
55 55 // PHP >=7.3
56 56 self::$metrics = 'hrtime';
57 57 } else {
@@ -57,8 +57,45 @@
57 57 } else {
58 58 self::$metrics = 'microtime';
59 59 }
60 60
61 + // Select theme (stylesheet::template)
62 + if (! empty( $_SERVER['HTTP_THEME'] ) ) {
63 +
64 + $theme = explode('::', $_SERVER['HTTP_THEME'] );
65 + if (! empty( $theme[1] ) ) {
66 + define('CODE_PROFILER_STYLESHEET', sanitize_file_name( $theme[0] ) );
67 + define('CODE_PROFILER_TEMPLATE', sanitize_file_name( $theme[1] ) );
68 +
69 + if ( is_file( WP_CONTENT_DIR .'/themes/'. CODE_PROFILER_STYLESHEET .'/style.css' ) ) {
70 +
71 + add_filter('pre_option_template', function () {
72 + return CODE_PROFILER_TEMPLATE;
73 + }, 1000 );
74 +
75 + add_filter('pre_option_stylesheet', function () {
76 + return CODE_PROFILER_STYLESHEET;
77 + }, 1000 );
78 +
79 + code_profiler_log_debug(
80 + sprintf(
81 + // We cannot load translation here.
82 + 'Setting theme to %s',
83 + CODE_PROFILER_STYLESHEET
84 + )
85 + );
86 + } else {
87 + code_profiler_log_error(
88 + sprintf(
89 + // We cannot load translation here.
90 + 'Unable to switch theme, %s not found',
91 + CODE_PROFILER_STYLESHEET
92 + )
93 + );
94 + }
95 + }
96 + }
97 +
61 98 $cp_options = get_option('code-profiler');
62 99 if ( empty( $cp_options['accuracy'] ) ) {
63 100 define('CODE_PROFILER_TICKS', 1 );
64 101 } else {
@@ -75,20 +112,34 @@
75 112 self::$buffer = $cp_options['buffer'] * 1000000;
76 113 }
77 114 code_profiler_log_debug(
78 115 sprintf(
79 - esc_html__('Setting size of memory buffer to %sMB', 'code-profiler'),
116 + // We cannot load translation here.
117 + 'Setting size of memory buffer to %sMB',
80 118 self::$buffer / 1000000
81 119 )
82 120 );
83 121
122 + // File & folder exclusions
123 + if ( isset( $cp_options['exclusions'] ) ) {
124 + self::$exclusions = json_decode( $cp_options['exclusions'] );
125 + }
126 +
84 127 add_filter('pre_http_request', [ $this, 'pre_http_request'], 10000, 3 );
85 128 add_action('http_api_debug', [ $this, 'http_api_debug'], 10000, 5 );
86 129 register_shutdown_function( [ $this, 'code_profiler_shutdown'] );
87 130 code_profiler_log_debug(
88 - esc_html__('Starting profiler', 'code-profiler')
131 + // We cannot load translation here.
132 + 'Starting profiler'
89 133 );
90 134 require 'class-stream.php';
135 +
136 + // Clear the temporary log because the buffer will be appended to it
137 + if ( file_exists( $this->tmp_calls ) ) {
138 + unlink( $this->tmp_calls );
139 + }
140 + self::$fh = fopen( $this->tmp_calls, 'wb');
141 +
91 142 CodeProfiler_Stream::start();
92 143 register_tick_function( array( $this, 'code_profiler_tick_handler') );
93 144 }
94 145
@@ -98,10 +149,19 @@
98 149 public function code_profiler_shutdown() {
99 150
100 151 CodeProfiler_Stream::stop();
101 152
102 - $summary['memory'] = memory_get_peak_usage();
103 - $summary['queries'] = get_num_queries() - 2;
153 + fclose( self::$fh );
154 +
155 + // Summary file (metadata)
156 + $summary = [];
157 + if ( is_file( $this->tmp_rerun ) ) {
158 + $summary['rerun'] = json_decode( file_get_contents( $this->tmp_rerun ), true );
159 + unlink( $this->tmp_rerun );
160 + }
161 + $summary['memory'] = memory_get_peak_usage();
162 + $summary['queries'] = get_num_queries() - 2;
163 + $summary['precision'] = CODE_PROFILER_TICKS;
104 164 file_put_contents( $this->tmp_summary, json_encode( $summary ) );
105 165
106 166 // Catch potential error
107 167 $e = error_get_last();
@@ -121,11 +181,11 @@
121 181 if ( $res === false ) {
122 182 $msg = sprintf( $err_msg, $this->tmp_iostats );
123 183 code_profiler_log_error( $msg );
124 184 }
125 - $res = file_put_contents( $this->tmp_ticks, self::$tick_list, FILE_APPEND );
185 + $res = file_put_contents( $this->tmp_calls, self::$tick_list, FILE_APPEND );
126 186 if ( $res === false ) {
127 - $msg = sprintf( $err_msg, $this->tmp_ticks );
187 + $msg = sprintf( $err_msg, $this->tmp_calls );
128 188 code_profiler_log_error( $msg );
129 189 }
130 190 $res = file_put_contents(
131 191 $this->tmp_diskio, "read\t". CodeProfiler_Stream::$io_read ."\nwrite\t".
@@ -168,13 +228,9 @@
168 228
169 229 // Buffer can grow *very* big hence we flush it every 10MB by default
170 230 if ( strlen( self::$tick_list ) > self::$buffer ) {
171 231 CodeProfiler_Stream::stop();
172 - file_put_contents(
173 - $this->tmp_ticks,
174 - self::$tick_list ."\t{$start}\t". $this->time() ."\n",
175 - FILE_APPEND
176 - );
232 + fwrite( self::$fh, self::$tick_list ."\t{$start}\t". $this->time() ."\n" );
177 233 CodeProfiler_Stream::start();
178 234 self::$tick_list = '';
179 235
180 236 } else {