PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.4.4
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.4.4
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 1.6.8 All 40 releases
code-profiler / lib / class-profiler.php

class-profiler.php in Code Profiler – WordPress Performance Profiling and Debugging Made Easy 1.4.4, at lib/class-profiler.php

155 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 +=====================================================================+
4 | ____ _ ____ __ _ _ |
5 | / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ |
6 | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| |
7 | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | |
8 | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| |
9 | |
10 | (c) Jerome Bruandet ~ https://code-profiler.com/ |
11 +=====================================================================+
12 */
13
14 if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); }
15
16 // =====================================================================
17
18 class CodeProfiler_Profiler {
19
20 static $tick_list;
21 static $buffer = 10000000;
22 static $metrics;
23
24 private $tmp_iostats;
25 private $tmp_summary;
26 private $tmp_diskio;
27 private $tmp_ticks;
28
29 /*
30 * Initialize
31 */
32 public function __construct() {
33
34 $microtime = sanitize_file_name( $_REQUEST['CODE_PROFILER_ON'] );
35
36 $this->tmp_summary = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
37 CODE_PROFILER_TMP_SUMMARY_LOG;
38 $this->tmp_iostats = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
39 CODE_PROFILER_TMP_IOSTATS_LOG;
40 $this->tmp_ticks = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
41 CODE_PROFILER_TMP_TICKS_LOG;
42 $this->tmp_diskio = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
43 CODE_PROFILER_TMP_DISKIO_LOG;
44
45 // Clear the temporary log because the buffer will be written
46 // with the FILE_APPEND flag
47 if ( file_exists( $this->tmp_ticks ) ) {
48 unlink( $this->tmp_ticks );
49 }
50
51 if ( version_compare( PHP_VERSION, '7.3', '<' ) ) {
52 self::$metrics = 'microtime';
53 } else {
54 self::$metrics = 'hrtime';
55 }
56
57 register_shutdown_function( array( $this, 'code_profiler_shutdown' ) );
58
59 require 'class-stream.php';
60 CodeProfiler_Stream::start();
61 register_tick_function( array( $this, 'code_profiler_tick_handler' ) );
62 }
63
64 /*
65 * Save data and check for potential PHP errors.
66 */
67 public function code_profiler_shutdown() {
68
69 CodeProfiler_Stream::stop();
70
71 $summary['memory'] = memory_get_peak_usage();
72 $summary['queries'] = get_num_queries() - 2;
73 file_put_contents( $this->tmp_summary, json_encode( $summary ) );
74
75 // Catch potential error
76 $e = error_get_last();
77 if ( isset( $e['type'] ) && $e['type'] === E_ERROR ) {
78 $err = str_replace( "\n", ' - ', $e['message'] );
79 code_profiler_log_error( sprintf(
80 /* Translators: Error message, line number and script */
81 esc_html__('Error: E_ERROR (%s - line %s in %s)', 'code-profiler'),
82 esc_html( $err ),
83 (int) $e['line'],
84 esc_html( $e['file'] )
85 ));
86 }
87
88 $err_msg = esc_html__('Cannot open file for writting: %s', 'code-profiler');
89 $res = file_put_contents( $this->tmp_iostats, json_encode( CodeProfiler_Stream::$io_stats ) );
90 if ( $res === false ) {
91 $msg = sprintf( $err_msg, $this->tmp_iostats );
92 code_profiler_log_error( $msg );
93 }
94 $res = file_put_contents( $this->tmp_ticks, self::$tick_list, FILE_APPEND );
95 if ( $res === false ) {
96 $msg = sprintf( $err_msg, $this->tmp_ticks );
97 code_profiler_log_error( $msg );
98 }
99 $res = file_put_contents(
100 $this->tmp_diskio, "read\t". CodeProfiler_Stream::$io_read ."\nwrite\t".
101 CodeProfiler_Stream::$io_write
102 );
103
104 }
105
106 /*
107 * Our tick handler.
108 */
109 public function code_profiler_tick_handler() {
110
111 if ( self::$metrics == 'hrtime' ) {
112 $start = hrtime( true );
113 } else {
114 $start = microtime( true );
115 }
116
117 $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
118 if ( isset( $backtrace[1] ) ) {
119 $el = $backtrace[1];
120 } else {
121 $el = $backtrace[0];
122 }
123
124 if ( empty( $el['file'] ) ) {
125 return;
126 }
127
128 self::$tick_list .= "{$el['file']}\t";
129 if ( isset( $backtrace[0]['file'][0] ) ) {
130 self::$tick_list .= $backtrace[0]['file'];
131 } else {
132 self::$tick_list .= '-';
133 }
134
135 if ( self::$metrics == 'hrtime' ) {
136 self::$tick_list .="\t{$start}\t". hrtime(true) ."\n";
137 } else {
138 self::$tick_list .="\t{$start}\t". microtime(true) ."\n";
139 }
140
141 // Buffer can grow *very* big hence we flush it every 10MB by default
142 if ( strlen( self::$tick_list ) > self::$buffer ) {
143 CodeProfiler_Stream::stop();
144 file_put_contents( $this->tmp_ticks, self::$tick_list, FILE_APPEND );
145 CodeProfiler_Stream::start();
146 self::$tick_list = '';
147 }
148 }
149 }
150
151 new CodeProfiler_Profiler();
152
153 // =====================================================================
154 // EOF
155