我们有一家公司帮助我们在 Cakephp 1.3 中构建某种 CRM。它有多个模型/ Controller / View ,当然,并与 MySQL 数据库交互。
与公司的合作变坏了,我们刚刚意识到,在 1.6Ghz 双核 PC 上,对该服务器的“每个”请求都会导致 CPU 峰值达到 20-60% 左右 1-2 秒。我们切换到 nginx,这里的 php 进程也占用了类似的 CPU 能力(在 Windows 和 Ubuntu 系统上)。
现在,我已经浏览了代码..虽然有些页面是可以理解的单调乏味(在 Controller 中加载一个模型,以及一个带有一个“foreach”循环的 View ,其中有一个嵌套的“foreach”循环),一些像 roles_controller.php 这样的页面只有 3 个角色,只需在 View 中列出用户!我什至在 View 中禁用了“foreach”循环(注释掉所有内容),但它仍然占用那么多 CPU。
这与调度程序/路由配置有关吗?我们不能将大部分数据缓存为内部工具(出租车预订),只能提供最新数据。我已将/cake 目录重置为原始目录,但无济于事。
总而言之,我想知道这是否在所有 Cakephp 甚至 PHP 设置中都很常见..如果不是,我该如何追踪导致高 CPU 使用率的原因..?
谢谢!
最佳答案
跟踪执行函数和循环的执行时间,以确定究竟是哪一个导致了如此大的负载。一一测试。 如前所述,这肯定是由 httpd 进程下的 PHP/Apache 引起的,而不是远程连接......网络不在这个故事中。
确定瓶颈后,您可以在这里发布功能,以便我们帮助您优化。
我只是调查了 html-> 链接,似乎对性能没有影响,但在途中我看到了一些名为“clean”的函数,它过滤输出并执行大量正则表达式,我可以说这可能会导致速度下降,只取决于 html 的数量,如果我没记错的话 :) 这里的要点是 RegEx 只是性能消耗者,_output 预制了 12 个这样的正则表达式。 文件位置:cake/libs/view/helper.php 行号:880-912 函数代码:
/**
* Removes harmful content from output
*
* @return void
* @access private
*/
function __clean() {
if (get_magic_quotes_gpc()) {
$this->__cleaned = stripslashes($this->__tainted);
} else {
$this->__cleaned = $this->__tainted;
}
$this->__cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->__cleaned);
$this->__cleaned = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u', "$1;", $this->__cleaned);
$this->__cleaned = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $this->__cleaned);
$this->__cleaned = html_entity_decode($this->__cleaned, ENT_COMPAT, "UTF-8");
$this->__cleaned = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $this->__cleaned);
$this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*)[\\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2nojavascript...', $this->__cleaned);
$this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2novbscript...', $this->__cleaned);
$this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu','$1=$2nomozbinding...', $this->__cleaned);
$this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $this->__cleaned);
$this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU', "$1>", $this->__cleaned);
$this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU', "$1>", $this->__cleaned);
$this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*>#iUu', "$1>", $this->__cleaned);
$this->__cleaned = preg_replace('#</*\w+:\w[^>]*>#i', "", $this->__cleaned);
do {
$oldstring = $this->__cleaned;
$this->__cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $this->__cleaned);
} while ($oldstring != $this->__cleaned);
$this->__cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->__cleaned);
}
}
正是我想说的,在大型 html 上使用如此长的正则表达式确实会导致速度下降。
检查以上内容并回复我们,以便我们深入调查。
关于php - 每个 CakePHP 1.3 页面请求的 httpd.exe 使用率很高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8642313/