|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin Page Framework |
|
4
|
|
|
* |
|
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
|
6
|
|
|
* Copyright (c) 2013-2016 Michael Uno; Licensed MIT |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Collects data of page loads in admin pages. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 2.1.7 |
|
14
|
|
|
* @package AdminPageFramework |
|
15
|
|
|
* @subpackage Common/Factory/Debug |
|
16
|
|
|
* @internal |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AdminPageFramework_PageLoadInfo_Base extends AdminPageFramework_FrameworkUtility { |
|
19
|
|
|
|
|
20
|
|
|
public $oProp; |
|
21
|
|
|
|
|
22
|
|
|
public $oMsg; |
|
23
|
|
|
|
|
24
|
|
|
protected $_nInitialMemoryUsage; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Sets up hooks and properties. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct( $oProp, $oMsg ) { |
|
30
|
|
|
|
|
31
|
|
|
if ( ! $this->_shouldProceed( $oProp ) ) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->oProp = $oProp; |
|
36
|
|
|
$this->oMsg = $oMsg; |
|
37
|
|
|
$this->_nInitialMemoryUsage = memory_get_usage(); |
|
38
|
|
|
|
|
39
|
|
|
add_action( 'in_admin_footer', array( $this, '_replyToSetPageLoadInfoInFooter' ), 999 ); |
|
40
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
/** |
|
43
|
|
|
* @since 3.8.5 |
|
44
|
|
|
* @return boolean |
|
45
|
|
|
*/ |
|
46
|
|
|
private function _shouldProceed( $oProp ) { |
|
47
|
|
|
|
|
48
|
|
|
if ( $oProp->bIsAdminAjax || ! $oProp->bIsAdmin ) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
return ( boolean ) $oProp->bShowDebugInfo; |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @remark Should be overridden in an extended class. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function _replyToSetPageLoadInfoInFooter() {} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Indicates whether the page load info is inserted or not. |
|
62
|
|
|
*/ |
|
63
|
|
|
static private $_bLoadedPageLoadInfo = false; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Display gathered information. |
|
67
|
|
|
* |
|
68
|
|
|
* @access public |
|
69
|
|
|
* @internal |
|
70
|
|
|
*/ |
|
71
|
|
|
public function _replyToGetPageLoadInfo( $sFooterHTML ) { |
|
72
|
|
|
|
|
73
|
|
|
if ( self::$_bLoadedPageLoadInfo ) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
self::$_bLoadedPageLoadInfo = true; |
|
77
|
|
|
|
|
78
|
|
|
$_nSeconds = timer_stop( 0 ); |
|
79
|
|
|
$_nQueryCount = get_num_queries(); |
|
80
|
|
|
$_nMemoryUsage = round( $this->_convertBytesToHR( memory_get_usage() ), 2 ); |
|
81
|
|
|
$_nMemoryPeakUsage = round( $this->_convertBytesToHR( memory_get_peak_usage() ), 2 ); |
|
82
|
|
|
$_nMemoryLimit = round( $this->_convertBytesToHR( $this->_convertToNumber( WP_MEMORY_LIMIT ) ), 2 ); |
|
83
|
|
|
$_sInitialMemoryUsage = round( $this->_convertBytesToHR( $this->_nInitialMemoryUsage ), 2 ); |
|
84
|
|
|
|
|
85
|
|
|
return $sFooterHTML |
|
86
|
|
|
. "<div id='admin-page-framework-page-load-stats'>" |
|
87
|
|
|
. "<ul>" |
|
88
|
|
|
. "<li>" . sprintf( $this->oMsg->get( 'queries_in_seconds' ), $_nQueryCount, $_nSeconds ) . "</li>" |
|
89
|
|
|
. "<li>" . sprintf( $this->oMsg->get( 'out_of_x_memory_used' ), $_nMemoryUsage, $_nMemoryLimit, round( ( $_nMemoryUsage / $_nMemoryLimit ), 2 ) * 100 . '%' ) . "</li>" |
|
90
|
|
|
. "<li>" . sprintf( $this->oMsg->get( 'peak_memory_usage' ), $_nMemoryPeakUsage ) . "</li>" |
|
91
|
|
|
. "<li>" . sprintf( $this->oMsg->get( 'initial_memory_usage' ), $_sInitialMemoryUsage ) . "</li>" |
|
92
|
|
|
. "</ul>" |
|
93
|
|
|
. "</div>"; |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Transforms the php.ini notation for numbers (like '2M') to an integer |
|
99
|
|
|
* |
|
100
|
|
|
* @access private |
|
101
|
|
|
* @param $nSize |
|
102
|
|
|
* @return int |
|
103
|
|
|
* @remark This is influenced by the work of Mike Jolley. |
|
104
|
|
|
* @see http://mikejolley.com/projects/wp-page-load-stats/ |
|
105
|
|
|
* @internal |
|
106
|
|
|
*/ |
|
107
|
|
|
private function _convertToNumber( $nSize ) { |
|
108
|
|
|
|
|
109
|
|
|
$_nReturn = substr( $nSize, 0, -1 ); |
|
110
|
|
|
switch( strtoupper( substr( $nSize, -1 ) ) ) { |
|
111
|
|
|
case 'P': |
|
|
|
|
|
|
112
|
|
|
$_nReturn *= 1024; |
|
113
|
|
|
case 'T': |
|
|
|
|
|
|
114
|
|
|
$_nReturn *= 1024; |
|
115
|
|
|
case 'G': |
|
|
|
|
|
|
116
|
|
|
$_nReturn *= 1024; |
|
117
|
|
|
case 'M': |
|
|
|
|
|
|
118
|
|
|
$_nReturn *= 1024; |
|
119
|
|
|
case 'K': |
|
120
|
|
|
$_nReturn *= 1024; |
|
121
|
|
|
} |
|
122
|
|
|
return $_nReturn; |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Converts bytes to HR. |
|
128
|
|
|
* |
|
129
|
|
|
* @access private |
|
130
|
|
|
* @param mixed $bytes |
|
|
|
|
|
|
131
|
|
|
* @remark This is influenced by the work of Mike Jolley. |
|
132
|
|
|
* @see http://mikejolley.com/projects/wp-page-load-stats/ |
|
133
|
|
|
*/ |
|
134
|
|
|
private function _convertBytesToHR( $nBytes ) { |
|
135
|
|
|
$_aUnits = array( 0 => 'B', 1 => 'kB', 2 => 'MB', 3 => 'GB' ); |
|
136
|
|
|
$_nLog = log( $nBytes, 1024 ); |
|
137
|
|
|
$_iPower = ( int ) $_nLog; |
|
138
|
|
|
$_iSize = pow( 1024, $_nLog - $_iPower ); |
|
139
|
|
|
return $_iSize . $_aUnits[ $_iPower ]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|