Yaf是一个国人用C语言编写的PHP框架,号称最快的php框架,项目主页是:http://www.yafdev.com/
一、安装
yaf已经被收录进pecl,所以安装也特简单:
pecl install http://pecl.php.net/get/yaf
如果提示错误:
ERROR: `phpize' failed
需要先安装php开发版:
yum install php-devel yum install pcre-devel
yaf安装成功后提示:
Build process completed successfully Installing '/usr/lib64/php/modules/yaf.so' install ok: channel://pecl.php.net/yaf-2.2.9 configuration option "php_ini" is not set to php.ini location You should add "extension=yaf.so" to php.ini
将yaf.so加入php:
vim /etc/php.d/yaf.ini
写入以下内容:
; Enable yaf extension module extension=yaf.so
重启php-fpm或httpd即可生效。
service php-fpm restart service httpd restart
二、Hello, world
假设项目目录为yaf,在yaf下建立以下结构:
yaf ├── application │ ├── controllers │ │ └── Index.php │ └── views │ └── index │ └── index.phtml ├── conf │ └── application.ini └── public └── index.php
其中,public是web的根目录,需要在web服务器配置里设置一下。 上面树状图中的四个文件需要写入内容, 入口文件:
vim yaf/public/index.php
<?php define("APPLICATION_PATH", dirname(dirname(__FILE__))); $app = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini"); $app->bootstrap() //call bootstrap methods defined in Bootstrap.php ->run();
配置文件:
vim yaf/conf/application.ini
[product] ;支持直接写PHP中的已定义常量 application.directory=APP_PATH "/application/"
控制器:
vim yaf/application/controllers/Index.php
<?php class IndexController extends Yaf_Controller_Abstract { // default action name public function indexAction() { $this->getView()->content = "Hello World"; } } ?>
视图:
vim yaf/application/views/index/index.phtml
<html> <head> <title>Hello World</title> </head> <body> <?php echo $content; ?> </body> </html>
三、性能测试
yaf是用c写的php扩展,号称最快的php框架,当然要ab测试一下: ab -n 10000 -c 100 网址
并发100,访问1万次,结果如下:
Document Path: / Document Length: 107 bytes Concurrency Level: 100 Time taken for tests: 2.849 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 2711355 bytes HTML transferred: 1070535 bytes Requests per second: 3510.41 [#/sec] (mean) Time per request: 28.487 [ms] (mean) Time per request: 0.285 [ms] (mean, across all concurrent requests) Transfer rate: 929.49 [Kbytes/sec] received
同一台服务器的drupal7开启页面缓存后,同样测试命令,结果如下:
Document Path: / Document Length: 15028 bytes Concurrency Level: 100 Time taken for tests: 6.444 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 154790000 bytes HTML transferred: 150280000 bytes Requests per second: 1551.80 [#/sec] (mean) Time per request: 64.441 [ms] (mean) Time per request: 0.644 [ms] (mean, across all concurrent requests) Transfer rate: 23457.39 [Kbytes/sec] received
同样条件的yii测试结果:
Document Path: /demos/helloworld/ Document Length: 11 bytes Concurrency Level: 100 Time taken for tests: 6.978 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 1751050 bytes HTML transferred: 110066 bytes Requests per second: 1433.14 [#/sec] (mean) Time per request: 69.777 [ms] (mean) Time per request: 0.698 [ms] (mean, across all concurrent requests) Transfer rate: 245.07 [Kbytes/sec] received
意外发现Yii很杯具,于是试试Yii的blog演示:
ab -n 1000 -c 30 blog网址
Document Path: /demos/blog/ Document Length: 5281 bytes Concurrency Level: 30 Time taken for tests: 8.769 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 5640000 bytes HTML transferred: 5281000 bytes Requests per second: 114.04 [#/sec] (mean) Time per request: 263.058 [ms] (mean) Time per request: 8.769 [ms] (mean, across all concurrent requests) Transfer rate: 628.13 [Kbytes/sec] received
悲摧!发现Yii比未开启缓存的drupal也好不了多少! 忍不住又测试一下同一台机器的drupal6,不开启缓存:
Document Path: / Document Length: 4966 bytes Concurrency Level: 30 Time taken for tests: 4.681 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 5304000 bytes HTML transferred: 4966000 bytes Requests per second: 213.61 [#/sec] (mean) Time per request: 140.441 [ms] (mean) Time per request: 4.681 [ms] (mean, across all concurrent requests) Transfer rate: 1106.45 [Kbytes/sec] received
裸奔的drupal6性能是yii的两倍! 再测试裸奔的drupal7,结果如下:
Document Path: / Document Length: 15028 bytes Concurrency Level: 30 Time taken for tests: 11.545 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 15476000 bytes HTML transferred: 15028000 bytes Requests per second: 86.62 [#/sec] (mean) Time per request: 346.337 [ms] (mean) Time per request: 11.545 [ms] (mean, across all concurrent requests) Transfer rate: 1309.13 [Kbytes/sec] received
性能测试小结(同一服务器比较,数字说话)
处理请求/秒
静态html —— 1万+
原生php —— 5k
Yaf helloworld —— 3510.41
Yii helloworld —— 1433.14
Yii blog —— 114.04
drupal6(不缓存)—— 213.61
drupal7(不缓存)—— 86.62
drupal6(匿名缓存)—— 858.68
drupal7(匿名缓存): 1551.80
测试环境: dell服务器四核4G内存,centos6 64位版,php5.3.26 + eAccelerator
从以上数据分析可得,yaf的helloworld测试是很快的,比起原生php仅损失30%的速度。
然而,从yii helloworld和yii blog性能对比可知,单页面的测试对比成型系统(哪怕只是一个简单的blog),根本不是一回事,毫无借鉴意义。鉴于未发现构建于yaf之上的cms系统,因此只能通过yaf和yii的helloworld对比,简单推导出yaf blog的性能,应该大约是279请求/秒。
本来我是用drupal作为参考对比,但测试后意外发现,drupal6的性能居然比yii blog还好。要知道,一个裸奔的drupal,已经包含文章发布/多用户管理/权限设置/日志访问等基本功能,远比yii blog这个演示版本复杂。
但是不是可以下结论,drupal做出来的站点比yii性能更好?也未必,因为drupal架构决定了,安装模块越多性能下降越快,而且是不可控制的。也许当功能复杂到某个程度时,达到某个临界点,drupal的性能就会被yii赶上也未可知。目前只可以说,这些框架还是系统,其实都是一个数量级的,习惯用哪个就用哪个呗。
对了,许多人提起drupal,都会说白宫的网站就是drupal做的,其实白宫的访问量连drupal的官网都远比不上,而最牛的drupal站点当属examiner.com,alexa排名一千以内。而yii呢?据说19楼就是基于它开发的,也不错。
评论