| @@ -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'); } |
| @@ -20,9 +20,8 @@ | ||
| 20 | 20 | private $last_type; |
| 21 | 21 | private $last_time = 0; |
| 22 | 22 | private $themes = []; |
| 23 | 23 | private $plugins = []; |
| 24 | - private $composer = []; | |
| 25 | 24 | private $summary_list = []; |
| 26 | 25 | private $cx_buffer = []; |
| 27 | 26 | private $parsed_data = 0; |
| 28 | 27 | private $plugins_dir; |
| @@ -35,9 +34,9 @@ | ||
| 35 | 34 | private $tmp_iostats; |
| 36 | 35 | private $tmp_summary; |
| 37 | 36 | private $tmp_diskio; |
| 38 | 37 | private $tmp_connections; |
| 39 | - private $tmp_ticks; | |
| 38 | + private $tmp_calls; | |
| 40 | 39 | |
| 41 | 40 | |
| 42 | 41 | /** |
| 43 | 42 | * Initialize |
| @@ -47,10 +46,10 @@ | ||
| 47 | 46 | $this->tmp_summary = CODE_PROFILER_UPLOAD_DIR ."/$microtime." . |
| 48 | 47 | CODE_PROFILER_TMP_SUMMARY_LOG; |
| 49 | 48 | $this->tmp_iostats = CODE_PROFILER_UPLOAD_DIR ."/$microtime." . |
| 50 | 49 | CODE_PROFILER_TMP_IOSTATS_LOG; |
| 51 | - $this->tmp_ticks = CODE_PROFILER_UPLOAD_DIR ."/$microtime." . | |
| 52 | - CODE_PROFILER_TMP_TICKS_LOG; | |
| 50 | + $this->tmp_calls = CODE_PROFILER_UPLOAD_DIR ."/$microtime." . | |
| 51 | + CODE_PROFILER_TMP_CALLS_LOG; | |
| 53 | 52 | $this->tmp_diskio = CODE_PROFILER_UPLOAD_DIR ."/$microtime." . |
| 54 | 53 | CODE_PROFILER_TMP_DISKIO_LOG; |
| 55 | 54 | $this->tmp_connections = CODE_PROFILER_UPLOAD_DIR ."/$microtime." . |
| 56 | 55 | CODE_PROFILER_TMP_CONNECTIONS_LOG; |
| @@ -57,25 +56,26 @@ | ||
| 57 | 56 | // Used for naming, not metrics |
| 58 | 57 | $this->microtime = $microtime; |
| 59 | 58 | |
| 60 | 59 | // Make sure all files are there |
| 61 | - if (! file_exists( $this->tmp_ticks ) ) { | |
| 62 | - $cp_error = 1; | |
| 60 | + if (! file_exists( $this->tmp_calls ) ) { | |
| 61 | + $cp_error = 'calls'; | |
| 63 | 62 | } elseif (! file_exists( $this->tmp_iostats ) ) { |
| 64 | - $cp_error = 2; | |
| 63 | + $cp_error = 'iostats'; | |
| 65 | 64 | } |
| 66 | 65 | if (! empty( $cp_error ) ) { |
| 67 | 66 | $error = sprintf( |
| 68 | - esc_html__('Cannot create the report: the profiler did not generate a data file (#%s). Make sure the following directory is writable: %s. If you are using a caching plugin or an opcode cache, try to disable it. You may also find more details about the error in the "Log" tab.', 'code-profiler'), | |
| 69 | - $cp_error, | |
| 70 | - CODE_PROFILER_UPLOAD_DIR .'/' | |
| 71 | - ); | |
| 67 | + esc_html__('Cannot create the report: the profiler did not generate a data file (%s). '. | |
| 68 | + 'You may find more details about this error in the "Log" tab or your PHP error log.', | |
| 69 | + 'code-profiler'), | |
| 70 | + $cp_error | |
| 71 | + ); | |
| 72 | 72 | $this->return_error( $error ); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // Total data analyzed |
| 76 | 76 | $this->parsed_data += filesize( $this->tmp_iostats ); |
| 77 | - $this->parsed_data += filesize( $this->tmp_ticks ); | |
| 77 | + $this->parsed_data += filesize( $this->tmp_calls ); | |
| 78 | 78 | |
| 79 | 79 | $this->profile_name = $profile_name; |
| 80 | 80 | $this->plugins_dir = preg_quote( realpath( WP_PLUGIN_DIR ) ); |
| 81 | 81 | $this->themes_dir = preg_quote( realpath( WP_CONTENT_DIR .'/themes') ); |
| @@ -87,18 +87,18 @@ | ||
| 87 | 87 | * Filter and save the profiler's data |
| 88 | 88 | */ |
| 89 | 89 | public function prepare_report() { |
| 90 | 90 | |
| 91 | - $this->parse_ticks(); | |
| 91 | + $this->parse_calls(); | |
| 92 | 92 | $this->get_plugins_theme_name(); |
| 93 | 93 | $this->save_iostats(); |
| 94 | 94 | $this->save_connections(); |
| 95 | 95 | $this->save_data(); |
| 96 | 96 | $this->save_diskio(); |
| 97 | - $this->save_composer(); | |
| 98 | 97 | |
| 99 | 98 | code_profiler_log_info( sprintf( |
| 100 | - __('Volume of code and data analyzed: %1$sMB (%2$s plugins and 1 theme) in %4$ss - Memory used: %3$sMB', 'code-profiler'), | |
| 99 | + __('Volume of code and data analyzed: %1$sMB (%2$s plugins and '. | |
| 100 | + '1 theme) in %4$ss - Memory used: %3$sMB', 'code-profiler'), | |
| 101 | 101 | number_format( $this->parsed_data / 1024 / 1024, 2 ), |
| 102 | 102 | (int) $this->total_plugins, |
| 103 | 103 | number_format( memory_get_peak_usage( false ) / 1024 / 1024, 2 ), |
| 104 | 104 | number_format( microtime( true ) - $this->microtime, 2 ) |
| @@ -187,26 +187,35 @@ | ||
| 187 | 187 | // Summary |
| 188 | 188 | $s = json_decode( file_get_contents( $this->tmp_summary ), true ); |
| 189 | 189 | unlink( $this->tmp_summary ); |
| 190 | 190 | if ( empty( $s['memory'] ) ) { |
| 191 | - $this->summary_list['memory'] = '-'; | |
| 191 | + $this->summary_list['memory'] = 0; | |
| 192 | 192 | } else { |
| 193 | 193 | $this->summary_list['memory'] = $s['memory'] / 1024 / 1024; |
| 194 | 194 | } |
| 195 | 195 | if ( empty( $s['queries'] ) ) { |
| 196 | - $this->summary_list['queries']= '-'; | |
| 196 | + $this->summary_list['queries']= 0; | |
| 197 | 197 | } else { |
| 198 | 198 | $this->summary_list['queries']= $s['queries']; |
| 199 | 199 | } |
| 200 | 200 | if ( empty( $this->total_io ) ) { |
| 201 | - $this->summary_list['io'] = '-'; | |
| 201 | + $this->summary_list['io'] = 0; | |
| 202 | 202 | } else { |
| 203 | 203 | $this->summary_list['io'] = $this->total_io; |
| 204 | 204 | } |
| 205 | + if (! empty( $s['precision'] ) ) { | |
| 206 | + $this->summary_list['precision'] = $s['precision']; | |
| 207 | + } | |
| 205 | 208 | $this->summary_list['items'] = $this->total_plugins + 1; // Add the theme |
| 206 | 209 | $this->summary_list['time'] = $this->convert_2_seconds( $this->summary_list['time'] ); |
| 207 | 210 | $this->summary_list['time'] = number_format( $this->summary_list['time'], 4 ); |
| 208 | 211 | |
| 212 | + // Re-run options | |
| 213 | + if (! empty( $s['rerun'] ) ) { | |
| 214 | + $this->summary_list['rerun'] = $s['rerun']; | |
| 215 | + $this->summary_list['rerun']['profile'] = $this->profile_name; | |
| 216 | + } | |
| 217 | + | |
| 209 | 218 | // Save Code Profiler, PHP and WordPress' versions |
| 210 | 219 | global $wp_version; |
| 211 | 220 | $this->summary_list['versions'] = [ |
| 212 | 221 | 'wp' => $wp_version, |
| @@ -214,9 +223,8 @@ | ||
| 214 | 223 | 'php' => PHP_VERSION |
| 215 | 224 | ]; |
| 216 | 225 | |
| 217 | 226 | file_put_contents( $summary_json, json_encode( $this->summary_list ) ); |
| 218 | - | |
| 219 | 227 | } |
| 220 | 228 | |
| 221 | 229 | |
| 222 | 230 | /** |
| @@ -242,9 +250,13 @@ | ||
| 242 | 250 | * Check if a slug is from a plugin or theme. |
| 243 | 251 | */ |
| 244 | 252 | private function plugin_or_theme( $script ) { |
| 245 | 253 | |
| 246 | - $res = ['theme' => '', 'plugin' => '', 'script' => '']; | |
| 254 | + $res = [ | |
| 255 | + 'theme' => '', | |
| 256 | + 'plugin' => '', | |
| 257 | + 'script' => '' | |
| 258 | + ]; | |
| 247 | 259 | |
| 248 | 260 | if ( preg_match("`^{$this->plugins_dir}[\\\/](?:(.+?)[\\\/]|([^\\\/]+\.php)$)`", $script, $slug ) ) { |
| 249 | 261 | if (! empty( $slug[1] ) ) { |
| 250 | 262 | $res['plugin'] = $slug[1]; |
| @@ -306,15 +318,15 @@ | ||
| 306 | 318 | |
| 307 | 319 | /** |
| 308 | 320 | * Parse, filter and sort the data |
| 309 | 321 | */ |
| 310 | - private function parse_ticks() { | |
| 322 | + private function parse_calls() { | |
| 311 | 323 | |
| 312 | - $fh = fopen( $this->tmp_ticks, 'rb'); | |
| 324 | + $fh = fopen( $this->tmp_calls, 'rb'); | |
| 313 | 325 | if ( $fh === false ) { |
| 314 | 326 | $error = sprintf( |
| 315 | 327 | esc_html__('Cannot open file for reading: %s', 'code-profiler'), |
| 316 | - $this->tmp_ticks | |
| 328 | + $this->tmp_calls | |
| 317 | 329 | ); |
| 318 | 330 | $this->return_error( $error ); |
| 319 | 331 | } |
| 320 | 332 | while(! feof( $fh ) ) { |
| @@ -376,16 +388,8 @@ | ||
| 376 | 388 | if (! isset( $this->plugins[ $slug['plugin'] ]['time'] ) ) { |
| 377 | 389 | $this->plugins[ $slug['plugin'] ]['time'] = 0; |
| 378 | 390 | } |
| 379 | 391 | } |
| 380 | - | |
| 381 | - // Look for multiple copies of composer and warn the user | |
| 382 | - if ( preg_match("`^{$this->plugins_dir}[\\\/](?:[^\\\/]+)[\\\/].+?[\\\/]composer[\\\/]autoload_real\.php`", $caller ) ) { | |
| 383 | - // Save the slug first, we'll fetch the name later | |
| 384 | - if (! isset( $this->composer[ $slug['plugin'] ] ) ) { | |
| 385 | - $this->composer[ $slug['plugin'] ] = ''; | |
| 386 | - } | |
| 387 | - } | |
| 388 | 392 | } |
| 389 | 393 | |
| 390 | 394 | // We don't have a slug: if there's an old record, update it |
| 391 | 395 | } else { |
| @@ -417,9 +421,9 @@ | ||
| 417 | 421 | } |
| 418 | 422 | } |
| 419 | 423 | |
| 420 | 424 | fclose( $fh ); |
| 421 | - unlink( $this->tmp_ticks ); | |
| 425 | + unlink( $this->tmp_calls ); | |
| 422 | 426 | } |
| 423 | 427 | |
| 424 | 428 | /******************************************************************** |
| 425 | 429 | * Save IO stats |
| @@ -462,27 +466,8 @@ | ||
| 462 | 466 | ); |
| 463 | 467 | } |
| 464 | 468 | |
| 465 | 469 | |
| 466 | - /** | |
| 467 | - * Save list of plugins using composer | |
| 468 | - */ | |
| 469 | - private function save_composer() { | |
| 470 | - | |
| 471 | - if (! empty( $this->composer ) ) { | |
| 472 | - // Try to get the plugin's name | |
| 473 | - foreach( $this->composer as $slug => $v ) { | |
| 474 | - if ( isset( $this->plugins[ $slug ]['name'] ) ) { | |
| 475 | - $this->composer[ $slug ] = $this->plugins[ $slug ]['name']; | |
| 476 | - } | |
| 477 | - } | |
| 478 | - file_put_contents( | |
| 479 | - CODE_PROFILER_UPLOAD_DIR ."/{$this->microtime}.{$this->profile_name}.composer.profile", | |
| 480 | - json_encode( $this->composer ) | |
| 481 | - ); | |
| 482 | - } | |
| 483 | - } | |
| 484 | - | |
| 485 | 470 | /** |
| 486 | 471 | * External connections. |
| 487 | 472 | */ |
| 488 | 473 | private function save_connections() { |
| @@ -498,13 +483,15 @@ | ||
| 498 | 483 | return; |
| 499 | 484 | } |
| 500 | 485 | |
| 501 | 486 | foreach( $connections as $connection => $array ) { |
| 502 | - $found = 0; | |
| 487 | + $found_call = 0; | |
| 488 | + $found_caller = 0; | |
| 503 | 489 | |
| 504 | 490 | foreach( $array['bt'] as $k =>$v ) { |
| 505 | 491 | if ( isset( $v['file'] ) && isset( $v['function'] ) && isset( $v['line'] ) ) { |
| 506 | - if ( ! $found && | |
| 492 | + | |
| 493 | + if (! $found_caller && ( $found_call || | |
| 507 | 494 | in_array( $v['function'], [ |
| 508 | 495 | 'wp_remote_get', |
| 509 | 496 | 'wp_safe_remote_get', |
| 510 | 497 | 'wp_remote_post', |
| @@ -509,19 +496,31 @@ | ||
| 509 | 496 | 'wp_safe_remote_get', |
| 510 | 497 | 'wp_remote_post', |
| 511 | 498 | 'wp_safe_remote_post' |
| 512 | 499 | ] ) |
| 513 | - ) { | |
| 514 | - $found = 1; | |
| 500 | + ) ) { | |
| 515 | 501 | |
| 502 | + $found_call = 1; | |
| 503 | + | |
| 516 | 504 | $type = $this->plugin_or_theme( $v['file'] ); |
| 505 | + | |
| 517 | 506 | if (! empty( $type['theme'] ) ) { |
| 518 | 507 | // Save it for later use |
| 519 | - $this->cx_buffer['theme'][ $type['theme'] ] = $array['stop'] - $connection; | |
| 508 | + if ( isset( $this->cx_buffer['theme'][ $type['theme'] ] ) ) { | |
| 509 | + $this->cx_buffer['theme'][ $type['theme'] ] += $array['stop'] - $connection; | |
| 510 | + } else { | |
| 511 | + $this->cx_buffer['theme'][ $type['theme'] ] = $array['stop'] - $connection; | |
| 512 | + } | |
| 513 | + $found_caller = 1; | |
| 520 | 514 | |
| 521 | 515 | } elseif (! empty( $type['plugin'] ) ) { |
| 522 | 516 | // Save it for later use |
| 523 | - $this->cx_buffer['plugin'][ $type['plugin'] ] = $array['stop'] - $connection; | |
| 517 | + if ( isset( $this->cx_buffer['plugin'][ $type['plugin'] ] ) ) { | |
| 518 | + $this->cx_buffer['plugin'][ $type['plugin'] ] += $array['stop'] - $connection; | |
| 519 | + } else { | |
| 520 | + $this->cx_buffer['plugin'][ $type['plugin'] ] = $array['stop'] - $connection; | |
| 521 | + } | |
| 522 | + $found_caller = 1; | |
| 524 | 523 | } |
| 525 | 524 | } |
| 526 | 525 | } |
| 527 | 526 | } |