-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aleksei Tikhomirov
committed
Apr 16, 2023
1 parent
6b1b1f6
commit 5ff681a
Showing
97 changed files
with
17,960 additions
and
976 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/* | ||
Plugin Name: WP Trace slow action | ||
Version: 1.0 | ||
Description: A simple plugin to show slow action | ||
Author: Aleksey T | ||
*/ | ||
|
||
|
||
class LogSlowActions | ||
{ | ||
public static $trace; | ||
public static $prev_action; | ||
|
||
public function add_action() | ||
{ | ||
self::$prev_action = !empty(self::$prev_action) ? self::$prev_action : microtime(true); | ||
add_action('mu_plugin_loaded', [$this, 'logger'], 99, 1); | ||
add_action('shutdown', [$this, 'logger'], 99, 1); | ||
} | ||
|
||
/** | ||
* Logger | ||
* @return void | ||
*/ | ||
public function logger() | ||
{ | ||
global $wp_filter; | ||
|
||
foreach ($wp_filter as $hook_name => $filter) { | ||
$timer = self::get_stop() - self::$prev_action; | ||
if (!empty(self::$prev_action) && $timer >= 0.02 ) { | ||
self::$trace[$hook_name] = [ | ||
'timer' => $timer, | ||
'stats' => self::get_stats(), | ||
]; | ||
} | ||
self::$prev_action = self::get_stop(); | ||
} | ||
|
||
if(self::$trace) { | ||
$this->write_log(self::$trace); | ||
} | ||
} | ||
|
||
/** | ||
* Get stop time | ||
* @return float | ||
*/ | ||
private function get_stop() | ||
{ | ||
global $timestart, $timeend; | ||
$timeend = microtime(true); | ||
$timetotal = ($timeend - $timestart) * 1000; | ||
return $timetotal; | ||
} | ||
|
||
/** | ||
* To float | ||
* | ||
* @param $number | ||
* @return float | ||
*/ | ||
private function to_float($number) | ||
{ | ||
return floatval(preg_replace('/[^\d.]/', '', number_format($number))); | ||
} | ||
|
||
private static function get_stats(){ | ||
return sprintf( | ||
__('SQL: %d за %s sec. %.2f MB ', 'wp-addon'), | ||
get_num_queries(), | ||
timer_stop(), | ||
(memory_get_peak_usage() / 1024 / 1024) | ||
); | ||
} | ||
|
||
public function write_log($data){ | ||
error_log(var_export($data, true)); | ||
} | ||
} | ||
|
||
function trace_slow_actions() | ||
{ | ||
(new LogSlowActions())->add_action(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.