dtcpvz8162 2014-02-05 13:11
浏览 35
已采纳

PHP请求输出需要很长时间才能收到

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, wich when loaded includes another php file called F1 that creates html table trough 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 timmings:

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 timmings:

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:

  • Use ob_start() and ob_end_flush().
  • Set apache mpm prefork SendBufferSize to a higher value, but I believe it was a long shot, as the longer response (50KB) has no problem.
  • Use echo instead of sprintf.

Anyone has a clue?

Best regards.

  • 写回答

1条回答 默认 最新

  • douji2283 2014-02-05 20:43
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?