A friend and I just started working in a project which other people stopped developing a couple of years ago, and we're trying to resurrect it. We've already solved most of the setup-related issues, but there's a really annoying one that we can't figure out.
In our localhosts, all the pages take A LOT of time to load/refresh. And I don't mean assets, scripts or anything, the problem is the latency until the first request completes. Most times it takes 15 to 30 seconds, which is unacceptable, and sometimes it even goes up to 1 or 2 minutes.
For example, here's a screenshot of the Network tab in Chrome dev tools. The first row is the view, the other ones are assets.
We've googled for hours and tried a few different things, but none of them has worked. Some solutions like this one point to some Apache's httpd.conf
settings, but I discarded that since I'm using the same server for other projects and this never happened (I tried it anyway, but didn't work). Others point to PHP version conflicts, so I tried changing the PHP in my MAMP from 5.4.10 to 5.2.17 (the project requires 5.2.3+), but that didn't seem to work either.
Apart from my MAMP installation, we also tested it in a Windows machine with WAMP (PHP5.5), and also in another Mac with a clean MAMP (PHP5.5), and the same thing happens in both environments. So, we are now wondering if the problem could be in CodeIgniter itself (which sounds unlikely) or in some project configuration, but we're pretty new to CodeIgniter (and also not PHP experts) and couldn't find anything.
Oh, and we also tried contacting the original developers, but they said that was two years ago and sounded like they're unwilling to help. I really hope they didn't have this issue while developing the project back then, because working with 30sec load times it's just insane.
Someone have any idea or know about something more we could try to find the issue? I could post some code if needed.
Update: I just found this unsolved question where a user experienced a similar issue with Laravel, but only sometimes. As I said, in my case this happens always, with latency times spanning from ~10 seconds to a few minutes.
Update 2: As suggested by Wrikken, i ran it through an xdebug profiler, but I'm not sure of how to interpret the results to see where the issue is. I opened a snapshot with PHPStorm's "Analyze Xdebug Profiler" tool, and sorted it by time used in each call. Here are a couple of screenshots:
And sorted by Own Time:
That CashewModel
showing up in some lines is a some sort of custom library built by the previous developers, which was also causing some problems we already solved. I hope the problem isn't hidden there, because I have no idea of how most of that custom code works.
Any ideas? Again, I can post code if needed.
Update 3: Digging into the code, that MY_Controller
in the screenshot above is a file where the previous developers created some custom controllers extending CI_Controller
. I just found out that they pushed all the Cashew code to GitHub, here's the MY_Controller file.
I'll also paste here all the relevant code around line 467 (in GitHub's version is 464), which involves the _remap
function inside the CashewController
and is where the profiler says all the time is spent. I translated some comments and names into English.
/**
*
* 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
//
}
So something's happening inside that call_user_func_array(array($this, $method), $params)
, right?
I found the issue thanks to a comment posted above but the user didn't write an answer, so I'm posting it here.
The code written by the previous developers is making a pretty intensive use of the memcached
extension, which I never used before, so I didn't know what it was or that I needed to enable it in my computer. I followed the steps here and that was it, load times are acceptable now.
Thanks everyone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With