之前总有人问我 Hprose 快,还是 Yar 快。这个问题我之前的回答都是,我没有做过测试,但我觉得 Yar 应该更快一些,毕竟他是鸟哥完全用纯 C 实现的。但这个答案好像并不能让大多数人满意。所以在被多人多次询问之后,昨晚我终于没忍住测试了一下,但是结果所反映出的并不是 Hprose 快,还是 Yar 快的问题。测试结果所能确定的问题只有一个,那就是在 Swoole 下跑的 Hprose 比在 Web 服务器上跑(比如 php-fpm 方式)更快。
下面我们先来列一下测试程序。
api.php
<?phpdefine("SEX_UNKNOWN", 0);
define("SEX_MALE", 1);
define("SEX_FEMALE", 2);
define("SEX_INTERSEX", 3);class User { var $name; var $sex; var $birthday; var $age; var $married; function __constructor() {} static function newUser($name, $sex, $birthday, $age, $married) {
$user = new self();
$user->name = $name;
$user->sex = $sex;
$user->birthday = $birthday;
$user->age = $age;
$user->married = $married; return $user;
}
}class API { public function hello($name) { return "hello " . $name . "!";
} public function getUserList() {
$userlist = array(
User::newUser("Amy", SEX_FEMALE, new DateTime("1983-12-03"), 26, true),
User::newUser("Bob", SEX_MALE, new DateTime("1989-06-12"), 20, false),
User::newUser("Chris", SEX_UNKNOWN, new DateTime("1980-03-08"), 29, true),
User::newUser("Alex", SEX_INTERSEX, new DateTime("1992-06-14"), 17, false)
); return $userlist;
}
}hprose_server.php
<?phpinclude("Hprose.php");include("api.php");
$server = new HproseHttpServer();
$server->addInstanceMethods(new API());
$server->start();hprose_client.php
<?phpinclude("Hprose.php");
$client = new HproseHttpClient("http://127.0.0.1/hprose_server.php");echo "<br />";
$t = microtime(true);for ($i = 0; $i < 10000; $i++) $client->hello("world");echo microtime(true) - $t;echo "<br />";
$t = microtime(true);for ($i = 0; $i < 10000; $i++) $client->getUserList();echo microtime(true) - $t;yar_server.php
<?phpinclude("api.php");
$service = new Yar_Server(new API());
$service->handle();yar_client.php
<?php$client = new Yar_Client("http://127.0.0.1/yar_server.php");echo "<br />";
$t = microtime(true);for ($i = 0; $i < 10000; $i++) $client->hello("world");echo microtime(true) - $t;echo "<br />";
$t = microtime(true);for ($i = 0; $i < 10000; $i++) $client->getUserList();echo microtime(true) - $t;hprose_swoole_http_server.php
<?phpinclude("Hprose.php");include("api.php");
$server = new HproseSwooleServer("http://127.0.0.1:8080/");
$server->addInstanceMethods(new API());
$server->start();hprose_swoole_http_client.php
<?phpinclude("Hprose.php");
$client = new HproseHttpClient("http://127.0.0.1:8080/");echo "<br />";
$t = microtime(true);for ($i = 0; $i < 10000; $i++) $client->hello("world");echo microtime(true) - $t;echo "<br />";
$t = microtime(true);for ($i = 0; $i < 10000; $i++) $client->getUserList();echo microtime(true) - $t;hprose_swoole_tcp_server.php
<?phpinclude("Hprose.php");include("api.php");
$server = new HproseSwooleServer("tcp://127.0.0.1:2015/");
$server->addInstanceMethods(new API());
$server->start();hprose_swoole_tcp_client.php
<?phpinclude("Hprose.php");
$client = new HproseSwooleClient("tcp://127.0.0.1:2015");echo "<br />";
$t = microtime(true);for ($i = 0; $i < 10000; $i++) $client->hello("world");echo microtime(true) - $t;echo "<br />";
$t = microtime(true);for ($i = 0; $i < 10000; $i++) $client->getUserList();echo microtime(true) - $t;下面是测试结果:
| 服务器与客户端 | hello | getUserList |
|---|---|---|
| Hprose Swoole TCP | 2.0799078941345秒 | 3.4906399250031 秒 |
| Hprose Swoole HTTP | 2.9583330154419秒 | 4.2354850769043秒 |
| Yar HTTP | 3.8473629951477秒 | 5.1223559379578秒 |
| Hprose HTTP | 4.8670680522919秒 | 6.5057880878448秒 |
