| @@ -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,29 +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 '. | |
| 69 | - 'generate a data file (#%s). Make sure the following directory '. | |
| 70 | - 'is writable: %s. If you are using a caching plugin or an '. | |
| 71 | - 'opcode cache, try to disable it. You may also find more '. | |
| 72 | - 'details about the error in the "Log" tab.', 'code-profiler'), | |
| 73 | - $cp_error, | |
| 74 | - CODE_PROFILER_UPLOAD_DIR .'/' | |
| 75 | - ); | |
| 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 | + ); | |
| 76 | 72 | $this->return_error( $error ); |
| 77 | 73 | } |
| 78 | 74 | |
| 79 | 75 | // Total data analyzed |
| 80 | 76 | $this->parsed_data += filesize( $this->tmp_iostats ); |
| 81 | - $this->parsed_data += filesize( $this->tmp_ticks ); | |
| 77 | + $this->parsed_data += filesize( $this->tmp_calls ); | |
| 82 | 78 | |
| 83 | 79 | $this->profile_name = $profile_name; |
| 84 | 80 | $this->plugins_dir = preg_quote( realpath( WP_PLUGIN_DIR ) ); |
| 85 | 81 | $this->themes_dir = preg_quote( realpath( WP_CONTENT_DIR .'/themes') ); |
| @@ -91,15 +87,14 @@ | ||
| 91 | 87 | * Filter and save the profiler's data |
| 92 | 88 | */ |
| 93 | 89 | public function prepare_report() { |
| 94 | 90 | |
| 95 | - $this->parse_ticks(); | |
| 91 | + $this->parse_calls(); | |
| 96 | 92 | $this->get_plugins_theme_name(); |
| 97 | 93 | $this->save_iostats(); |
| 98 | 94 | $this->save_connections(); |
| 99 | 95 | $this->save_data(); |
| 100 | 96 | $this->save_diskio(); |
| 101 | - $this->save_composer(); | |
| 102 | 97 | |
| 103 | 98 | code_profiler_log_info( sprintf( |
| 104 | 99 | __('Volume of code and data analyzed: %1$sMB (%2$s plugins and '. |
| 105 | 100 | '1 theme) in %4$ss - Memory used: %3$sMB', 'code-profiler'), |
| @@ -192,26 +187,35 @@ | ||
| 192 | 187 | // Summary |
| 193 | 188 | $s = json_decode( file_get_contents( $this->tmp_summary ), true ); |
| 194 | 189 | unlink( $this->tmp_summary ); |
| 195 | 190 | if ( empty( $s['memory'] ) ) { |
| 196 | - $this->summary_list['memory'] = '-'; | |
| 191 | + $this->summary_list['memory'] = 0; | |
| 197 | 192 | } else { |
| 198 | 193 | $this->summary_list['memory'] = $s['memory'] / 1024 / 1024; |
| 199 | 194 | } |
| 200 | 195 | if ( empty( $s['queries'] ) ) { |
| 201 | - $this->summary_list['queries']= '-'; | |
| 196 | + $this->summary_list['queries']= 0; | |
| 202 | 197 | } else { |
| 203 | 198 | $this->summary_list['queries']= $s['queries']; |
| 204 | 199 | } |
| 205 | 200 | if ( empty( $this->total_io ) ) { |
| 206 | - $this->summary_list['io'] = '-'; | |
| 201 | + $this->summary_list['io'] = 0; | |
| 207 | 202 | } else { |
| 208 | 203 | $this->summary_list['io'] = $this->total_io; |
| 209 | 204 | } |
| 205 | + if (! empty( $s['precision'] ) ) { | |
| 206 | + $this->summary_list['precision'] = $s['precision']; | |
| 207 | + } | |
| 210 | 208 | $this->summary_list['items'] = $this->total_plugins + 1; // Add the theme |
| 211 | 209 | $this->summary_list['time'] = $this->convert_2_seconds( $this->summary_list['time'] ); |
| 212 | 210 | $this->summary_list['time'] = number_format( $this->summary_list['time'], 4 ); |
| 213 | 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 | + | |
| 214 | 218 | // Save Code Profiler, PHP and WordPress' versions |
| 215 | 219 | global $wp_version; |
| 216 | 220 | $this->summary_list['versions'] = [ |
| 217 | 221 | 'wp' => $wp_version, |
| @@ -219,9 +223,8 @@ | ||
| 219 | 223 | 'php' => PHP_VERSION |
| 220 | 224 | ]; |
| 221 | 225 | |
| 222 | 226 | file_put_contents( $summary_json, json_encode( $this->summary_list ) ); |
| 223 | - | |
| 224 | 227 | } |
| 225 | 228 | |
| 226 | 229 | |
| 227 | 230 | /** |
| @@ -315,15 +318,15 @@ | ||
| 315 | 318 | |
| 316 | 319 | /** |
| 317 | 320 | * Parse, filter and sort the data |
| 318 | 321 | */ |
| 319 | - private function parse_ticks() { | |
| 322 | + private function parse_calls() { | |
| 320 | 323 | |
| 321 | - $fh = fopen( $this->tmp_ticks, 'rb'); | |
| 324 | + $fh = fopen( $this->tmp_calls, 'rb'); | |
| 322 | 325 | if ( $fh === false ) { |
| 323 | 326 | $error = sprintf( |
| 324 | 327 | esc_html__('Cannot open file for reading: %s', 'code-profiler'), |
| 325 | - $this->tmp_ticks | |
| 328 | + $this->tmp_calls | |
| 326 | 329 | ); |
| 327 | 330 | $this->return_error( $error ); |
| 328 | 331 | } |
| 329 | 332 | while(! feof( $fh ) ) { |
| @@ -385,16 +388,8 @@ | ||
| 385 | 388 | if (! isset( $this->plugins[ $slug['plugin'] ]['time'] ) ) { |
| 386 | 389 | $this->plugins[ $slug['plugin'] ]['time'] = 0; |
| 387 | 390 | } |
| 388 | 391 | } |
| 389 | - | |
| 390 | - // Look for multiple copies of composer and warn the user | |
| 391 | - if ( preg_match("`^{$this->plugins_dir}[\\\/](?:[^\\\/]+)[\\\/].+?[\\\/]composer[\\\/]autoload_real\.php`", $caller ) ) { | |
| 392 | - // Save the slug first, we'll fetch the name later | |
| 393 | - if (! isset( $this->composer[ $slug['plugin'] ] ) ) { | |
| 394 | - $this->composer[ $slug['plugin'] ] = ''; | |
| 395 | - } | |
| 396 | - } | |
| 397 | 392 | } |
| 398 | 393 | |
| 399 | 394 | // We don't have a slug: if there's an old record, update it |
| 400 | 395 | } else { |
| @@ -426,9 +421,9 @@ | ||
| 426 | 421 | } |
| 427 | 422 | } |
| 428 | 423 | |
| 429 | 424 | fclose( $fh ); |
| 430 | - unlink( $this->tmp_ticks ); | |
| 425 | + unlink( $this->tmp_calls ); | |
| 431 | 426 | } |
| 432 | 427 | |
| 433 | 428 | /******************************************************************** |
| 434 | 429 | * Save IO stats |
| @@ -471,27 +466,8 @@ | ||
| 471 | 466 | ); |
| 472 | 467 | } |
| 473 | 468 | |
| 474 | 469 | |
| 475 | - /** | |
| 476 | - * Save list of plugins using composer | |
| 477 | - */ | |
| 478 | - private function save_composer() { | |
| 479 | - | |
| 480 | - if (! empty( $this->composer ) ) { | |
| 481 | - // Try to get the plugin's name | |
| 482 | - foreach( $this->composer as $slug => $v ) { | |
| 483 | - if ( isset( $this->plugins[ $slug ]['name'] ) ) { | |
| 484 | - $this->composer[ $slug ] = $this->plugins[ $slug ]['name']; | |
| 485 | - } | |
| 486 | - } | |
| 487 | - file_put_contents( | |
| 488 | - CODE_PROFILER_UPLOAD_DIR ."/{$this->microtime}.{$this->profile_name}.composer.profile", | |
| 489 | - json_encode( $this->composer ) | |
| 490 | - ); | |
| 491 | - } | |
| 492 | - } | |
| 493 | - | |
| 494 | 470 | /** |
| 495 | 471 | * External connections. |
| 496 | 472 | */ |
| 497 | 473 | private function save_connections() { |
| @@ -507,13 +483,15 @@ | ||
| 507 | 483 | return; |
| 508 | 484 | } |
| 509 | 485 | |
| 510 | 486 | foreach( $connections as $connection => $array ) { |
| 511 | - $found = 0; | |
| 487 | + $found_call = 0; | |
| 488 | + $found_caller = 0; | |
| 512 | 489 | |
| 513 | 490 | foreach( $array['bt'] as $k =>$v ) { |
| 514 | 491 | if ( isset( $v['file'] ) && isset( $v['function'] ) && isset( $v['line'] ) ) { |
| 515 | - if ( ! $found && | |
| 492 | + | |
| 493 | + if (! $found_caller && ( $found_call || | |
| 516 | 494 | in_array( $v['function'], [ |
| 517 | 495 | 'wp_remote_get', |
| 518 | 496 | 'wp_safe_remote_get', |
| 519 | 497 | 'wp_remote_post', |
| @@ -518,19 +496,31 @@ | ||
| 518 | 496 | 'wp_safe_remote_get', |
| 519 | 497 | 'wp_remote_post', |
| 520 | 498 | 'wp_safe_remote_post' |
| 521 | 499 | ] ) |
| 522 | - ) { | |
| 523 | - $found = 1; | |
| 500 | + ) ) { | |
| 524 | 501 | |
| 502 | + $found_call = 1; | |
| 503 | + | |
| 525 | 504 | $type = $this->plugin_or_theme( $v['file'] ); |
| 505 | + | |
| 526 | 506 | if (! empty( $type['theme'] ) ) { |
| 527 | 507 | // Save it for later use |
| 528 | - $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; | |
| 529 | 514 | |
| 530 | 515 | } elseif (! empty( $type['plugin'] ) ) { |
| 531 | 516 | // Save it for later use |
| 532 | - $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; | |
| 533 | 523 | } |
| 534 | 524 | } |
| 535 | 525 | } |
| 536 | 526 | } |