PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/include/ADODB/perf/perf-mysql.inc.php

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