Nat*_*n H 5 php database profiling yii amazon-web-services
Yii提供了一个设置,它使用每个SQL调用的执行时间(CProfileLogRoute)来描述所有SQL调用.除了它不适用于ajax调用.我该如何访问这些数据?
我试图找到一个打开登录弹出窗口的ajax调用的瓶颈...
在类似的说明中,CProfileLogRoute中显示的执行时间是否包括到SQL服务器的网络旅行(如果有的话)?我的数据库由亚马逊的RDS托管,我想知道这是否是瓶颈所在(本地似乎很好).
您可以尝试按照下面建议的方式扩展CFileLogRoute并在应用程序的配置中启用它,如下所示:
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'ProfileLogRoute',
'levels' => "profile"
)
)
)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,所有分析的查询都将写入位于protected/runtime目录中的日志文件中(方法的重写与CProfileWebRouteprocessLogs的摘要报告类似地实现......不幸的是源自CWebLogRoute):CProfileWebRoute
<?php
class ProfileLogRoute extends CFileLogRoute
{
protected function processLogs($logs)
{
$logFile=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
if(@filesize($logFile)>$this->getMaxFileSize()*1024)
$this->rotateFiles();
$fp=@fopen($logFile,'a');
@flock($fp,LOCK_EX);
$profileStack = array();
$profileResults = array();
foreach ($logs as $log)
{
if ($log[1] === CLogger::LEVEL_PROFILE)
{
$message = $log[0];
if (!strncasecmp($message, 'begin:', 6))
{
$log[0] = substr($message,6);
$profileStack[] = $log;
}
else if(!strncasecmp($message, 'end:', 4))
{
$token = substr($message,4);
if(($last = array_pop($profileStack)) !== null && $last[0] === $token)
{
$info = array(
'delta' => $log[3] - $last[3],
'category' => $last[2],
'time' => $last[3]
);
$this->aggregateResult($token, $info, $profileResults);
}
else
{
throw new CException(Yii::t('yii','CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
array('{token}'=>$token)));
}
}
}
else
{
@fwrite($fp,$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]));
}
}
if (!empty($profileResults))
{
$now = microtime(true);
while(($last = array_pop($profileStack)) !== null)
{
$info = array(
'delta' => $now - $last[3],
'category' => $last[2],
'time' => $last[3]
);
$token = $last[0];
$this->aggregateResult($token, $info, $profileResults);
}
$entries = array_values($profileResults);
$func = create_function('$a,$b','return $a["total"]<$b["total"]?1:0;');
usort($entries, $func);
foreach ($entries as $entry)
{
$message = sprintf("Min: % 11.8f Max: % 11.8f Total: % 11.8f Calls: % 3d %s", $entry['min'], $entry['max'], $entry['total'], $entry['calls'], $entry['token']);
@fwrite($fp, $this->formatLogMessage($message, CLogger::LEVEL_PROFILE, $entry['category'], $entry['time']));
}
}
@flock($fp,LOCK_UN);
@fclose($fp);
}
protected function aggregateResult($token, $info, &$results)
{
if (isset($results[$token]))
{
if ($info['delta'] < $results[$token]['min'])
$results[$token]['min'] = $info['delta'];
else if($info['delta'] > $results[$token]['max'])
$results[$token]['max'] = $info['delta'];
$results[$token]['calls']++;
$results[$token]['total'] += $info['delta'];
return;
}
$results[$token] = array(
'token' => $token,
'calls' => 1,
'min' => $info['delta'],
'max' => $info['delta'],
'total' => $info['delta'],
'category' => $info['category'],
'time' => $info['time']
);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想知道 Yii 如何测量 SQL 执行时间,你可以查看CDbCommand类 -queryInternal方法的源代码,更准确地说。分析是在Yii::beginProfile('system.db.CDbCommand.query...')和Yii::endProfile('system.db.CDbCommand.query...')调用之间进行的。正如您所看到的,这两个调用都在同一方法内,因此分析时间不包括网络传输。问题可能是您的远程数据库服务器在巨大的负载下运行。