[2] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | V4.51 29 July 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved. |
---|
| 4 | Released under both BSD license and Lesser GPL library license. |
---|
| 5 | Whenever there is any discrepancy between the two licenses, |
---|
| 6 | the BSD license will take precedence. See License.txt. |
---|
| 7 | Set tabs to 4 for best viewing. |
---|
| 8 | |
---|
| 9 | Latest version is available at http://adodb.sourceforge.net |
---|
| 10 | |
---|
| 11 | Library for basic performance monitoring and tuning |
---|
| 12 | |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | // security - hide paths |
---|
| 16 | if (!defined('ADODB_DIR')) die(); |
---|
| 17 | |
---|
| 18 | class perf_mysql extends adodb_perf{ |
---|
| 19 | |
---|
| 20 | var $tablesSQL = 'show table status'; |
---|
| 21 | |
---|
| 22 | var $createTableSQL = "CREATE TABLE adodb_logsql ( |
---|
| 23 | created datetime NOT NULL, |
---|
| 24 | sql0 varchar(250) NOT NULL, |
---|
| 25 | sql1 text NOT NULL, |
---|
| 26 | params text NOT NULL, |
---|
| 27 | tracer text NOT NULL, |
---|
| 28 | timer decimal(16,6) NOT NULL |
---|
| 29 | )"; |
---|
| 30 | |
---|
| 31 | var $settings = array( |
---|
| 32 | 'Ratios', |
---|
| 33 | 'MyISAM cache hit ratio' => array('RATIO', |
---|
| 34 | '=GetKeyHitRatio', |
---|
| 35 | '=WarnCacheRatio'), |
---|
| 36 | 'InnoDB cache hit ratio' => array('RATIO', |
---|
| 37 | '=GetInnoDBHitRatio', |
---|
| 38 | '=WarnCacheRatio'), |
---|
| 39 | 'data cache hit ratio' => array('HIDE', # only if called |
---|
| 40 | '=FindDBHitRatio', |
---|
| 41 | '=WarnCacheRatio'), |
---|
| 42 | 'sql cache hit ratio' => array('RATIO', |
---|
| 43 | '=GetQHitRatio', |
---|
| 44 | ''), |
---|
| 45 | 'IO', |
---|
| 46 | 'data reads' => array('IO', |
---|
| 47 | '=GetReads', |
---|
| 48 | 'Number of selects (Key_reads is not accurate)'), |
---|
| 49 | 'data writes' => array('IO', |
---|
| 50 | '=GetWrites', |
---|
| 51 | 'Number of inserts/updates/deletes * coef (Key_writes is not accurate)'), |
---|
| 52 | |
---|
| 53 | 'Data Cache', |
---|
| 54 | 'MyISAM data cache size' => array('DATAC', |
---|
| 55 | array("show variables", 'key_buffer_size'), |
---|
| 56 | '' ), |
---|
| 57 | 'BDB data cache size' => array('DATAC', |
---|
| 58 | array("show variables", 'bdb_cache_size'), |
---|
| 59 | '' ), |
---|
| 60 | 'InnoDB data cache size' => array('DATAC', |
---|
| 61 | array("show variables", 'innodb_buffer_pool_size'), |
---|
| 62 | '' ), |
---|
| 63 | 'Memory Usage', |
---|
| 64 | 'read buffer size' => array('CACHE', |
---|
| 65 | array("show variables", 'read_buffer_size'), |
---|
| 66 | '(per session)'), |
---|
| 67 | 'sort buffer size' => array('CACHE', |
---|
| 68 | array("show variables", 'sort_buffer_size'), |
---|
| 69 | 'Size of sort buffer (per session)' ), |
---|
| 70 | 'table cache' => array('CACHE', |
---|
| 71 | array("show variables", 'table_cache'), |
---|
| 72 | 'Number of tables to keep open'), |
---|
| 73 | 'Connections', |
---|
| 74 | 'current connections' => array('SESS', |
---|
| 75 | array('show status','Threads_connected'), |
---|
| 76 | ''), |
---|
| 77 | 'max connections' => array( 'SESS', |
---|
| 78 | array("show variables",'max_connections'), |
---|
| 79 | ''), |
---|
| 80 | |
---|
| 81 | false |
---|
| 82 | ); |
---|
| 83 | |
---|
| 84 | function perf_mysql(&$conn) |
---|
| 85 | { |
---|
| 86 | $this->conn =& $conn; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | function Explain($sql,$partial=false) |
---|
| 90 | { |
---|
| 91 | |
---|
| 92 | if (strtoupper(substr(trim($sql),0,6)) !== 'SELECT') return '<p>Unable to EXPLAIN non-select statement</p>'; |
---|
| 93 | $save = $this->conn->LogSQL(false); |
---|
| 94 | if ($partial) { |
---|
| 95 | $sqlq = $this->conn->qstr($sql.'%'); |
---|
| 96 | $arr = $this->conn->GetArray("select distinct sql1 from adodb_logsql where sql1 like $sqlq"); |
---|
| 97 | if ($arr) { |
---|
| 98 | foreach($arr as $row) { |
---|
| 99 | $sql = reset($row); |
---|
| 100 | if (crc32($sql) == $partial) break; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | $sql = str_replace('?',"''",$sql); |
---|
| 105 | |
---|
| 106 | if ($partial) { |
---|
| 107 | $sqlq = $this->conn->qstr($sql.'%'); |
---|
| 108 | $sql = $this->conn->GetOne("select sql1 from adodb_logsql where sql1 like $sqlq"); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | $s = '<p><b>Explain</b>: '.htmlspecialchars($sql).'</p>'; |
---|
| 112 | $rs = $this->conn->Execute('EXPLAIN '.$sql); |
---|
| 113 | $s .= rs2html($rs,false,false,false,false); |
---|
| 114 | $this->conn->LogSQL($save); |
---|
| 115 | $s .= $this->Tracer($sql); |
---|
| 116 | return $s; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | function Tables() |
---|
| 120 | { |
---|
| 121 | if (!$this->tablesSQL) return false; |
---|
| 122 | |
---|
| 123 | $rs = $this->conn->Execute($this->tablesSQL); |
---|
| 124 | if (!$rs) return false; |
---|
| 125 | |
---|
| 126 | $html = rs2html($rs,false,false,false,false); |
---|
| 127 | return $html; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | function GetReads() |
---|
| 131 | { |
---|
| 132 | global $ADODB_FETCH_MODE; |
---|
| 133 | $save = $ADODB_FETCH_MODE; |
---|
| 134 | $ADODB_FETCH_MODE = ADODB_FETCH_NUM; |
---|
| 135 | $rs = $this->conn->Execute('show status'); |
---|
| 136 | $ADODB_FETCH_MODE = $save; |
---|
| 137 | |
---|
| 138 | if (!$rs) return 0; |
---|
| 139 | $val = 0; |
---|
| 140 | while (!$rs->EOF) { |
---|
| 141 | switch($rs->fields[0]) { |
---|
| 142 | case 'Com_select': |
---|
| 143 | $val = $rs->fields[1]; |
---|
| 144 | $rs->Close(); |
---|
| 145 | return $val; |
---|
| 146 | } |
---|
| 147 | $rs->MoveNext(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | $rs->Close(); |
---|
| 151 | |
---|
| 152 | return $val; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | function GetWrites() |
---|
| 156 | { |
---|
| 157 | global $ADODB_FETCH_MODE; |
---|
| 158 | $save = $ADODB_FETCH_MODE; |
---|
| 159 | $ADODB_FETCH_MODE = ADODB_FETCH_NUM; |
---|
| 160 | $rs = $this->conn->Execute('show status'); |
---|
| 161 | $ADODB_FETCH_MODE = $save; |
---|
| 162 | |
---|
| 163 | if (!$rs) return 0; |
---|
| 164 | $val = 0.0; |
---|
| 165 | while (!$rs->EOF) { |
---|
| 166 | switch($rs->fields[0]) { |
---|
| 167 | case 'Com_insert': |
---|
| 168 | $val += $rs->fields[1]; break; |
---|
| 169 | case 'Com_delete': |
---|
| 170 | $val += $rs->fields[1]; break; |
---|
| 171 | case 'Com_update': |
---|
| 172 | $val += $rs->fields[1]/2; |
---|
| 173 | $rs->Close(); |
---|
| 174 | return $val; |
---|
| 175 | } |
---|
| 176 | $rs->MoveNext(); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | $rs->Close(); |
---|
| 180 | |
---|
| 181 | return $val; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | function FindDBHitRatio() |
---|
| 185 | { |
---|
| 186 | // first find out type of table |
---|
| 187 | //$this->conn->debug=1; |
---|
| 188 | $rs = $this->conn->Execute('show table status'); |
---|
| 189 | if (!$rs) return ''; |
---|
| 190 | $type = strtoupper($rs->fields[1]); |
---|
| 191 | $rs->Close(); |
---|
| 192 | switch($type){ |
---|
| 193 | case 'MYISAM': |
---|
| 194 | case 'ISAM': |
---|
| 195 | return $this->DBParameter('MyISAM cache hit ratio').' (MyISAM)'; |
---|
| 196 | case 'INNODB': |
---|
| 197 | return $this->DBParameter('InnoDB cache hit ratio').' (InnoDB)'; |
---|
| 198 | default: |
---|
| 199 | return $type.' not supported'; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | function GetQHitRatio() |
---|
| 205 | { |
---|
| 206 | //Total number of queries = Qcache_inserts + Qcache_hits + Qcache_not_cached |
---|
| 207 | $hits = $this->_DBParameter(array("show status","Qcache_hits")); |
---|
| 208 | $total = $this->_DBParameter(array("show status","Qcache_inserts")); |
---|
| 209 | $total += $this->_DBParameter(array("show status","Qcache_not_cached")); |
---|
| 210 | |
---|
| 211 | $total += $hits; |
---|
| 212 | if ($total) return ($hits*100)/$total; |
---|
| 213 | return 0; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /* |
---|
| 217 | Use session variable to store Hit percentage, because MySQL |
---|
| 218 | does not remember last value of SHOW INNODB STATUS hit ratio |
---|
| 219 | |
---|
| 220 | # 1st query to SHOW INNODB STATUS |
---|
| 221 | 0.00 reads/s, 0.00 creates/s, 0.00 writes/s |
---|
| 222 | Buffer pool hit rate 1000 / 1000 |
---|
| 223 | |
---|
| 224 | # 2nd query to SHOW INNODB STATUS |
---|
| 225 | 0.00 reads/s, 0.00 creates/s, 0.00 writes/s |
---|
| 226 | No buffer pool activity since the last printout |
---|
| 227 | */ |
---|
| 228 | function GetInnoDBHitRatio() |
---|
| 229 | { |
---|
| 230 | global $HTTP_SESSION_VARS; |
---|
| 231 | |
---|
| 232 | $rs = $this->conn->Execute('show innodb status'); |
---|
| 233 | if (!$rs || $rs->EOF) return 0; |
---|
| 234 | $stat = $rs->fields[0]; |
---|
| 235 | $rs->Close(); |
---|
| 236 | $at = strpos($stat,'Buffer pool hit rate'); |
---|
| 237 | $stat = substr($stat,$at,200); |
---|
| 238 | if (preg_match('!Buffer pool hit rate\s*([0-9]*) / ([0-9]*)!',$stat,$arr)) { |
---|
| 239 | $val = 100*$arr[1]/$arr[2]; |
---|
| 240 | $HTTP_SESSION_VARS['INNODB_HIT_PCT'] = $val; |
---|
| 241 | return $val; |
---|
| 242 | } else { |
---|
| 243 | if (isset($HTTP_SESSION_VARS['INNODB_HIT_PCT'])) return $HTTP_SESSION_VARS['INNODB_HIT_PCT']; |
---|
| 244 | return 0; |
---|
| 245 | } |
---|
| 246 | return 0; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | function GetKeyHitRatio() |
---|
| 250 | { |
---|
| 251 | $hits = $this->_DBParameter(array("show status","Key_read_requests")); |
---|
| 252 | $reqs = $this->_DBParameter(array("show status","Key_reads")); |
---|
| 253 | if ($reqs == 0) return 0; |
---|
| 254 | |
---|
| 255 | return ($hits/($reqs+$hits))*100; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | } |
---|
| 259 | ?> |
---|