I have a few ajax calls on a page that request some json content. On all these calls, I am getting a significant wait time on the response completing. For each of these calls, there is a "waiting" period of a few seconds in the call as shown in Chrome Network panel below. I have attached a picture:
I am not really sure what is causing that, as I did some benchmarking on the php code that is querying the database, and according to that, the call for the query and processing the json to send back is running in 0.001 seconds or so.
So, is this just a network latency thing? Is this a problem where I am not doing the database query correctly? Maybe I'm flooding the maximum connections per browser window? No idea. The other requests are moving just as slowly, so it seems like it could be something consistent.
Here is another photo of the rest of the requests timing (with the other main ajax call taking just as much time as the get_usergames_simple call:
For reference, here is the ajax call:
self.getGamesContent = function()
{
var userID = "<?php echo $userID; ?>";
var post_data = {
userID: userID
};
$.post("https://mydomain.com/games/get_usergames_simple/", post_data, function(result)
{
var json = $.parseJSON(result);
var mappedGames = $.map(json.games, function(item) {
return new GameItem(item)
});
self.gameitems(mappedGames);
});
};
And here is the php code in the controller running the query:
$userID = $this->input->post('userID');
$this->benchmark->mark('code_start');
$userGames = $this->cache->model('games', 'getGamesSimpleByUserID', array($userID), 120); // keep for 2 minutes
$returnString = "{";
$returnString .= '"user_id": "' . $userID . '",';
$gameCount = 0;
$returnString .= '"games": [';
foreach ($userGames as $ug)
{
$returnString .= "{";
$returnString .= '"user_id" : "' . $userID . '",';
$returnString .= '"game_id" : "' . $ug->GameID . '",';
$returnString .= '"game_name" : "' . $ug->GameName . '",';
$returnString .= '"game_image" : "' . $ug->GameImage . '",';
$returnString .= '"description" : "' . htmlspecialchars($ug->GameDescription) . '",';
$returnString .= '"genre_id" : "' . $ug->GameGenreCatID . '",';
$returnString .= '"genre_name" : "' . $ug->GameGenreName . '",';
$returnString .= '"publisher_id" : "' . $ug->GamePublisherID . '",';
$returnString .= '"publisher_name" : "' . $ug->GamePublisherName . '",';
$returnString .= '"developer_id" : "' . $ug->GameDeveloperID . '",';
$returnString .= '"developer_name" : "' . $ug->GameDeveloperName . '",';
$returnString .= '"active_flag" : "' . $ug->GameIsActive . '",';
$returnString .= '"create_date" : "' . $ug->GameCreateDate . '",';
$returnString .= '"remove_date" : "' . $ug->GameRemoveDate . '",';
$returnString .= '"last_update_date" : "' . $ug->GameLastUpdateDate . '",';
$returnString .= '"user_syncing_game" : "' . $ug->UserSyncingGame . '"';
$returnString .= "},";
$gameCount++;
}
if ($gameCount > 0)
$returnString = substr($returnString, 0, strlen($returnString) - 1);
$returnString .= "]}";
$this->benchmark->mark('code_end');
//echo $this->benchmark->elapsed_time('code_start', 'code_end');
echo $returnString;
Definitely there are some slow actions in the controller's constructor.
It's much better to use built-in profiler in Codeigniter:
http://ellislab.com/codeigniter/user-guide/general/profiling.html
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