PHP模板系统的对决:Blitz、smarty和php原生代码
本帖最后由 lsstarboy 于 2012-07-31 17:19 编辑原文请移步以下链接:请点击此处
网上关于Blitz的资料很少,仅有的几个测试也有点模糊,并且只测试了直接输出变量的部分,这次我加入了数组的循环测试部分。
一、Blitz
1、从0-100循环,总流量已经超过7M,很可能会受网速的限制。
服务器CPU利用率:85%左右,负载:1或2
最繁忙的程序是nginx
CPU利用率恢复到0%时,测试马上结束。%webbench -t 90 -c 200 http://ict.sdcangyuan.com/test/blitz/test.php
200 clients, running 90 sec.
Speed=103998 pages/min, 7422003 bytes/sec.
Requests: 155993 susceed, 4 failed.
%webbench -t 90 -c 300 http://ict.sdcangyuan.com/test/blitz/test.php
300 clients, running 90 sec.
Speed=103214 pages/min, 7354411 bytes/sec.
Requests: 154808 susceed, 14 failed.2、改为从0-50循环:%webbench -t 90 -c 300 http://ict.sdcangyuan.com/test/blitz/test.php
300 clients, running 90 sec.
Speed=114114 pages/min, 3711739 bytes/sec.
Requests: 171164 susceed, 7 failed.
%webbench -t 90 -c 400 http://ict.sdcangyuan.com/test/blitz/test.php
400 clients, running 90 sec.
Speed=113617 pages/min, 3687560 bytes/sec.
Requests: 170407 susceed, 19 failed.
[*] input (Total) output
packetserrs idrops bytes packetserrs bytes colls drops
29k 0 0 6.4M 25k 0 9.7M 0 0
28k 0 0 6.3M 24k 0 9.6M 0 0
28k 0 0 6.2M 24k 0 9.4M 0 0
28k 0 0 6.3M 24k 0 9.5M 0 0
28k 0 0 6.4M 24k 0 9.6M 0 0
28k 0 0 6.3M 24k 0 9.6M 0 0
28k 0 0 6.3M 24k 0 9.5M 0 0
28k 0 0 6.3M 24k 0 9.6M 0 0
28k 0 0 6.2M 24k 0 9.5M 0 0
28k 0 0 6.3M 24k 0 9.5M 0 0 ab -c 300 -t 90 http://ict.sdcangyuan.com/test/blitz/test.php
Document Path: /test/blitz/test.php
Document Length: 1794 bytes
Concurrency Level: 100
Time taken for tests: 18.493 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 97800000 bytes
HTML transferred: 89700000 bytes
Requests per second: 2703.68 [#/sec] (mean)
Time per request: 36.987 (mean)
Time per request: 0.370 (mean, across all concurrent requests)
Transfer rate: 5164.46 receivedhttp://blog.chinaunix.net/attachment/201207/31/20332519_1343722930TArR.png
http://blog.chinaunix.net/attachment/201207/31/20332519_134372293488nt.png
二、smarty
1、CPU利用率: 100%, 负载:25左右
最繁忙的程序是php-fpm
CPU利用率恢复到0%时,仍然要等近两分钟才能结束测试
300个连接,webbench会等会长时间也得不到结果。
下面的结果可能出错,因为同样的输出内容,在每分钟11W的时候,流量大约是不到4M,看后台,出现了大量的error,说明已经超出php的计算范围,这个时候应该出现了大量的错误页面:%webbench -t 90 -c 250 http://ict.sdcangyuan.com/test/smarty/demo/index.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://ict.sdcangyuan.com/test/smarty/demo/index.php
250 clients, running 90 sec.
Speed=118555 pages/min, 955586 bytes/sec.
Requests: 177833 susceed, 0 failed.减少并发数,可以看到结果差不多,每分钟1w,每秒在4百k左右:%webbench -t 90 -c 100http://ict.sdcangyuan.com/test/smarty/demo/index.phpWebbench - Simple Web Benchmark 1.5Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://ict.sdcangyuan.com/test/smarty/demo/index.php100 clients, running 90 sec.
Speed=13425 pages/min, 440285 bytes/sec.Requests: 20138 susceed, 0 failed.继续加到200个并发,同时附上流量信息:
[*]%webbench -t 90 -c 200 http://ict.sdcangyuan.com/test/smarty/demo/index.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://ict.sdcangyuan.com/test/smarty/demo/index.php
200 clients, running 90 sec.
Speed=13413 pages/min, 439825 bytes/sec.
Requests: 20120 susceed, 0 failed.[*] input (Total) output
packetserrs idrops bytes packetserrs bytes colls drops
3.5k 0 0 764k 3.3k 0 1.1M 0 0
3.6k 0 0 782k 3.4k 0 1.2M 0 0
3.4k 0 0 741k 3.2k 0 1.1M 0 0
3.5k 0 0 760k 3.3k 0 1.1M 0 0
3.5k 0 0 768k 3.3k 0 1.1M 0 0
继续加到300并发,坚持了不到1分钟,nginx-error爆满:
[*]%webbench -t 90 -c 300 http://ict.sdcangyuan.com/test/smarty/demo/index.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://ict.sdcangyuan.com/test/smarty/demo/index.php
300 clients, running 90 sec.
Speed=123128 pages/min, 979094 bytes/sec.
Requests: 184515 susceed, 178 failed.
%
packetserrs idrops bytes packetserrs bytes colls drops
21k 0 0 1.9M 19k 0 2.6M 0 0
20k 0 0 1.9M 18k 0 2.5M 0 0
20k 0 0 1.9M 18k 0 2.5M 0 0
20k 0 0 1.9M 18k 0 2.5M 0 0
20k 0 0 1.9M 18k 0 2.5M 0 0
20k 0 0 1.8M 18k 0 2.5M 0 0
20k 0 0 1.9M 18k 0 2.5M 0 0
20k 0 0 1.9M 18k 0 2.6M 0 0
ab -c 100 -t 90 http://ict.sdcangyuan.com/test/smarty/demo/index.php
Document Path: /test/smarty/demo/index.php
Document Length: 1806 bytes
Concurrency Level: 100
Time taken for tests: 90.001 seconds
Complete requests: 20336
Failed requests: 0
Write errors: 0
Total transferred: 40021248 bytes
HTML transferred: 36726816 bytes
Requests per second: 225.95 [#/sec] (mean)
Time per request: 442.568 (mean)
Time per request: 4.426 (mean, across all concurrent requests)
Transfer rate: 434.26 receivedhttp://blog.chinaunix.net/attachment/201207/31/20332519_1343722939XXBu.png
http://blog.chinaunix.net/attachment/201207/31/20332519_1343722943y9Rr.png
三、php代码输出:
CPU利用率:55%左右%webbench -t 90 -c 300 http://ict.sdcangyuan.com/test/test.phpWebbench - Simple Web Benchmark 1.5Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://ict.sdcangyuan.com/test/test.php300 clients, running 90 sec.
Speed=153972 pages/min, 3984266 bytes/sec.Requests: 230950 susceed, 8 failed.
[*]
input (Total) output
packetserrs idrops bytes packetserrs bytes colls drops
28k 0 0 5.5M 24k 0 7.9M 0 0
28k 0 0 5.5M 24k 0 7.9M 0 0
28k 0 0 5.5M 24k 0 7.9M 0 0
27k 0 0 5.3M 23k 0 7.7M 0 0
28k 0 0 5.4M 24k 0 7.9M 0 0
28k 0 0 5.5M 24k 0 8M 0 0
28k 0 0 5.4M 24k 0 7.9M 0 0
28k 0 0 5.5M 24k 0 7.9M 0 0 ab -c 100 -t 90 http://ict.sdcangyuan.com/test/test.php
Document Path: /test/test.php
Document Length: 1392 bytes
Concurrency Level: 100
Time taken for tests: 15.221 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 77701554 bytes
HTML transferred: 69601392 bytes
Requests per second: 3285.00 [#/sec] (mean)
Time per request: 30.441 (mean)
Time per request: 0.304 (mean, across all concurrent requests)
Transfer rate: 4985.35 received
http://blog.chinaunix.net/attachment/201207/31/20332519_1343722946WM7J.png
http://blog.chinaunix.net/attachment/201207/31/20332519_1343722949zWCW.png
四、结论:
Blitz的速度大约是smarty的10倍,能达到php的直接echo方式的80%左右,性能相当不错了。
但是Blitz的变量值传递非常别扭,必须多一级数组,在数组循环的时候,竟然夸张地需要三级数组!相比来说,smarty的单级数组显得非常简洁和便于理解。
五、三种方式的源代码:
注:以上各个测试结果,均有Xcache加速器,下次测试无Xcache时的对比,另外php原生代码如果完全都用变量输出的话,可能还会慢一点:
1、Blitz
php文件:<?php
$t = new Blitz();
$t->load('{{ include("./test.tpl") }}');
$arr = array();
for ( $i = 0 ; $i < 50 ; $i++ ){
$arr[]['number'] = $i;
}
$t->set(array('arr'=>$arr));
$abc = "This is a Test template!";
$t->set(array('abc'=>$abc));
$title = "The Test Page of lsstarboy!";
$t->set(array('title'=>$title));
$t->display();
?>tpl文件:
<html>
<head>
<title> {{ $title }} </title>
<body>
{{ $abc }} <br>
{{ BEGIN arr }}
<p>The number is {{ $number }} ! </p>
{{ END }}
<p> This is the end!!</p>
</body>
</html>2、smarty:
php文件:
<?php
require('../libs/Smarty.class.php');
$smarty = new Smarty;
$smarty->debugging = false;
$smarty->caching = true;
$smarty->cache_lifetime = 120;
$smarty->assign("title","The Test Page Of lsstarboy");
$smarty->assign("abc","This is a Test template!");
$arr = array();
for( $i=0; $i < 50; $i++ ) {
$arr[] = $i;
}
$smarty->assign("arr",$arr);
$smarty->display('index.tpl');
?>tpl文件:<html>
<head>
<title>{$title}</title>
</head>
<body>
{$abc}<br>
{section name=test loop=$arr}
<p>The number is {$arr} ! </p>
{/section}
<p>This is the end!!</p>
</body>
</html>3、php原生代码:<?php
echo "<html>\n";
echo "<head>\n";
$title = "The Test Page of lsstarboy!";
echo "<title>$title</title>\n";
echo "</head>\n";
echo "<body>\n";
echo "This is a Test template!";
for ( $i =1 ; $i<50; $i++ ) {
echo "The number is : " . $i . "!";
}
$abc = "This is the end!";
echo $abc;
echo "</body>\n";
echo "</html>";
?>
----end----
占位,发不带Xcache的结果: 再占个位备用,有时间的话,再分析一下discuz的模板,据说速度非常快。 用了smarty就没有动力学习其他的了。除非特别要求性能的,直接用php了。
页:
[1]