我和一个朋友刚刚开始从事一个项目,其他人几年前就停止了开发,我们正在努力恢复它。我们已经解决了大部分与设置相关的问题,但有一个非常烦人的问题我们无法解决。
在我们的本地主机中,所有页面都占用A LOT加载/刷新的时间。我指的不是资产、脚本或任何东西,问题是第一个请求完成之前的延迟。大多数时候需要15到30秒,这是不可接受的,有时甚至会长达1或2分钟。
例如,下面是 Chrome 开发工具中“网络”选项卡的屏幕截图。第一行是视图,其他行是资产。
我们在谷歌上搜索了几个小时并尝试了一些不同的方法,但没有一个起作用。一些解决方案如this one https://serverfault.com/a/349618指向一些 Apache 的httpd.conf
设置,但我放弃了它,因为我在其他项目中使用相同的服务器,并且这种情况从未发生过(无论如何我都尝试过,但没有成功)。其他人指出 PHP 版本冲突,所以我尝试将 MAMP 中的 PHP 从 5.4.10 更改为 5.2.17(该项目需要 5.2.3+),但这似乎也不起作用。
除了我的 MAMP 安装之外,我们还在带有 WAMP (PHP5.5) 的 Windows 计算机中测试了它,并且还在另一台带有干净的 MAMP (PHP5.5) 的 Mac 中测试了它,并且在这两种环境中都会发生相同的情况。因此,我们现在想知道问题是否可能出在 CodeIgniter 本身(这听起来不太可能)或某些项目配置中,但我们对 CodeIgniter 还很陌生(也不是 PHP 专家),找不到任何东西。
哦,我们也尝试联系最初的开发者,但他们说那是两年前的事,听起来他们不愿意提供帮助。我真的希望他们当时开发项目时没有遇到这个问题,因为加载时间为 30 秒,这太疯狂了。
有人有任何想法或了解更多我们可以尝试找到问题的事情吗?如果需要的话我可以发布一些代码。
Update:我刚刚发现这个未解决的问题 https://stackoverflow.com/q/23673578/2999215用户在使用 Laravel 时遇到了类似的问题,但只是有时。正如我所说,就我而言,这种情况发生了always,延迟时间从约 10 秒到几分钟不等。
更新2:正如 Wrikken 所建议的,我通过 xdebug 分析器运行了它,但我不确定如何解释结果以了解问题所在。我使用 PHPStorm 的“Analyze Xdebug Profiler”工具打开了一个快照,并按每次调用所用的时间对其进行了排序。以下是一些屏幕截图:
并按自己的时间排序:
That CashewModel
在某些行中出现的是由以前的开发人员构建的某种自定义库,这也导致了我们已经解决的一些问题。我希望问题没有隐藏在那里,因为我不知道大多数自定义代码是如何工作的。
有任何想法吗?再说一遍,如果需要的话我可以发布代码。
更新3:深入研究代码,发现MY_Controller
在上面的屏幕截图中是一个文件,以前的开发人员在其中创建了一些自定义控制器,扩展CI_Controller
。我刚刚发现他们把所有的 Cashew 代码都推到了 GitHub 上,这是 MY_Controller 文件 https://github.com/cubledesarrollo/cashew/blob/master/application/core/MY_Controller.php.
我还将在此处粘贴第 467 行周围的所有相关代码(在 GitHub 的版本中是 464),其中涉及_remap
里面的函数CashewController
并且是分析器所说的所有时间都花在的地方。我把一些评论和名字翻译成英文。
/**
*
* Extension of the default controller, adding support for templates
*
* Usage example:
*
* class Dummy extends EC_Controller
* {
* public function index()
* {
* $this->add_section('id_in_template', 'page_name');
* $this->render_page(); // Renders the default template.
* }
* }
*
*/
class CashewController extends CI_Controller
{
//
// Some attributes here
//
function __construct() { ... }
/**
* We use this _remap to automatically create the CRUD method calls
*
* @param string $method
* @param string $params
*/
public function _remap($method, $params = array())
{
// NEW
if ($method == 'new') {
$method = '_new';
}
// CREATE
else if ($method == 'index' && $this->request_method() == 'post') {
$method = '_create';
}
else if (is_numeric($method) && $this->request_method() == 'post' && count($params) == 0) {
$params[0] = $method;
$method = '_create';
}
// SHOW
else if (is_numeric($method) && count($params) == 0) {
$params[0] = $method;
$method = '_show';
}
else if (is_numeric($method) && count($params) == 1 && $params[0] == 'edit') {
// EDIT
if ($this->request_method() == 'get') {
$params[0] = $method;
$method = '_edit';
}
// UPDATE
else if ($this->request_method() == 'post') {
$params[0] = $method;
$method = '_update';
}
}
// DELETE
else if (is_numeric($method) && count($params) == 1 && $params[0] == 'delete') {
$params[0] = $method;
$method = '_delete';
}
if (method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $params);
}
show_404();
}
//
// Some more functions
//
}
所以里面发生了一些事情call_user_func_array(array($this, $method), $params)
, right?