Feature:Advantage/Performance/zh

From Kiwiphp

Jump to: navigation, search

Contents

前言

相当多的流行PHP框架都面临着性能问题,一些国外同行做了测试,分别用框架实现最简单的打印“Hello World”功能,与原生PHP代码项目,很多框架的QPS(每秒处理的请求数)不及原生PHP的10%:

测试方法

写一个最简单的 Action(kiwiphp中控制器层的一个概念),只是简单的"return true",并不输出任何东西(因为echo "hello world"存在IO开销,可能会是的测试结果对原生PHP不利),然后利用http_load分别测他们的QPS。

这样可以看出,执行一个最简单的任务,使用kiwiphp和最高效的原生PHP代码相比会有多少额外性能开销。

环境

软件

  • OS: RedHat Enterprise Linux Advanced Server 32bit
  • Web Server: Apache 1.3.37
  • PHP 5.2.5,启用了APC

硬件

/bin/hwconfig
Summary:        HP DL140 G3, 2 x Xeon E5310 1.60GHz, 4GB
System:         HP DL140 G3
Processors:     2 x Xeon E5310 1.60GHz (8 cores) - Clovertown G0, 64-bit, quad-core, 65nm, L2: 8MB
Memory:         4GB

测试步骤

原生PHP

简单写个类,执行“return true”:

<?php
class HelloWorldAction
{
       public function execute()
       {
               return true;
       }
}
$obj = new HelloWorldAction();
$obj->execute();


Kiwiphp

创建一个 "Hello World" 应用, 和这个教程略有不同,不输出任何东西只是简单的"return true"。

入口文件

<?php
/**
 * Please replace the relative path(s) with absolute ones
 * It will take some performance benefit
 */
include("/path/to/kiwiphp/trunk/runtime/kiwi.php");
define("ENV_MODE", "product");
Kiwi::run(array(
        "proj_dir" => "/path/to/kiwiphp/trunk/example/share/project1/",
        "app_name" => "tool"
)); 

Action 文件

<?php
class HelloWorldAction extends Action
{
       public function execute()
       {
               return true;
       }
} 

Kiwiphp 会自动把 kiwiphp runtime目录下的文件全部载入:

测量

测量他们的 QPS

$ http_load -parallel 100 -seconds 10 nokiwi.url;  http_load -parallel 100 -seconds 10 kiwi.url
52764 fetches, 100 max parallel, 5.32916e+06 bytes, in 10 seconds
101 mean bytes/connection
5276.39 fetches/sec, 532916 bytes/sec
msecs/connect: 0.104866 mean, 1.744 max, 0.047 min
msecs/first-response: 18.6695 mean, 447.779 max, 4.441 min
HTTP response codes:
  code 200 -- 52764
33330 fetches, 100 max parallel, 3.36633e+06 bytes, in 10 seconds
101 mean bytes/connection
3332.99 fetches/sec, 336632 bytes/sec
msecs/connect: 2.43803 mean, 3000.33 max, 0.042 min
msecs/first-response: 27.2727 mean, 370.932 max, 14.116 min
HTTP response codes:
  code 200 -- 33330

基准测试结果

36041 / 53806 = 63.17%
在这个任务里,kiwi是原生php的63.17%(多测试几次,有浮动,基本在60%-66%之间)。 从绝对时间上看,用kiwiphp会造成每个请求0.01毫秒的延迟(这个是因为服务器配置还比较好,差一些的机器,绝对时间消耗会大一些) 。