Acceleration of php echo [turn] Author: Laruence address: http://www.laruence.com/2011/02/13/1870.html reprint please indicate the source
You may have noticed that when using ECHO in PHP to output a large string, the execution time is obviously long, and some may think that PHP's ECHO performance is very poor.
I have explained the cause in my previous article and hope to correct the misunderstanding of "poor ECHO performance of PHP.
However, the previous article only gave the reason and did not introduce how to avoid this problem. Today, a product line (Apache with PHP) in the company found a problem, some users initiate a large number of download requests in a short period of time, resulting in a sharp increase in the number of http connections and database connections,
The reason for the sharp increase in the number of database connections is that the database connection is in single-column mode and will not be released until the request processing is complete. in this way, a problem occurs. if the request processing time is too long, a large number of database connections may exist.
This user's network speed is very slow, which means that ECHO's "performance" is very poor ~, The download takes a long time ~. As shown in:
ECHO execution
This raises the question that I want to talk about today. how can we make ECHO faster and end the PHP request processing process as soon as possible...
We know that the reason why ECHO is slow is to wait for "write data" to return a successful response. a simple method is to open the output cache,
Edit php. ini
Output_buffering = 4096 // byte
Of course, you can also call ob_start () in the script ():
Ob_start (); echo $ huge_string; // Other Logic. ob_end_flush ();
Here, there is a note that ob_start will open up a buffer of 4096 size, so if the huge_string is greater than 4096, it will not accelerate.
Now, our ECHO will "immediately" run successfully and return. because the data is temporarily written to our output cache. if the buffer size is large enough, the content will be sent to the client (strictly speaking, it is sent to WebServer) at the end of the script ).
However, this does not solve the problem we encountered today, because the data needs to be sent to the client by PHP (the Output buffer of WebServer is not considered at this time) at the end ), this process will not end, the request will not be closed, and PHP will not execute the DB destructor ~
Now that you are dreaming, you can use Apache's output cache to change it to the following execution process:
Acceleration ECHO
Assume that PHP needs to output 100 kB of data, so the Apache output cache must be greater than kb. otherwise, when the Apache output cache is full, it will actually be sent to the client, and in this process, the ECHO executed at that time will block the waiting.
So how can I modify the Apache output cache? We can use the SendBufferSize configuration command in the apache configuration file:
SendBufferSize 4096 // note that it is byt
Detailed SendBufferSize description, see the http://httpd.apache.org/docs/2.0/en/mod/mpm_common.html#sendbuffersize
Note: For other Webserver with php-cgi modes, read the relevant Webserver manual to find similar configurations.
Now, PHP's ECHO will directly deliver the content to Apache. after PHP completes the execution, it will not wait until the content is sent to the client and quit directly. the content will be sent to the client by Apache after PHP processing is complete. this accelerates the ECHO execution efficiency.
For example, printf, print, file_put_contents ("php: // output ")... And so on. it's the same as ECHO.
Finally, it should be noted that, in this way, only the waiting time of the original ECHO is transferred to Apache, and the time when the client obtains the content is not actually reduced. it only accelerates the PHP processing process and advances the exit time of PHP, which can reduce the time occupied by PHP resources and indirectly increase the resource usage.