这个问题与我的另一个问题found here有关.
起初我认为这是一个网络问题,但似乎越来越可能,这与我的php配置和php文件的运行有关.我做了以下测试用例:
我创建了一个名为test.php的php文件,其中包含以下内容:
echo 'test';
?>
并创建了两个bash文件,其中包含以下内容:
//bash1.sh
#!/bin/bash
/usr/bin/php /testFirstByte/test.php
和另一个
//bash2.sh
#!/bin/bash
echo Test;
然后我继续他们的运行时间,运行每个并在其前面加上’time’命令,即:time php test.php
结果如下:
// time php test.php
test
real 0m0.548s
user 0m0.445s
sys 0m0.101s
// time sh bash2.hs
Test
real 0m0.002s
user 0m0.002s
sys 0m0.000s
// time sh bash1.hs
X-Powered-By: PHP/5.5.30
Content-type: text/html; charset=utf-8
test
real 0m0.539s
user 0m0.429s
sys 0m0.108s
对我来说,看起来,每当我尝试运行PHP脚本时,运行时间会增加至少半秒,无论我尝试运行哪个PHP脚本.我无法弄清楚如何解决这个问题,所以任何帮助都会有很大的帮助!
编辑1:我做了一个简单的脚本,我希望这是你的内部测试@Eineki的意思.脚本如下:
$timer = microtime(true);
require('test_simple.php');
$end = microtime(true) - $timer;
echo "Require test duration was: " . $end . " seconds\n";
$timer = microtime(true);
exec('php test_simple.php');
$end = microtime(true) - $timer;
echo "Exec test duration was: " . $end . " seconds\n";
结果如下:
Require test duration was: 0.00102400779724 seconds
Exec test duration was: 0.61318397522 seconds
编辑2:加载的扩展名列表如下:
Array
(
[0] => Core
[1] => date
[2] => ereg
[3] => libxml
[4] => openssl
[5] => pcre
[6] => sqlite3
[7] => zlib
[8] => bcmath
[9] => bz2
[10] => calendar
[11] => ctype
[12] => curl
[13] => dom
[14] => hash
[15] => fileinfo
[16] => filter
[17] => ftp
[18] => gd
[19] => gettext
[20] => SPL
[21] => iconv
[22] => session
[23] => json
[24] => mbstring
[25] => mcrypt
[26] => standard
[27] => mysql
[28] => mysqli
[29] => mysqlnd
[30] => Phar
[31] => posix
[32] => Reflection
[33] => imap
[34] => SimpleXML
[35] => sockets
[36] => exif
[37] => tokenizer
[38] => xml
[39] => xmlreader
[40] => xmlwriter
[41] => zip
[42] => cgi-fcgi
[43] => PDO
[44] => pdo_sqlite
[45] => pdo_mysql
[46] => mailparse
[47] => Zend OPcache
)
这是我的php版本,因为php -v:
PHP 5.5.30 (cgi-fcgi) (built: Dec 3 2015 06:55:27)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
编辑3:运行@voter在php文件上建议的strace,在生产服务器(有问题的服务器)和我们的开发服务器上,不会发生此问题.在prod服务器上,然后在开发服务器上,所有产生输出的东西基本上都是10倍.也许熟悉strace的人可以从中得出结论?
编辑4:
vmstat 1命令的例外情况:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
5 0 3163644 5410932 2522564 13417292 0 1 22 62 1 0 18 6 75 1 0
2 0 3163644 5845884 2522568 13406468 0 0 0 916 31787 5966 9 6 85 0 0
8 0 3163644 5439468 2522572 13406840 0 0 8 432 50513 5322 12 6 82 0 0
4 0 3163644 5750124 2522572 13407624 0 0 4 232 54417 5615 8 7 86 0 0
4 0 3163644 5748608 2522576 13407480 0 0 4 760 118206 5736 7 9 83 0 0
3 0 3163644 5742648 2522576 13418040 0 0 0 244 68462 6689 10 7 83 0 0
4 0 3163644 5671104 2522576 13407620 0 0 40 568 34157 4222 7 5 87 0 0
4 0 3163644 5980828 2522580 13401712 0 0 16 524 43754 6391 17 6 77 0 0
5 0 3163644 5506988 2522592 13418868 0 0 264 280 59452 5955 16 7 77 0 0
5 0 3163644 5577116 2522600 13417800 0 0 32 540 68056 8968 11 6 83 0 0
7 0 3163644 4747580 2522612 13451468 0 0 16 376 241800 7107 12 13 75 0 0
4 0 3163644 4948548 2522616 13440832 0 0 12 468 354599 5155 7 16 77 0 0
编辑5:
服务器上的top命令的结果:
top - 09:17:58 up 15 days, 1:53, 8 users, load average: 6.90, 6.22, 5.34
Tasks: 687 total, 3 running, 683 sleeping, 0 stopped, 1 zombie
Cpu(s): 15.0%us, 3.4%sy, 0.0%ni, 80.7%id, 0.8%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 49390000k total, 43364688k used, 6025312k free, 2697344k buffers
Swap: 16482300k total, 3495772k used, 12986528k free, 11878096k cached
解决方法:
你的php -v输出看起来就像你从命令行调用php cgi.
对于命令行,通常有一个php版本的php,它可以单独安装,例如apt-get install php5-cli.
这会对性能产生很大影响.对中型Linux服务器的快速测试给了我
#time php-cgi test.php
X-Powered-By: PHP/5.5.26
Content-type: text/html
test
real 0m0.117s
user 0m0.036s
sys 0m0.076s
# time php test.php
test
real 0m0.074s
user 0m0.040s
sys 0m0.036s
如您所见,cgi版本的时间加倍.
您是否可以尝试使用CLI版本来查看差异是否仍然很大?
这可能无法帮助您解决您提到的其他线程中的问题,除非您还在那里使用CGI模块并且没有正确配置它.
标签:php,benchmarking
来源: https://codeday.me/bug/20190607/1195143.html