简体   繁体   English

对 http 请求的响应来自 Android 应用程序对 PHP 代码的响应太慢时 Z62E0B5B350C9E34D2C19AA8019D

[英]Response to http request from Android application to PHP code with Nginx is too slow when the response is big

In a Linux server Nginx as web server,在 Linux 服务器 Nginx 作为 web 服务器中,

Response to http request from Android application to PHP code with Nginx is too slow对 http 请求的响应来自 Android 应用程序对 PHP 代码的 Z62E0B5B350C9E3D2C19AA8019D 太慢

The point is the response from the server is as following:关键是服务器的响应如下:

echo "{mResponse}"

When mResponse is small (you say 500 bytes), there is no problem and the response is too quick mResponse 很小的时候(你说 500 字节),没有问题,响应太快
When mResponse is big (you say 200K bytes), there is a problem and the response is too slow mResponse很大的时候(你说200K字节)有问题,响应太慢

I doubted on echo and searched a lot and found out the echo may be too slow我怀疑回声并搜索了很多,发现回声可能太慢了
There were some solutions like chuncking the big data into smaller chuncks (4096) and then echoing有一些解决方案,例如将大数据分块成更小的块(4096),然后回显
Or ob_start and...或者 ob_start 和...

I tested them and finally found out the problem is somewhere else because when I used the following code, the time of echo is ok:我对它们进行了测试,最后发现问题出在其他地方,因为当我使用以下代码时,回显的时间是可以的:

$time_start =  microtime(true);
$this->echobig("@#@{$mCommand}70{$mUserID}{$mResponse}{$mCheckSum}#@#"); 
echo "\nThe first script took: " . ( microtime(true) - $time_start) . " sec";

$time_start =  microtime(true);
ob_start();
$this->echobig("@#@{$mCommand}70{$mUserID}{$mResponse}{$mCheckSum}#@#"); 
ob_end_flush();
echo "\nThe Second script took: " . ( microtime(true) - $time_start) . " sec";

    public function echobig($string)
    {
        $splitString = str_split($string, 4096);
        foreach($splitString as $chunk)
        {
            echo $chunk;
         }
    }

On both above codes (The first and second scripts) the time was near在上述两个代码(第一个和第二个脚本)上,时间都快到了

0.0005 sec 0.0005 秒

which is ok没关系

But the Android application is receiving the response in 13 seconds但是 Android 应用程序在13 秒内收到响应
As I said when the response is small, the Android application quickly receives the response (in 2 seconds )正如我所说,当响应很小时,Android 应用程序很快收到响应(在2 秒内

Now I doubt on Nginx settings or PHP settings (may be buffer limits somewhere)现在我怀疑 Nginx 设置或 PHP 设置(可能是某处的缓冲区限制)
I don't know which parameter is problematic不知道哪个参数有问题

Sorry, It has nothing to do with Nginx or PHP对不起,它与 Nginx 或 PHP 无关
The problem is the following code to calculate the check sum of the mentioned big response:问题是以下代码来计算上述大响应的校验和:

        $cs = 0;
        $content_length = strlen($string);
        for ($i=0; $i < $content_length;$i++)
        {
                $K = mb_substr($string,$i, 1, 'UTF-8');
                $A = mb_convert_encoding(mb_substr($string,$i, 1, 'UTF-8'),"UTF-8");
                $B = $this->mb_ord_WorkAround($A);
                $cs = $cs + $B;                
                
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM