我在页面上有一些 ajax 调用,请求一些 json 内容。在所有这些电话中,我等待响应完成的时间很长。对于每个呼叫,呼叫中都会有几秒钟的“等待”期,如下面的 Chrome 网络面板所示。我附上了一张图片:
我不太确定是什么原因造成的,因为我对查询数据库的 php 代码做了一些基准测试,根据该结果,查询调用和处理要发送回的 json 的运行时间约为 0.001 秒左右。
那么,这只是网络延迟问题吗?这是我没有正确执行数据库查询的问题吗?也许我正在淹没每个浏览器窗口的最大连接数?不知道。其他请求的进展也同样缓慢,因此看起来可能是一致的。
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:
作为参考,这里是 ajax 调用:
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);
});
};
这是运行查询的控制器中的 php 代码:
$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;