2015-04-04 23 views
0

我在Windows 2012上使用Apache版本(httpd-2.4.10-win64-VC11)。我試圖在點擊圖片項目它會打開關於這個項目的信息對話。我這樣做:等待時間(TTFB)大於4秒結果來自`get_headers()`

var self = $(this), 
    vaid = "1", 
    btnhref = self.attr('href'), 
    itemID = getVar("item",btnhref); 

$.ajax({ 
    type: 'POST', 
    url: '/action.php', 
    data: {"itemID" : itemID}, 
    dataType: 'json', 
    beforeSend: function(xhr) {      
    xhr.setRequestHeader('va', vaid);         
    }, 
    statusCode: { 
    401:function() { Validation('401 Unauthorized</br>Error: Invalid Action'); } 
    }, 
    complete: function() 
    {  
    var state = { 
     "dialog": true 
    }; 
    history.pushState(state, document.title, btnhref);        
    }, 
    success: function(response){}, 
    cache: true, 
    contentType: "application/x-www-form-urlencoded", 
    processData: true 
}); 

問題是響應非常緩慢,我無法找到它的原因。下面我添加我從鉻速度追蹤器收到的數據。

P.S:我正在使用cloudfront的圖像,CSS & Javascripts文件。

http://i.stack.imgur.com/UEh2Q.png

編輯:這個細節從提琴手Web調試:(用GET或POST相同的結果)

==時序信息============

ClientConnected:13:53:10.037

ClientBeginRequest:13:53:10.064

GotRequestHeaders:13:53:10.064

ClientDoneRequest:13:53:10.064

確定網關:0毫秒

DNS查找:1毫秒

TCP/IP連接:16ms的

HTTPS握手:0ms

ServerConnected:13:53:10.080

FiddlerBeginRequest:13:53:10.080

ServerGotRequest:13:53:10.080

ServerBeginResponse:13:53:10.593

GotResponseHeaders:13:53:10.593

ServerDoneResponse:13 :53:15.366

ClientBeginResponse:13:53:15.366

ClientDoneResponse:13:53:15。366

Apache Log:LogFormat「%h%l%u%t \」%r \「%> s%b \」%{Referer} i \「\」%{User-Agent} i \「 「組合

37.8.80.203 - - [04/Apr/2015:11:06:13 +0000] "GET /shadiCo/item/146 HTTP/1.1" 200 33640 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36" 
37.8.80.203 - - [04/Apr/2015:11:06:20 +0000] "POST /method/pusher HTTP/1.1" 200 96 "http://localhost/shadiCo/item/146" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36" 
37.8.80.203 - - [04/Apr/2015:11:06:20 +0000] "POST /method/pusher HTTP/1.1" 200 96 "http://localhost/shadiCo/item/146" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36" 

後使用@elcodedocle代碼編輯: 問題不阿賈克斯或MySQL問題,其get_headers()函數問題它們會減慢,我使用的檢查,如果圖像的存在與否,由請求使用此功能:

public function url_exists($url) { 
    if(!empty($url) && $this->is_url($url) && extension_loaded('openssl')) 
    {    
      $file_headers = get_headers($url); 
      if($file_headers[0] == 'HTTP/1.1 404 Not Found') 
      { 
       return false; 
      }else{ 

        $allowed = array('Content-Type: image/jpeg','Content-Type: image/pjpeg','Content-Type: image/png','Content-Type: image/gif'); 

        if(!in_array($file_headers[1], $allowed)) 
        { 
            return false; 
        }else{ 
            return true; 
        }      
      } 
    }else{ 
     return false; 
    } 
} 
+0

嘗試使用GET而不是POST – 2015-04-04 10:42:27

+1

可以從apache發送日誌嗎?我的意思是 - 你確定,它是ajax慢? – Legendary 2015-04-04 10:45:16

+0

這可能是服務器配置錯誤(hosts文件,DNS conf,路由表,apache虛擬主機......類似的東西)。需要服務器日誌爲action.php POST ajax請求,否則它只是盲目猜測。 – NotGaeL 2015-04-04 11:03:16

回答

0

您似乎沒有問題機智h客戶端延遲,既不構建也不發送請求,也不處理結果。

你應該看看服務器上發生了什麼。

  • 您的服務器端腳本是否立即開始執行?檢查瀏覽器發送請求和腳本執行開始之間是否存在重大延遲。 Web服務器路由或某種其他類型的服務器錯誤配置可能會延遲腳本執行的開始。

  • 腳本的哪個部分放慢了速度?你必須通過使用斷點(Xdebug可以幫助你)來確定延遲的位置,這是做這件事的專業方式,或者通過在不同點記錄服務器的時間戳(這在我看來更加業餘,但更容易使用microtimeerror_log一次溶液),例如:

<?php 

// Execution starts (compare this timestamp with the browser's request timestamp to check for delays on execution start) 

ob_start(); 

$opNames = array(); 
$opDelays = array(); 
$start = microtime(true); 
error_log("Start time: {$start}"); 
$time = $start; 

// Initialization, read and process input ops, etc ... 

// YOUR CODE HERE 

$prevtime = $time; 
$time = microtime(true); 
$delay = $time - $prevtime; 
$opNames[] = "Input processing"; 
$opDelays[] = $delay; 
error_log("Input processed at {$time}. Processing time: {$delay}"); 

// Database access 

// YOUR CODE HERE 

$prevtime = $time; 
$time = microtime(true); 
$delay = $time - $prevtime; 
$opNames[] = "Database access"; 
$opDelays[] = $delay; 
error_log("Database response received at {$time}. Operation duration: {$delay}"); 

// Results processing, output formatting... 

// YOUR CODE HERE 

$prevtime = $time; 
$time = microtime(true); 
$delay = $time - $prevtime; 
$opNames[] = "Results processing"; 
$opDelays[] = $delay; 
error_log("Results processed at {$time}. Processing time: {$delay}"); 

// Output flushing and end of execution 

ob_end_flush(); 

$time = microtime(true); 
$delay = $time - $start; 
$maxDelayIndexes = array_keys($opDelays, max($opDelays)); 
error_log("End of script execution at {$time}. Total processing time: {$delay}. Most expensive operation: ".$opNames[$maxDelayIndexes[0]]); 
  • 你怎麼能解決這個問題?當你知道問題是什麼時再問。

[編輯]

所以你分析你的腳本,發現get_headers($url)是什麼減慢執行。

你想檢查是否有特定的公共資源,圖像,在$url

存在這真的是一個遠程資源,或者它承載的應用程序運行在同一臺服務器上?因爲如果它是本地的,那麼簡單的file_exists($_SERVER['DOCUMENT ROOT']."path/to/my/image.jpg")就可以完成,並且這比完整的HTTP請求get_headers($url)執行快幾個數量級。

如果它是位於不同服務器上的遠程資源,則仍然可以通過使用PHP curl extension加速該進程,該進程允許「ping」資源以獲取響應代碼,而無需等待接收完成這在一個沉重的圖像上可能會超過您遇到問題的4秒鐘)。這是它是如何做:

public function url_exists($url){ 
    if(empty($url) || !$this->is_url($url)){ 
    return false; 
    } 
    $curl = curl_init(); 
    curl_setopt_array($curl, array(
     CURLOPT_RETURNTRANSFER => true, // get result as a string, do not output 
     CURLOPT_URL => $url, // url to connect to 
     CURLOPT_NOBODY => true, // only the header 
     CURLOPT_SSL_VERIFYPEER => false, // we don't really care about cert validation for this, do we? 
     CURLOPT_CONNECTTIMEOUT => 2 // return error if can't connect in 2 seconds or less 
    ) 
); 
    curl_exec($curl); 
    $response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    curl_close($curl); 
    if ($response_code === 200){ 
    return true; 
    } 
    return false; 
} 

在旁註,如果你仍然有這個性能問題,你可能有興趣在探索other cURL options如DNS高速緩存,以加快DNS解析,這通常是到期時間服務器IP未緩存時檢查的慢速部分。