?? adodb-perf.inc.php
字號:
<?php/* V4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. See License.txt. Set tabs to 4 for best viewing. Latest version is available at http://adodb.sourceforge.net Library for basic performance monitoring and tuning. My apologies if you see code mixed with presentation. The presentation suits my needs. If you want to separate code from presentation, be my guest. Patches are welcome. */if (!defined('ADODB_DIR')) include_once(dirname(__FILE__).'/adodb.inc.php');include_once(ADODB_DIR.'/tohtml.inc.php');define( 'ADODB_OPT_HIGH', 2);define( 'ADODB_OPT_LOW', 1);// returns in K the memory of current process, or 0 if not knownfunction adodb_getmem(){ if (function_exists('memory_get_usage')) return (integer) ((memory_get_usage()+512)/1024); $pid = getmypid(); if ( strncmp(strtoupper(PHP_OS),'WIN',3)==0) { $output = array(); exec('tasklist /FI "PID eq ' . $pid. '" /FO LIST', $output); return substr($output[5], strpos($output[5], ':') + 1); } /* Hopefully UNIX */ exec("ps --pid $pid --no-headers -o%mem,size", $output); if (sizeof($output) == 0) return 0; $memarr = explode(' ',$output[0]); if (sizeof($memarr)>=2) return (integer) $memarr[1]; return 0;}// avoids localization problems where , is used instead of .function adodb_round($n,$prec){ return number_format($n, $prec, '.', '');}/* return microtime value as a float */function adodb_microtime(){ $t = microtime(); $t = explode(' ',$t); return (float)$t[1]+ (float)$t[0];}/* sql code timing */function& adodb_log_sql(&$connx,$sql,$inputarr){ $perf_table = adodb_perf::table(); $connx->fnExecute = false; $t0 = microtime(); $rs =& $connx->Execute($sql,$inputarr); $t1 = microtime(); if (!empty($connx->_logsql) && (empty($connx->_logsqlErrors) || !$rs)) { global $ADODB_LOG_CONN; if (!empty($ADODB_LOG_CONN)) { $conn = &$ADODB_LOG_CONN; if ($conn->databaseType != $connx->databaseType) $prefix = '/*dbx='.$connx->databaseType .'*/ '; else $prefix = ''; } else { $conn =& $connx; $prefix = ''; } $conn->_logsql = false; // disable logsql error simulation $dbT = $conn->databaseType; $a0 = split(' ',$t0); $a0 = (float)$a0[1]+(float)$a0[0]; $a1 = split(' ',$t1); $a1 = (float)$a1[1]+(float)$a1[0]; $time = $a1 - $a0; if (!$rs) { $errM = $connx->ErrorMsg(); $errN = $connx->ErrorNo(); $conn->lastInsID = 0; $tracer = substr('ERROR: '.htmlspecialchars($errM),0,250); } else { $tracer = ''; $errM = ''; $errN = 0; $dbg = $conn->debug; $conn->debug = false; if (!is_object($rs) || $rs->dataProvider == 'empty') $conn->_affected = $conn->affected_rows(true); $conn->lastInsID = @$conn->Insert_ID(); $conn->debug = $dbg; } if (isset($_SERVER['HTTP_HOST'])) { $tracer .= '<br>'.$_SERVER['HTTP_HOST']; if (isset($_SERVER['PHP_SELF'])) $tracer .= $_SERVER['PHP_SELF']; } else if (isset($_SERVER['PHP_SELF'])) $tracer .= '<br>'.$_SERVER['PHP_SELF']; //$tracer .= (string) adodb_backtrace(false); $tracer = (string) substr($tracer,0,500); if (is_array($inputarr)) { if (is_array(reset($inputarr))) $params = 'Array sizeof='.sizeof($inputarr); else { // Quote string parameters so we can see them in the // performance stats. This helps spot disabled indexes. $xar_params = $inputarr; foreach ($xar_params as $xar_param_key => $xar_param) { if (gettype($xar_param) == 'string') $xar_params[$xar_param_key] = '"' . $xar_param . '"'; } $params = implode(', ', $xar_params); if (strlen($params) >= 3000) $params = substr($params, 0, 3000); } } else { $params = ''; } if (is_array($sql)) $sql = $sql[0]; if ($prefix) $sql = $prefix.$sql; $arr = array('b'=>strlen($sql).'.'.crc32($sql), 'c'=>substr($sql,0,3900), 'd'=>$params,'e'=>$tracer,'f'=>adodb_round($time,6)); //var_dump($arr); $saved = $conn->debug; $conn->debug = 0; $d = $conn->sysTimeStamp; if (empty($d)) $d = date("'Y-m-d H:i:s'"); if ($conn->dataProvider == 'oci8' && $dbT != 'oci8po') { $isql = "insert into $perf_table values($d,:b,:c,:d,:e,:f)"; } else if ($dbT == 'odbc_mssql' || $dbT == 'informix' || strncmp($dbT,'odbtp',4)==0) { $timer = $arr['f']; if ($dbT == 'informix') $sql2 = substr($sql2,0,230); $sql1 = $conn->qstr($arr['b']); $sql2 = $conn->qstr($arr['c']); $params = $conn->qstr($arr['d']); $tracer = $conn->qstr($arr['e']); $isql = "insert into $perf_table (created,sql0,sql1,params,tracer,timer) values($d,$sql1,$sql2,$params,$tracer,$timer)"; if ($dbT == 'informix') $isql = str_replace(chr(10),' ',$isql); $arr = false; } else { if ($dbT == 'db2') $arr['f'] = (float) $arr['f']; $isql = "insert into $perf_table (created,sql0,sql1,params,tracer,timer) values( $d,?,?,?,?,?)"; } $ok = $conn->Execute($isql,$arr); $conn->debug = $saved; if ($ok) { $conn->_logsql = true; } else { $err2 = $conn->ErrorMsg(); $conn->_logsql = true; // enable logsql error simulation $perf =& NewPerfMonitor($conn); if ($perf) { if ($perf->CreateLogTable()) $ok = $conn->Execute($isql,$arr); } else { $ok = $conn->Execute("create table $perf_table ( created varchar(50), sql0 varchar(250), sql1 varchar(4000), params varchar(3000), tracer varchar(500), timer decimal(16,6))"); } if (!$ok) { ADOConnection::outp( "<p><b>LOGSQL Insert Failed</b>: $isql<br>$err2</p>"); $conn->_logsql = false; } } $connx->_errorMsg = $errM; $connx->_errorCode = $errN; } $connx->fnExecute = 'adodb_log_sql'; return $rs;} /*The settings data structure is an associative array that database parameter per element.Each database parameter element in the array is itself an array consisting of:0: category code, used to group related db parameters1: either a. sql string to retrieve value, eg. "select value from v\$parameter where name='db_block_size'", b. array holding sql string and field to look for, e.g. array('show variables','table_cache'), c. a string prefixed by =, then a PHP method of the class is invoked, e.g. to invoke $this->GetIndexValue(), set this array element to '=GetIndexValue',2: description of the database parameter*/class adodb_perf { var $conn; var $color = '#F0F0F0'; var $table = '<table border=1 bgcolor=white>'; var $titles = '<tr><td><b>Parameter</b></td><td><b>Value</b></td><td><b>Description</b></td></tr>'; var $warnRatio = 90; var $tablesSQL = false; var $cliFormat = "%32s => %s \r\n"; var $sql1 = 'sql1'; // used for casting sql1 to text for mssql var $explain = true; var $helpurl = "<a href=http://phplens.com/adodb/reference.functions.fnexecute.and.fncacheexecute.properties.html#logsql>LogSQL help</a>"; var $createTableSQL = false; var $maxLength = 2000; // Sets the tablename to be used function table($newtable = false) { static $_table; if (!empty($newtable)) $_table = $newtable; if (empty($_table)) $_table = 'adodb_logsql'; return $_table; } // returns array with info to calculate CPU Load function _CPULoad() {/*cpu 524152 2662 2515228 336057010cpu0 264339 1408 1257951 168025827cpu1 259813 1254 1257277 168031181page 622307 25475680swap 24 1891intr 890153570 868093576 6 0 4 4 0 6 1 2 0 0 0 124 0 8098760 2 13961053 0 0 0 0 0 0 0 0 0 0 0 0 0 16 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0disk_io: (3,0):(3144904,54369,610378,3090535,50936192) (3,1):(3630212,54097,633016,3576115,50951320)ctxt 66155838btime 1062315585processes 69293*/ // Algorithm is taken from // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__obtaining_raw_performance_data.asp if (strncmp(PHP_OS,'WIN',3)==0) { if (PHP_VERSION == '5.0.0') return false; if (PHP_VERSION == '5.0.1') return false; if (PHP_VERSION == '5.0.2') return false; if (PHP_VERSION == '5.0.3') return false; if (PHP_VERSION == '4.3.10') return false; # see http://bugs.php.net/bug.php?id=31737 @$c = new COM("WinMgmts:{impersonationLevel=impersonate}!Win32_PerfRawData_PerfOS_Processor.Name='_Total'"); if (!$c) return false; $info[0] = $c->PercentProcessorTime; $info[1] = 0; $info[2] = 0; $info[3] = $c->TimeStamp_Sys100NS; //print_r($info); return $info; } // Algorithm - Steve Blinch (BlitzAffe Online, http://www.blitzaffe.com) $statfile = '/proc/stat'; if (!file_exists($statfile)) return false; $fd = fopen($statfile,"r"); if (!$fd) return false; $statinfo = explode("\n",fgets($fd, 1024)); fclose($fd); foreach($statinfo as $line) { $info = explode(" ",$line); if($info[0]=="cpu") { array_shift($info); // pop off "cpu" if(!$info[0]) array_shift($info); // pop off blank space (if any) return $info; } } return false; } /* NOT IMPLEMENTED */ function MemInfo() { /* total: used: free: shared: buffers: cached:Mem: 1055289344 917299200 137990144 0 165437440 599773184Swap: 2146775040 11055104 2135719936MemTotal: 1030556 kBMemFree: 134756 kBMemShared: 0 kBBuffers: 161560 kBCached: 581384 kBSwapCached: 4332 kBActive: 494468 kBInact_dirty: 322856 kBInact_clean: 24256 kBInact_target: 168316 kBHighTotal: 131064 kBHighFree: 1024 kBLowTotal: 899492 kBLowFree: 133732 kBSwapTotal: 2096460 kBSwapFree: 2085664 kBCommitted_AS: 348732 kB */ } /* Remember that this is client load, not db server load! */ var $_lastLoad; function CPULoad() { $info = $this->_CPULoad(); if (!$info) return false; if (empty($this->_lastLoad)) { sleep(1); $this->_lastLoad = $info; $info = $this->_CPULoad(); } $last = $this->_lastLoad; $this->_lastLoad = $info; $d_user = $info[0] - $last[0]; $d_nice = $info[1] - $last[1]; $d_system = $info[2] - $last[2]; $d_idle = $info[3] - $last[3]; //printf("Delta - User: %f Nice: %f System: %f Idle: %f<br>",$d_user,$d_nice,$d_system,$d_idle); if (strncmp(PHP_OS,'WIN',3)==0) { if ($d_idle < 1) $d_idle = 1; return 100*(1-$d_user/$d_idle); }else { $total=$d_user+$d_nice+$d_system+$d_idle;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -