php - Waitting Time ( TTFB ) is above 4 seconds result from `get_headers()` -
php - Waitting Time ( TTFB ) is above 4 seconds result from `get_headers()` -
i using apache version ( httpd-2.4.10-win64-vc11 ) on windows 2012. trying when click on image item open dialog inforamation item. :
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 });
the problem response slow , unable find reason it. below add together info received chrome speed tracer.
p.s : using cloudfront images , css & javascripts files .
http://i.stack.imgur.com/ueh2q.png
edit : details fiddler web debugger: ( or post same results )
== timing info ============
clientconnected: 13:53:10.037
clientbeginrequest: 13:53:10.064
gotrequestheaders: 13:53:10.064
clientdonerequest: 13:53:10.064
determine gateway: 0ms
dns lookup: 1ms
tcp/ip connect: 16ms
https handshake: 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\"" combined
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, 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, 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, gecko) chrome/41.0.2272.101 safari/537.36"
edited after utilize @elcodedocle code : problem not ajax or mysql problem , get_headers() function problem slow downwards request utilize check if image exists or not, using function :
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') { homecoming 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)) { homecoming false; }else{ homecoming true; } } }else{ homecoming false; } }
you don't seem have problem client latency, neither building , sending request nor processing result.
you should start looking @ what's happening on server.
does server-side script start executing right away? check there no important delay between moment request sent browser , moment script execution begins. web server routing or other kind of server misconfiguration might delaying script execution start.
which part of script slowing down? must determine delay either using breakpoints (xdebug help that), pro way it, or logging server's timestamp @ different points (which in sentiment more amateurish, easier 1 time solution), illustration using microtime , error_log:
.
<?php // execution starts (compare timestamp browser's request timestamp check delays on execution start) ob_start(); $opnames = array(); $opdelays = array(); $start = microtime(true); error_log("start time: {$start}"); $time = $start; // initialization, read , process input ops, etc ... // code here $prevtime = $time; $time = microtime(true); $delay = $time - $prevtime; $opnames[] = "input processing"; $opdelays[] = $delay; error_log("input processed @ {$time}. processing time: {$delay}"); // database access // code here $prevtime = $time; $time = microtime(true); $delay = $time - $prevtime; $opnames[] = "database access"; $opdelays[] = $delay; error_log("database response received @ {$time}. operation duration: {$delay}"); // results processing, output formatting... // code here $prevtime = $time; $time = microtime(true); $delay = $time - $prevtime; $opnames[] = "results processing"; $opdelays[] = $delay; error_log("results processed @ {$time}. processing time: {$delay}"); // output flushing , end of execution ob_end_flush(); $time = microtime(true); $delay = $time - $start; $maxdelayindexes = array_keys($opdelays, max($opdelays)); error_log("end of script execution @ {$time}. total processing time: {$delay}. expensive operation: ".$opnames[$maxdelayindexes[0]]);
how can prepare problem? inquire 1 time again when know problem is. [edit]
so you've analyzed script , discovered get_headers($url)
what's slowing downwards execution.
you want check if particular public resource, image, exists on $url
is remote resource or hosted on same server application running? because if it's local 1 maybe simple file_exists($_server['document root']."path/to/my/image.jpg")
do, , that's several orders of magnitude faster total http request get_headers($url)
performs.
if remote resource located on different server can still speed much process using php curl extension, allows "ping" resource response code without waiting until reception finish (which on heavy image can more 4 seconds having problems with). how it's done:
public function url_exists($url){ if(empty($url) || !$this->is_url($url)){ homecoming false; } $curl = curl_init(); curl_setopt_array( $curl, array( curlopt_returntransfer => true, // result string, not output curlopt_url => $url, // url connect curlopt_nobody => true, // header curlopt_ssl_verifypeer => false, // don't care cert validation this, we? curlopt_connecttimeout => 2 // homecoming 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){ homecoming true; } homecoming false; }
on sidenote, if still have performance issues on this, might interested in exploring other curl options such dns cache expire time in order speed dns resolution, slow part of check when server ip not cached.
php
Comments
Post a Comment