📜 ⬆️ ⬇️

PHP's echo function can run for more than 1 second.

Or about the features of managing content given in PHP.


The reason for this article was a two-day study, the results of which showed that the echo and print functions, innocuous in their performance, can actually work for a very long time and their performance depends on the quality of the Internet of the end user.

To begin with, if I had been told this yesterday, I would have wagged my finger at his temple with this person, but a series of tests carried out inexorably testifies to this.


')

Page generation: 1 second on a powerful server and 200 ms. on the weak.


It all started with the fact that I implemented a self-written profiler in the CakePHP framework and embedded a function to calculate the execution intervals after the main logical parts of the code. On the local server, everything worked fine, the profiler showed 200-300 ms. , but on the production (the server is much more powerful, for which we have not yet caught up with the visitors), the execution time was sometimes 1-3 seconds !

Using your favorite performance debugging method:
  1. $microtime = microtime(true);
  2. //
  3. echo (microtime(true) - $microtime);

, :
  1. print $out;

.

, , 6 PHP. , , 11-32 .
Nagle algorithm, . -, Apache. .

.


, , :
  1. $index = !empty($_GET['index']) ? $_GET['index'] : 1;
  2. $example_output = str_repeat(str_repeat("*", 1024), $index);
  3.  
  4. $microtime = microtime(true)*1000;
  5. echo $example_output;
  6. $interval = microtime(true)*1000 - $microtime;
  7.  
  8. echo '<br>Display Length: ', $index, ' KiB.<br>';
  9.  
  10. if($interval < 100 && $_GET['index'] < 100)
  11.   echo '<meta http-equiv="refresh" content="1; url=?index='.($index + 1).'" />';
  12.  
  13. echo 'Reach end file: ', round($interval, 2), ' ms.'."<br>\n";

, , , echo 100 .

, 11 :
*****
Display Length: 11 KiB.
Reach end file: 0.07 ms.


12 :
*****
Display Length: 12 KiB.
Reach end file: 348.92 ms.


. — 13 . ( ) — .

, 348.92 ms , , , .

.


, PHP.
, :
PHP good output buffering
:

:
1. .
2. Apache`.
3. PHP.
4. echo, PHP .
5. Apache .
6. PHP shutdown . , register_shutdown_function.
7. . .
8. PHP ( session_write_close).
9. Apache .
10. .

:
PHP output buffering problems
:
4a. echo. Apache.
4b. Apache , , echo.
8a. PHP , , .
8b. Apache .
8c. PHP .

4 , . , PHP .

.


PHP, « » , output_buffering=200K .

, . PHP:

, . . output_buffering , PHP shutdown , .
:
php_value output_buffering 131072

, - , . :
php_flag output_buffering On


«» :
PHP output buffering problem solution
, :


.


, , echo. , , , 11 PHP . , 18 .

.


1984 Nagle, PHP . , , .

.


, .
. . , wget, 128 .
wget --limit-rate=1K www.test.lo/nagle_test.php?index=128
Display Length: 128 KiB.
Reach end file: 57234.61 ms.


, :

Source: https://habr.com/ru/post/45016/


All Articles