# code-profiler/1.4.1/lib/class-profiler.php

Code Profiler – WordPress Performance Profiling and Debugging Made Easy, version 1.4.1. 154 lines.

- Page: https://pluginprobe.com/plugins/code-profiler/1.4.1/code/lib/class-profiler.php
- Raw: https://pluginprobe.com/plugins/code-profiler/1.4.1/raw/lib/class-profiler.php
- Modified: 2022-04-26T05:47:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-profiler/1.4.1/code/lib/class-profiler.php#L10-L20`.

```php
<?php
/*
 +=====================================================================+
 |    ____          _        ____             __ _ _                   |
 |   / ___|___   __| | ___  |  _ \ _ __ ___  / _(_) | ___ _ __         |
 |  | |   / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__|        |
 |  | |__| (_) | (_| |  __/ |  __/| | | (_) |  _| | |  __/ |           |
 |   \____\___/ \__,_|\___| |_|   |_|  \___/|_| |_|_|\___|_|           |
 |                                                                     |
 |  (c) Jerome Bruandet ~ https://code-profiler.com/                   |
 +=====================================================================+
*/

if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); }

// =====================================================================

class CodeProfiler_Profiler {

	static $tick_list;
	static $buffer = 10000000;
	static $metrics;

	private $tmp_iostats;
	private $tmp_summary;
	private $tmp_diskio;
	private $tmp_ticks;

	/*
	 * Initialize
	 */
	public function __construct() {

		$microtime = sanitize_file_name( $_REQUEST['CODE_PROFILER_ON'] );

		$this->tmp_summary = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
									CODE_PROFILER_TMP_SUMMARY_LOG;
		$this->tmp_iostats = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
									CODE_PROFILER_TMP_IOSTATS_LOG;
		$this->tmp_ticks   = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
									CODE_PROFILER_TMP_TICKS_LOG;
		$this->tmp_diskio  = CODE_PROFILER_UPLOAD_DIR ."/$microtime." .
									CODE_PROFILER_TMP_DISKIO_LOG;

		// Clear the temporary log because the buffer will be written
		// with the FILE_APPEND flag
		if ( file_exists( $this->tmp_ticks ) ) {
			unlink( $this->tmp_ticks );
		}

		if ( version_compare( PHP_VERSION, '7.3', '<' ) ) {
			self::$metrics = 'microtime';
		} else {
			self::$metrics = 'hrtime';
		}

		register_shutdown_function( array( $this, 'code_profiler_shutdown' ) );

		require 'class-stream.php';
		CodeProfiler_Stream::start();
		register_tick_function( array( $this, 'code_profiler_tick_handler' ) );
	}

	/*
	 * Save data and check for potential PHP errors.
	 */
	public function code_profiler_shutdown() {

		CodeProfiler_Stream::stop();

		$summary['memory']	= memory_get_peak_usage();
		$summary['queries']	= get_num_queries() - 2;
		file_put_contents( $this->tmp_summary, json_encode( $summary ) );

		// Catch potential error
		$e = error_get_last();
		if ( isset( $e['type'] ) && $e['type'] === E_ERROR ) {
			$err = str_replace( "\n", ' - ', $e['message'] );
			code_profiler_log_error( sprintf(
				esc_html__('Error: E_ERROR (%s - line %s in %s)', 'code-profiler'),
				esc_html( $err ),
				(int) $e['line'],
				esc_html( $e['file'] )
			));
		}

		$err_msg = esc_html__('Cannot open file for writting: %s', 'code-profiler');
		$res = file_put_contents( $this->tmp_iostats, json_encode( CodeProfiler_Stream::$io_stats ) );
		if ( $res === false ) {
			$msg = sprintf( $err_msg, $this->tmp_iostats );
			code_profiler_log_error( $msg );
		}
		$res = file_put_contents( $this->tmp_ticks, self::$tick_list, FILE_APPEND );
		if ( $res === false ) {
			$msg = sprintf( $err_msg, $this->tmp_ticks );
			code_profiler_log_error( $msg );
		}
		$res = file_put_contents(
			$this->tmp_diskio, "read\t". CodeProfiler_Stream::$io_read ."\nwrite\t".
			CodeProfiler_Stream::$io_write
		);

	}

	/*
	 * Our tick handler.
	 */
	public function code_profiler_tick_handler() {

		if ( self::$metrics == 'hrtime' ) {
			$start = hrtime( true );
		} else {
			$start = microtime( true );
		}

		$backtrace = debug_backtrace(	DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
		if ( isset( $backtrace[1] ) ) {
			$el = $backtrace[1];
		} else {
			$el = $backtrace[0];
		}

		if ( empty( $el['file'] ) ) {
			return;
		}

		self::$tick_list .= "{$el['file']}\t";
		if ( isset( $backtrace[0]['file'][0] ) ) {
			self::$tick_list .= $backtrace[0]['file'];
		} else {
			self::$tick_list .= '-';
		}

		if ( self::$metrics == 'hrtime' ) {
			self::$tick_list .="\t{$start}\t". hrtime(true) ."\n";
		} else {
			self::$tick_list .="\t{$start}\t". microtime(true) ."\n";
		}

		// Buffer can grow *very* big hence we flush it every 10MB by default
		if ( strlen( self::$tick_list ) > self::$buffer ) {
			CodeProfiler_Stream::stop();
			file_put_contents( $this->tmp_ticks, self::$tick_list, FILE_APPEND );
			CodeProfiler_Stream::start();
			self::$tick_list = '';
		}
	}
}

new CodeProfiler_Profiler();

// =====================================================================
// EOF

```
