您好,欢迎来到码863代码分享网! 请[登录] / [注册]

Hprose 和 Yar 的性能比较

编辑

之前总有人问我 Hprose 快,还是 Yar 快。这个问题我之前的回答都是,我没有做过测试,但我觉得 Yar 应该更快一些,毕竟他是鸟哥完全用纯 C 实现的。但这个答案好像并不能让大多数人满意。所以在被多人多次询问之后,昨晚我终于没忍住测试了一下,但是结果所反映出的并不是 Hprose 快,还是 Yar 快的问题。测试结果所能确定的问题只有一个,那就是在 Swoole 下跑的 Hprose 比在 Web 服务器上跑(比如 php-fpm 方式)更快。

下面我们先来列一下测试程序。

公共 API

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 HTTP 服务器和客户端

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 HTTP 服务器和客户端

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 服务器和客户端

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 服务器和客户端

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;

测试结果

下面是测试结果:

服务器与客户端hellogetUserList
Hprose Swoole TCP2.0799078941345秒3.4906399250031 秒
Hprose Swoole HTTP2.9583330154419秒4.2354850769043秒
Yar HTTP3.8473629951477秒5.1223559379578秒
Hprose HTTP4.8670680522919秒6.5057880878448秒


CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网 湘ICP备2023018646号-1
MYSQl共执行 3 个查询,用时 0.0018138885498047 秒,PHP脚本用时 0.004134 秒,占用内存 0.605 MB,Gzip 已启用