2013-03-16 10 views
5

Ho alcune chiamate Ajax su una pagina che richiede alcuni contenuti JSON. In tutte queste chiamate, sto ottenendo un tempo di attesa significativo sulla risposta che termina. Per ognuna di queste chiamate, c'è un periodo di "attesa" di alcuni secondi nella chiamata, come mostrato nel pannello Chrome Network di seguito. Ho allegato una foto:Perché la durata di "attesa" è così lunga sulla mia chiamata Ajax? (Pannello di rete Chrome)

Waiting On the Ajax Call - screenshot 1

io non sono davvero sicuro di quello che sta causando che, come ho fatto un po 'di benchmarking sul codice php che sta interrogando il database, e in base a questo, l'invito per la query e l'elaborazione del json da inviare è in esecuzione in 0,001 secondi circa.

Quindi, questo è solo un problema di latenza di rete? È un problema in cui non sto facendo correttamente la query del database? Forse sto invadendo le connessioni massime per finestra del browser? Nessuna idea. Le altre richieste si stanno muovendo altrettanto lentamente, quindi sembra che potrebbe essere qualcosa di coerente.

Ecco un'altra foto del resto della tempistica richieste (con l'altra chiamata ajax principale prendendo proprio tutto il tempo che la chiamata get_usergames_simple: All the requests

Per riferimento, ecco la chiamata 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); 
    }); 
}; 

E qui è il codice php nel controller di eseguire la 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; 
+1

C'è qualcosa di lento nel costruttore del controller? –

+0

Questa è una bella domanda! Non ci avevo pensato ... Permettimi di fare un po 'di benchmark e di tornare da te. – janson0

+1

Perché no 'json_encode()' invece di concatenazioni di stringhe? – complex857

risposta

Problemi correlati