PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.4
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.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, at lib/class-profiler.php

154 lines 4.4 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 esc_html__('Error: E_ERROR (%s - line %s in %s)', 'code-profiler'),
81 esc_html( $err ),
82 (int) $e['line'],
83 esc_html( $e['file'] )
84 ));
85 }
86
87 $err_msg = esc_html__('Cannot open file for writting: %s', 'code-profiler');
88 $res = file_put_contents( $this->tmp_iostats, json_encode( CodeProfiler_Stream::$io_stats ) );
89 if ( $res === false ) {
90 $msg = sprintf( $err_msg, $this->tmp_iostats );
91 code_profiler_log_error( $msg );
92 }
93 $res = file_put_contents( $this->tmp_ticks, self::$tick_list, FILE_APPEND );
94 if ( $res === false ) {
95 $msg = sprintf( $err_msg, $this->tmp_ticks );
96 code_profiler_log_error( $msg );
97 }
98 $res = file_put_contents(
99 $this->tmp_diskio, "read\t". CodeProfiler_Stream::$io_read ."\nwrite\t".
100 CodeProfiler_Stream::$io_write
101 );
102
103 }
104
105 /*
106 * Our tick handler.
107 */
108 public function code_profiler_tick_handler() {
109
110 if ( self::$metrics == 'hrtime' ) {
111 $start = hrtime( true );
112 } else {
113 $start = microtime( true );
114 }
115
116 $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
117 if ( isset( $backtrace[1] ) ) {
118 $el = $backtrace[1];
119 } else {
120 $el = $backtrace[0];
121 }
122
123 if ( empty( $el['file'] ) ) {
124 return;
125 }
126
127 self::$tick_list .= "{$el['file']}\t";
128 if ( isset( $backtrace[0]['file'][0] ) ) {
129 self::$tick_list .= $backtrace[0]['file'];
130 } else {
131 self::$tick_list .= '-';
132 }
133
134 if ( self::$metrics == 'hrtime' ) {
135 self::$tick_list .="\t{$start}\t". hrtime(true) ."\n";
136 } else {
137 self::$tick_list .="\t{$start}\t". microtime(true) ."\n";
138 }
139
140 // Buffer can grow *very* big hence we flush it every 10MB by default
141 if ( strlen( self::$tick_list ) > self::$buffer ) {
142 CodeProfiler_Stream::stop();
143 file_put_contents( $this->tmp_ticks, self::$tick_list, FILE_APPEND );
144 CodeProfiler_Stream::start();
145 self::$tick_list = '';
146 }
147 }
148 }
149
150 new CodeProfiler_Profiler();
151
152 // =====================================================================
153 // EOF
154