📄 adodb-perf-module.inc.php
字号:
if ($val < $this->warnRatio)
return '<span style="color: red;"><strong>Cache ratio should be at least '.$this->warnRatio.'%</strong></span>';
else return '';
}
/***********************************************************************************************/
// HIGH LEVEL UI FUNCTIONS
/***********************************************************************************************/
function UI($pollsecs=5)
{
$perf_table = perfmon_parent_ADOConnection::table();
$app = $this->host;
if ($this->host && $this->database) $app .= ', db=';
$app .= $this->database;
if ($app) $app .= ', ';
$savelog = $this->LogSQL(false);
$info = $this->ServerInfo();
if(isset($_GET['clearsql'])) {
$this->Execute("delete from $perf_table");
}
$this->LogSQL($savelog);
// magic quotes
if (isset($_GET['sql']) && get_magic_quotes_gpc()) {
$_GET['sql'] = $_GET['sql'] = str_replace(array("\\'",'\"'),array("'",'"'),$_GET['sql']);
}
if (!isset($_SESSION['ADODB_PERF_SQL'])) $nsql = $_SESSION['ADODB_PERF_SQL'] = 10;
else $nsql = $_SESSION['ADODB_PERF_SQL'];
$app .= $info['description'];
if(isset($_GET['do'])) { $do = $_GET['do'];
} else if (isset($_POST['do'])) $do = $_POST['do'];
else if (isset($_GET['sql'])) $do = 'viewsql';
else $do = 'stats';
if (isset($_GET['nsql'])) {
if ($_GET['nsql'] > 0) $nsql = $_SESSION['ADODB_PERF_SQL'] = (integer) $_GET['nsql'];
} // page header
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\"> <head> <title>ADOdb-Lite Performance Monitor on $app</title> <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" /> <style type=\"text/css\"> /*<![CDATA[*/ body { background-color: #FFFFFF; font-size: 10pt; color: #000000; } td { padding: 0px 3px 0px 3px; } table { border: 2px groove #000000; } /*]]>*/ </style> </head> <body> ";
if ($do == 'viewsql') { $form = "\n<form method=\"post\" action=\"". $_SERVER['PHP_SELF'] ."\"># SQL:<input type=\"hidden\" value=\"viewsql\" name=\"do\" /> <input type=\"text\" size=\"4\" name=\"nsql\" value=\"$nsql\" /><input type=\"submit\" value=\"Go\" /></form>"; }
else { $form = " "; }
$allowsql = !defined('ADODB_PERF_NO_RUN_SQL');
if(empty($_GET['hidem'])) {
echo "\n<table style=\"width: 100%; background-color: lightyellow;\">\n<tr>\n<td colspan='2'>\n<strong><a href=\"http://adodb.sourceforge.net/?perf=1\">ADOdb-Lite</a> Performance Monitor</strong> <span style=\"font-size: 8pt;\">for $app</span>\n</td>\n</tr>\n<tr>\n<td>\n<a href=\"?do=stats\"><strong>Performance Stats</strong></a> <a href=\"?do=viewsql\"><strong>View SQL</strong></a> <a href=\"?do=tables\"><strong>View Tables</strong></a> <a href=\"?do=poll\"><strong>Poll Stats</strong></a>",
$allowsql ? ' <a href="?do=dosql"><strong>Run SQL</strong></a></td>' : '</td>',
"\n<td>$form\n</td>",
"\n</tr>\n</table>\n"; }
switch ($do) {
case 'stats':
echo $this->HealthCheck();
echo $this->CheckMemory();
break;
case 'poll':
echo "<iframe width=\"720\" height=\"80%\"
src=\"{$_SERVER['PHP_SELF']}?do=poll2&hidem=1\"></iframe>";
break;
case 'poll2':
echo "<pre>";
$this->Poll($pollsecs); echo "</pre>";
break;
case 'dosql':
if (!$allowsql) break;
$this->DoSQLForm();
break;
case 'viewsql':
if (empty($_GET['hidem'])) {
echo " <a href=\"?do=viewsql&clearsql=1\">Clear SQL Log</a><br />";
} echo($this->SuspiciousSQL($nsql));
echo($this->ExpensiveSQL($nsql));
echo($this->InvalidSQL($nsql));
break;
case 'tables':
echo $this->Tables(); break; default: echo $this->HealthCheck();
echo $this->CheckMemory();
break;
}
global $ADODB_vers;
echo "<div align=\"center\"><span style=\"font-size: 8pt;\">$ADODB_vers</span></div>";
}
/*
Runs in infinite loop, returning real-time statistics
*/
function Poll($secs=5)
{
//$saveE = $this->LogSQL(false); // but how to re-enable?
if ($secs <= 1) $secs = 1;
echo "Accumulating statistics, every $secs seconds...\n"; flush();
$arro =& $this->PollParameters();
$cnt = 0;
set_time_limit(0);
sleep($secs);
while (1) {
$arr =& $this->PollParameters();
$hits = sprintf('%2.2f',$arr[0]);
$reads = sprintf('%12.4f',($arr[1]-$arro[1])/$secs);
$writes = sprintf('%12.4f',($arr[2]-$arro[2])/$secs);
$sess = sprintf('%5d',$arr[3]);
$load = $this->CPULoad();
if ($load !== false) {
$oslabel = 'WS-CPU%';
$osval = sprintf(" %2.1f ",(float) $load);
} else {
$oslabel = '';
$osval = '';
}
if ($cnt % 10 == 0) echo " Time ".$oslabel." Hit% Sess Reads/s Writes/s\n";
$cnt += 1;
echo date('H:i:s').' '.$osval."$hits $sess $reads $writes\n";
flush();
if (connection_aborted()) return;
sleep($secs);
$arro = $arr;
}
}
/*
Returns basic health check in a command line interface
*/
function HealthCheckCLI()
{
return $this->HealthCheck(true);
}
/*
Returns basic health check as HTML
*/
function HealthCheck($cli=false)
{
$saveE = $this->LogSQL(false);
if ($cli) $html = '';
else $html = $this->table.'<tr><td colspan="3"><h3>'.$this->dbtype.'</h3></td></tr>'.$this->titles;
foreach($this->settings as $name => $arr) {
if ($arr === false) break;
if (!is_string($name)) {
if ($cli) $html .= " -- $arr -- \n";
else $html .= "<tr style=\"background-color: $this->color;\"><td colspan=\"3\"><em>$arr</em> </td></tr>";
continue;
}
if (!is_array($arr)) break;
$category = $arr[0];
$how = $arr[1];
if (sizeof($arr)>2) $desc = $arr[2];
else $desc = ' ';
if ($category == 'HIDE') continue;
$val = $this->_DBParameter($how);
if ($desc && strncmp($desc,"=",1) === 0) {
$fn = substr($desc,1);
$desc = $this->$fn($val);
}
if ($val === false) {
$m = $this->ErrorMsg();
$val = "Error: $m";
} else {
if (is_numeric($val) && $val >= 256*1024) {
if ($val % (1024*1024) == 0) {
$val /= (1024*1024);
$val .= 'M';
} else if ($val % 1024 == 0) {
$val /= 1024;
$val .= 'K';
}
}
}
if ($category != $oldc) {
$oldc = $category;
}
if (strlen($desc)==0) $desc = ' ';
if (strlen($val)==0) $val = ' ';
if ($cli) {
$html .= str_replace(' ','',sprintf($this->cliFormat,strip_tags($name),strip_tags($val),strip_tags($desc)));
} else {
$html .= "<tr><td>".$name.'</td><td>'.$val.'</td><td>'.$desc."</td></tr>\n";
}
}
if (!$cli) $html .= "</table>\n";
$this->LogSQL($saveE);
return $html;
}
function Tables($orderby='1')
{
if (!$this->tablesSQL) return false;
$savelog = $this->LogSQL(false);
$rs = $this->Execute($this->tablesSQL.' order by '.$orderby);
$this->LogSQL($savelog);
$html = rs2html($rs,false,false,false,false);
return $html;
}
function CreateLogTable()
{
if (!$this->createTableSQL) return false;
$savelog = $this->LogSQL(false);
$ok = $this->Execute($this->createTableSQL);
$this->LogSQL($savelog);
return ($ok) ? true : false;
}
function DoSQLForm()
{
$PHP_SELF = $_SERVER['PHP_SELF'];
$sql = isset($_REQUEST['sql']) ? $_REQUEST['sql'] : ''; // Let the form spoofing commence... ***
if (isset($_SESSION['phplens_sqlrows'])) $rows = $_SESSION['phplens_sqlrows'];
else $rows = 3;
if (isset($_REQUEST['SMALLER'])) {
$rows /= 2;
if ($rows < 3) $rows = 3;
$_SESSION['phplens_sqlrows'] = $rows;
}
if (isset($_REQUEST['BIGGER'])) {
$rows *= 2;
$_SESSION['phplens_sqlrows'] = $rows;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table><tr>
<td> Form size: <input type="submit" value=" < " name="SMALLER" /><input type="submit" value=" > > " name="BIGGER" />
</td>
<td align=right>
<input type="submit" value=" Run SQL Below " name="RUN" /><input type="hidden" name="do" value="dosql" />
</td></tr>
<tr>
<td colspan="2"><textarea rows="<?php print $rows; ?>" name="sql" cols="80"><?php print htmlentities($sql, ENT_QUOTES, 'UTF-8'); ?></textarea>
</td>
</tr>
</table>
</form>
<?php
if (!isset($_REQUEST['sql'])) return;
$sql = $this->undomq(trim($sql));
if (substr($sql,strlen($sql)-1) === ';') {
$print = true;
$sqla = $this->SplitSQL($sql);
} else {
$print = false;
$sqla = array($sql);
}
foreach($sqla as $sqls) {
if (!$sqls) continue;
if ($print) {
print "<p>".htmlentities($sqls, ENT_QUOTES, 'UTF-8')."</p>";
flush();
}
$savelog = $this->LogSQL(false);
$rs = $this->Execute($sqls);
$this->LogSQL($savelog);
if ($rs && is_object($rs) && !$rs->EOF) {
rs2html($rs);
while ($rs->NextRecordSet()) {
print "<table style=\"width: 98%; background-color: #C0C0FF;\"><tr><td> </td></tr></table>";
rs2html($rs);
}
} else {
$e1 = (integer) $this->ErrorNo();
$e2 = $this->ErrorMsg();
if (($e1) || ($e2)) {
if (empty($e1)) $e1 = '-1'; // postgresql fix
print ' '.$e1.': '.$e2;
} else {
print "<p>No Recordset returned<br /></p>";
}
}
} // foreach
}
function SplitSQL($sql)
{
$arr = explode(';',$sql);
return $arr;
}
function undomq(&$m)
{
if (get_magic_quotes_gpc()) {
// undo the damage
$m = str_replace('\\\\','\\',$m);
$m = str_replace('\"','"',$m);
$m = str_replace('\\\'','\'',$m);
}
return $m;
}
/************************************************************************/
/**
* Reorganise multiple table-indices/statistics/..
* OptimizeMode could be given by last Parameter
*
* @example
* <pre>
* optimizeTables( 'tableA');
* </pre>
* <pre>
* optimizeTables( 'tableA', 'tableB', 'tableC');
* </pre>
* <pre>
* optimizeTables( 'tableA', 'tableB', ADODB_OPT_LOW);
* </pre>
*
* @param string table name of the table to optimize
* @param int mode optimization-mode
* <code>ADODB_OPT_HIGH</code> for full optimization
* <code>ADODB_OPT_LOW</code> for CPU-less optimization
* Default is LOW <code>ADODB_OPT_LOW</code>
* @author Markus Staab
* @return Returns <code>true</code> on success and <code>false</code> on error
*/
function OptimizeTables()
{
$args = func_get_args();
$numArgs = func_num_args();
if ( $numArgs == 0) return false;
$mode = ADODB_OPT_LOW;
$lastArg = $args[ $numArgs - 1];
if ( !is_string($lastArg)) {
$mode = $lastArg;
unset( $args[ $numArgs - 1]);
}
foreach( $args as $table) {
$this->optimizeTable( $table, $mode);
}
}
/**
* Reorganise the table-indices/statistics/.. depending on the given mode.
* Default Implementation throws an error.
*
* @param string table name of the table to optimize
* @param int mode optimization-mode
* <code>ADODB_OPT_HIGH</code> for full optimization
* <code>ADODB_OPT_LOW</code> for CPU-less optimization
* Default is LOW <code>ADODB_OPT_LOW</code>
* @author Markus Staab
* @return Returns <code>true</code> on success and <code>false</code> on error
*/
function OptimizeTable( $table, $mode = ADODB_OPT_LOW)
{
$this->outp( sprintf( "<p>%s: '%s' not implemented for driver '%s'</p>", __CLASS__, __FUNCTION__, $this->dbtype));
return false;
}
/**
* Reorganise current database.
* Default implementation loops over all <code>MetaTables()</code> and
* optimize each using <code>optmizeTable()</code> * * Non-functional in ADOdb-Lite due to lack of MetaTables() implementation in default modules
*
* @author Markus Staab
* @return Returns <code>true</code> on success and <code>false</code> on error
*/
function optimizeDatabase()
{
//$tables = $this->MetaTables( 'TABLES'); // non-functional without a MetaTables method in ADOdb-Lite...
if ( !$tables ) return false;
foreach( $tables as $table) {
if (!$this->optimizeTable( $table)) {
return false;
}
}
return true;
}
}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -