虽然一般假设是让 PHP 通过 cmets 进行咀嚼会导致 没有可测量的差异,但最好检查一下,不是吗?
(注意:按照常识,我们期望纯粹的请求处理、权限管理、流程控制、调度这个、委托那个、启动 PHP 运行时环境、管理各种缓存、摆弄资产文件、整个磁盘和网络 I/O 等等等等,哦,顺便说一句,也执行代码,所有这些加起来很可能远远超过任何大量的 cmets。)
所以我给了它一个非常简单的尝试,只是为了立即感受它。
1。设置
预测“评论影响”与neutrinos 一样难以检测,因此我特意做了一个稍微有点病态的设置,试图使差异可衡量,但仍不能过于不切实际。
我创建了两个文件。一个没有 cmets,只有大约 100 个字节,直截了当,no-comments.php:
<?php
function task() {
++$GLOBALS;
echo "[$GLOBALS] Lorem ipsum dolor sit amet cosectetur...\n";
}
另外,大约 60K(仅出于与堆管理相关的迷信而保持在 64K 以下)comments.php:
<?php
/* ... some 30K comments ... */
// OK, that's something, but how about:
/* ... same 30K comments again ... (Phantomjs changelog, for the curious of you. :) ) */
// Finally, do something:
function task() {
++$GLOBALS; // Comments are cheap, so let me tell you how much I enjoyed this instead of properly declaring a counter. :)
echo "[$GLOBALS] Lorem ipsum with a lot of comments...\n";
}
注意:这当然很可能会测试文件大小的实际影响,而不仅仅是 cmets,但这始终是“cmets(非)问题”的固有部分" 无论如何,我也想要 something 首先。也许即使那已经无法衡量了,对吧?
一般的想法是然后以各种方式循环 task(),从同一个 PHP 进程内部循环一点(或根本没有),从外部循环很多,通过单独的执行,强制重新解析,这是本实验唯一有趣的部分。
为了获得最快的结果,我做了一些 shell 运行:
#!/bin/bash
for (( i = 0; i < 1000; i++ ))
do
php comments.php # <-- and another batch with "no-comments.php"
done
但结果证明这是不可靠的,因为增加循环次数会导致执行时间发生莫名其妙且不成比例的变化。我改用 PHP 运行器,运行更流畅:
#!/usr/bin/php
<?php
$t1 = microtime(true);
for ($i = 0; $i < 1000; ++$i ) {
system("php comments.php"); // <-- and with "no-comments.php"
}
$t2 = microtime(true);
echo "Time: ", $t2 - $t1
对于 HTTP 运行,然后我添加了这个index.php:
<?php
$GLOBALS = 0; // innovative use of a dull language feature ;)
$t1 = microtime(true);
require_once (isset($_GET['no']) ? 'no-' : '') . 'comments.php';
// Played a bit with looping here, but ended up leaving it out.
// for ($i = 0; $i < 3; ++$i) {
// task();
// echo '<br>';
// }
$t2 = microtime(true);
echo "<hr>Time: ", number_format($t2 - $t1, 10);
注意:起初,不幸的是,我启用了 PHP 的 Zend Opcache,并浪费了很多时间试图理解结果...... ;-o 然后我当然禁用了缓存,并重复了 Web 测试(only)。
主机只是普通的 Debian、Apache2 和一些 PHP5(我猜它是 FPM——甚至没有费心检查,因为这应该与测试的主题正交(如果这不是真的,请纠正我) . 它实际上甚至可以通过减少无关的 PHP 启动开销来掩盖微小的注释解析时间来帮助揭示差异。)
2。结果 - 外壳:
运行 PHP-cli 出奇的慢,所以我很快就厌烦了,这两种变体只进行了 1000 次迭代的十几个循环。 (以秒为单位的结果。)
评论:
44.2015209198
39.710990905762
42.374881982803
36.29861998558
44.764121055603
38.85772395134
42.627450942993
38.342661142349
48.539611816406
39.784120082855
50.34646987915
47.782819032669
36.974604845047
45.692447900772
平均:42.592717
没有评论:
45.617978811264
43.397685050964
46.341667175293
44.246716976166
40.348230838776
43.048954963684
38.57627081871
50.429704189301
41.811543226242
35.755078077316
53.086957931519
31.751699924469
48.388355970383
49.540207862854
平均:43.738647
如你所见,这都是垃圾......但如果我们忽略环境波动,结论是使用更多的 cmets,它会让你的脚本更快! :)
3。结果 - HTTP、Zend Opcache 已启用:
(从 ab 输出中去除了一些噪音。)
评论:
ab -qd -n 10000 'http://.../comments/?yes'
Server Software: Apache/2.4.10
Concurrency Level: 1
Time taken for tests: 3.158 seconds
Complete requests: 10000
Failed requests: 0
Non-2xx responses: 10000
Total transferred: 7120000 bytes
HTML transferred: 4620000 bytes
Requests per second: 3166.12 [#/sec] (mean)
Time per request: 0.316 [ms] (mean)
Transfer rate: 2201.45 [Kbytes/sec] received
没有评论:
ab -qd -n 10000 'http://.../comments/?no'
Server Software: Apache/2.4.10
Concurrency Level: 1
Time taken for tests: 3.367 seconds
Complete requests: 10000
Failed requests: 0
Non-2xx responses: 10000
Total transferred: 7120000 bytes
HTML transferred: 4620000 bytes
Requests per second: 2969.95 [#/sec] (mean)
Time per request: 0.337 [ms] (mean)
Transfer rate: 2065.04 [Kbytes/sec] received
哇! :-o 就像 shell 运行一样! :) 好吧,不相信我的眼睛,我又重复了几次,直到它有意义... :) 看到了吗?这里:
Benchmarking ...<"NO COMMENTS">... (be patient).....done
Time taken for tests: 2.912 seconds
Total transferred: 7120000 bytes
HTML transferred: 4620000 bytes
Requests per second: 3433.87 [#/sec] (mean)
Time per request: 0.291 [ms] (mean)
Transfer rate: 2387.61 [Kbytes/sec] received
(顺便说一句,不要问我,为什么非 2xx 响应。他们通过网络是 200 OK。)
然后,再进行十倍的迭代:
评论:
Time taken for tests: 32.499 seconds
Requests per second: 3077.04 [#/sec] (mean)
Time per request: 0.325 [ms] (mean)
Transfer rate: 2139.51 [Kbytes/sec] received
没有评论:
Time taken for tests: 28.257 seconds
Requests per second: 3538.92 [#/sec] (mean)
Time per request: 0.283 [ms] (mean)
Transfer rate: 2460.66 [Kbytes/sec] received
呸,完美! 评论是邪恶的! ;)
好吧,我还做了几个,我只能向你展示这个没有评论的结果:
Time taken for tests: 37.399 seconds
Requests per second: 2673.84 [#/sec] (mean)
Time per request: 0.374 [ms] (mean)
Transfer rate: 1859.15 [Kbytes/sec] received
4。结果 - HTTP、Zend Opcache 已禁用:
好的,在意识到我打开了缓存之后,我从 PHP-FPM 配置中注释掉了扩展名(所以,确实,这就是这里运行的内容),重新启动服务,检查 phpinfo(),并收集新结果:
评论:
Time taken for tests: 34.756 seconds
Requests per second: 2877.23 [#/sec] (mean)
Time per request: 0.348 [ms] (mean)
Transfer rate: 2000.58 [Kbytes/sec] received
再一次:
Time taken for tests: 31.170 seconds
Requests per second: 3208.24 [#/sec] (mean)
Time per request: 0.312 [ms] (mean)
Transfer rate: 2230.73 [Kbytes/sec] received
没有评论:
Time taken for tests: 30.060 seconds
Requests per second: 3326.70 [#/sec] (mean)
Time per request: 0.301 [ms] (mean)
Transfer rate: 2313.10 [Kbytes/sec] received
再一次:
Time taken for tests: 32.990 seconds
Requests per second: 3031.23 [#/sec] (mean)
Time per request: 0.330 [ms] (mean)
Transfer rate: 2107.65 [Kbytes/sec] received
嗯。正如您所看到的,基本上:与 opcache 开/关状态没有什么差别!也不是在开/关之间(除了一个微小的提示,但已经看到了波动......)! :-o
5。结论
所以...最后,数字!好吧,事实上,无用的垃圾,但至少不仅仅是宗教猜测。出于混淆 data 的合理原因而感到困惑比缺少数据要好得多! :)
现在,在我确实在这方面浪费了足够多的时间之后,“cmets 成本多少”这个古老问题的答案仍然是一个谜。
由于中微子已经(令人难以置信地)被发现多年,我们可能会开始感到尴尬。有人最终会带来突破并最终检测到 PHP 评论的影响吗?没人知道...