PHP Benchmark一, 25/07/2022 - 11:17 的修訂版本

修訂版本可以讓你追蹤文章的多個版本的不同之處。

 

 


php-fpm log location

 

php-fpm settings

; php80
php_flag[display_errors] = off
php_admin_flag[log_errors] = on
php_admin_value[error_log] = /var/opt/remi/php80/log/php-fpm/www-error.log

ls -ld /var/opt/remi/php80/log/php-fpm/www-error.log   # Folder permission 係其他人看不了.

drwxrwx--- 2 apache root 23 Jul  7 16:49 /var/opt/remi/php80/log/php-fpm

會 log 到 error_log 的類型:

  • Fatal error
  • Parse error
  •  

 * 可透過 "error_reporting(E_ALL & ~E_NOTICE);" 修改 log 什麼

 


Memory Limit

 

Show Current memory usage

# Returns the amount of memory allocated to PHP

memory_get_usage()         # php 8.0

<?php
  echo memory_get_usage() . PHP_EOL; // 347864
  $a = str_repeat("Hello", 1000000);
  echo memory_get_usage() . PHP_EOL; // 5349104
  unset($a);
  echo memory_get_usage() . PHP_EOL; // 347864
?>

(5349104 - 347864) / 1000000 ~ 5

Memory usage limit settings

mod_php

Runtime set memory limit in Code

ini_set('memory_limit', '256M');

 * Runtime set memory limit 只在 mod_php 時才有效, FPM 情況無效

memory 在 php-fpm 時 Settings

php.ini

memory_limit = 256M

php-fpm.conf

php_admin_value[memory_limit] = 256M

Log

/var/opt/remi/php80/log/php-fpm/www-error.log

... PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried to allocate 268435464 bytes)
    in /home/vhosts/datahunter.org/public_html/tim.php on line 21

 


Execution time limit

 

php-fpm.conf

php_admin_value[max_execution_time] = 30

計時 script

$start_time = microtime(true);
...
$end_time = microtime(true);
$execution_time = ($end_time - $start_time);
echo " Execution time: " . $execution_time . " us";

# us (1s=10^6)

microtime() 当前 Unix 时间戳以及微秒数

Log

... PHP Fatal error:  Maximum execution time of 30 seconds exceeded in 
    /home/vhosts/datahunter.org/public_html/tim.php on line 19

 


Test script

 

功能: 測試 memory_limit 及

<?php

error_reporting(E_ALL & ~E_NOTICE);

// Setting
$nloop = 1000000;

echo "max_execution_time: " . ini_get('max_execution_time') . PHP_EOL;
echo "memory_limit: " .  ini_get('memory_limit') . PHP_EOL;

echo "========" . PHP_EOL;

// start timer
$start_time = microtime(true);

// code
$big_array = array();
for ($i = 0; $i < $nloop; $i++)
{
    for ($j = 0; $j < $nloop; $j++)
    {
        //hash('sha256', 'To run time consume function');
        //$big_array[] = $i;
    }
}

// result
$end_time = microtime(true);
$execution_time = ($end_time - $start_time);

echo "Execution time: " . $execution_time . " us" . PHP_EOL;
echo "Usage: " . memory_get_usage() . PHP_EOL;
?>