Arthur Samarcos
Arthur Samarcos

Reputation: 3299

PHP request output taking a very long time to be received

I´m stuck for 3 days trying to figure it out what is the cause of this problem. Lets go to the details:

A jQuery ajax call loads a php file named HELPER, which when loaded includes another php file called F1 that creates html table through mysqli queries. Ajax get the response and paste the string in a html DIV. The response is a html table. The web server is apache2.2.

Problem is, the code runs in less than 1 second, but the response takes about 50 seconds. The response is only 20 KB.

Some simple HTML table code.
<?php
if (!$res = $sql->query("A QUERY")) { die('custom error 46'); }

if (!$res->num_rows > 0) { die('custom error 47'); }

$myStr = '';
while ( $row = $res->fetch_object() ) {
    if ($row->summary == "1") { 
        $mysum = " class='qtfck-table-summary'"; 
        $myIsSum = "Sim"; 
    } else { 
        $mysum = ""; 
        $myIsSum = ""; 
    }
    if(intval($row->id_centrodecusto) > 0) { 
        $mycc = "<input type='checkbox' name='" . $row->id_task . "' value='" . $row->id_centrodecusto . "' CHECKED />"; 
    } else { 
        $mycc = "<input type='checkbox' name='" . $row->id_task . "' value='' />"; 
    }
    $myj = " style='padding-left:" . intval($row->depth) * 10 . "px'";
    $myStr =    "<tr%s><td>%s</td><td><center>%s</center></td><td><center>%s</center></td><td%s>%s</td><td><center>%s</center></td><td><center>%s</center></td></tr>";
    echo sprintf($myStr,$mysum,$row->wbs,$row->depth,$myIsSum,$myj,$row->name,$row->uniqueid,$mycc); 
}
?>
Html Table closure

The timings:

PHP START: 0.92 sec
PHP END: 0.98 sec
JS RECEIVED DATA: 49.50 sec
JS PROCESSED DATA: 49.56 sec

I did some digging and it looks like the apache/httpd process (shell via top command) is going nuts, taking 100% CPU load during the full 50 seconds of wait.

BUT here´s something funny. If I change the string generated by the sprintf function and, let´s say, set some random string, there´s no problem at all.

Some simple HTML table code.
<?php
if (!$res = $sql->query("A QUERY")) { die('custom error 46'); }

if (!$res->num_rows > 0) { die('custom error 47'); }

$myStr = '';
while ( $row = $res->fetch_object() ) {
    if ($row->summary == "1") { 
        $mysum = " class='qtfck-table-summary'"; 
        $myIsSum = "Sim"; 
    } else { 
        $mysum = ""; 
        $myIsSum = ""; 
    }
    if(intval($row->id_centrodecusto) > 0) { 
        $mycc = "<input type='checkbox' name='" . $row->id_task . "' value='" . $row->id_centrodecusto . "' CHECKED />"; 
    } else { 
        $mycc = "<input type='checkbox' name='" . $row->id_task . "' value='' />"; 
    }
    $myj = " style='padding-left:" . intval($row->depth) * 10 . "px'";
    echo "<TR><TD>eZ6OnMCZgygePZeUQHcqbOmHQDxhDF4KzfkgOd198xhPFV2rRezlIqBdJLY2TcNlO0PLUmK6CQI9PQMZgkLrcoeYIYhM0x9xK4yQXIFb5SLdq32</TD><TD>UTuQPG9WCbOswuJMdkkckMoAW49C71IN9qKdk8OAdRRV3ZuCYxM5GEZKrXXrwE7cWHKTcXTiO4KwGjh1ejENvduZvEVkwA3zoHbWkzEjtFa1GMaNzD2rqswEDSoQix2CLziBNiHD8zliSWu5rvU8wd6dodWBvubvog</TD></TR>";
}
?>
Html Table closure

The response to this request is 50 KB in size.

The timings:

PHP START: 0.78 sec
PHP END: 0.81 sec
JS RECEIVED DATA: 1.13 sec
JS PROCESSED DATA: 1.19 sec

What I have already tried:

Anyone has a clue?

Best regards.

Upvotes: 0

Views: 268

Answers (1)

Arthur Samarcos
Arthur Samarcos

Reputation: 3299

I don´t know why but the problem was caused by the "center" html tags. Somehow the presence of the center tags slowdown the response. I just removed them and created appropriate css classes using "text-align: center" and the problem was gone.

I also did try avoiding PHP to echo the HTML. Even then the tags causes problems again.

Here´s the fix. I just don´t have an explanation for it.

Upvotes: 1

Related Questions