1.4. Yaf的性能

对比

为了极限的看出Yaf框架的性能如何, 作者并没有和其他框架做对比, 而是和原生的PHP做对比, 测试代码如下:

例 1.1. 1.测试用原生的PHPorig.php

                                         
<?php
class IndexController {
        public function actionIndex() {
                echo "Laruence";
        }       
}

$controller = new IndexController();
$controller->actionIndex();
?>
                                           
                          

例 1.2. 2.测试用的Yaf的入口文件ap.php

                                         
<?php
$conf = array(
        "application.directory" => "/home/laruence/local/www/htdocs/ap",
);

$app = new Yaf_Application($conf);
$app->run();
                                           
                          

例 1.3. 2.测试用的Yaf的默认控制器Index.php

                                         
<?php
class IndexController extends Yaf_Controller {
        public function actionIndex() {
                $this->disableView(); //关闭视图输出
                echo "Laruence";
        }
}
?>
                                           
                          

测试结果

作者简单的采用了ab作为测试工具, 分别在并发1, 100, 200的情况下对俩者做了测试.

1个并发

例 1.4. 请求1000次, 原生的PHP

                                         
$ ./ab -n1000 -c1 http://127.0.0.1/orig.php

Document Path:          orig.php
Document Length:        8 bytes

Concurrency Level:      1
Time taken for tests:   0.463 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      130000 bytes
HTML transferred:       8000 bytes
Requests per second:    2159.41 [#/sec] (mean)
Time per request:       0.463 [ms] (mean)
Time per request:       0.463 [ms] (mean, across all concurrent requests)
Transfer rate:          274.14 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.2      0       5
Waiting:        0    0   0.2      0       5
Total:          0    0   0.2      0       5

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      0
  98%      0
  99%      1
 100%      5 (longest request)

                                   
                  

例 1.5. 请求1000次, Yaf

                                 
$ ./ab -n1000 -c1 http://127.0.0.1/ap/index.php

Document Path:          /ap/index.php
Document Length:        8 bytes

Concurrency Level:      1
Time taken for tests:   0.525 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      130000 bytes
HTML transferred:       8000 bytes
Requests per second:    1906.24 [#/sec] (mean)
Time per request:       0.525 [ms] (mean)
Time per request:       0.525 [ms] (mean, across all concurrent requests)
Transfer rate:          242.00 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.3      0       7
Waiting:        0    0   0.3      0       7
Total:          0    0   0.3      1       7
ERROR: The median and mean for the total time are more than twice the standard
       deviation apart. These results are NOT reliable.

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      7 (longest request)
                                   
                  

100个并发

例 1.6. 请求1000次, 原生的PHP

                                         
$ ./ab -n1000 -c100 http://127.0.0.1/orig.php

Document Path:          orig.php
Document Length:        8 bytes

Concurrency Level:      100
Time taken for tests:   0.287 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      130000 bytes
HTML transferred:       8000 bytes
Requests per second:    3478.82 [#/sec] (mean)
Time per request:       28.745 [ms] (mean)
Time per request:       0.287 [ms] (mean, across all concurrent requests)
Transfer rate:          441.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.0      0       6
Processing:     5   27   4.8     27      35
Waiting:        5   27   4.8     27      35
Total:          6   27   4.6     27      36

Percentage of the requests served within a certain time (ms)
  50%     27
  66%     28
  75%     29
  80%     31
  90%     35
  95%     35
  98%     35
  99%     35
 100%     36 (longest request)
                                   
                  

例 1.7. 请求1000次, Yaf

                                 
$ ./ab -n1000 -c100 http://127.0.0.1/ap/index.php

Document Path:          /ap/index.php
Document Length:        8 bytes

Concurrency Level:      100
Time taken for tests:   0.316 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      130000 bytes
HTML transferred:       8000 bytes
Requests per second:    3165.24 [#/sec] (mean)
Time per request:       31.593 [ms] (mean)
Time per request:       0.316 [ms] (mean, across all concurrent requests)
Transfer rate:          401.84 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.0      0       6
Processing:     6   30   5.6     27      44
Waiting:        6   30   5.6     27      44
Total:          6   30   5.6     27      44

Percentage of the requests served within a certain time (ms)
  50%     27
  66%     32
  75%     34
  80%     36
  90%     37
  95%     40
  98%     42
  99%     42
 100%     44 (longest request)
                                   
                  

说明

在测试的过程中, 通过vmstat观察, 机器的Idel一直保持在50-60左右.

总体来看, Yaf的性能比起原生的PHP, 损失的程度在10%左右, 另外考虑到因为Yaf有一次IO操作(载入Controller), 而原生的PHP并没有, 那么基本可以认为使用了Yaf框架以后, 性能损失在10%以内.

[重要] 重要
在测试的过程中, 并没有多次挑选最好的数据来把测试结果弄的漂亮点. 因为有一些时候, Yaf的性能几乎和原生的PHP的性能差别在2%以内.

最后, 要说明一点: 测试结果, 只是一个简单的说明, 并不是为了证明什么结论. 因为框架的时间和真正应用逻辑的耗时比起来, 真的是很小的一部分.