Hprose 和 Yar 的性能比较

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

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

公共 API

api.php

<?php
define("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

<?php
include("Hprose.php");
include("api.php");

$server = new HproseHttpServer();
$server->addInstanceMethods(new API());
$server->start();

hprose_client.php

<?php
include("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

<?php
include("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

<?php
include("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

<?php
include("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

<?php
include("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

<?php
include("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秒

转载于:https://my.oschina.net/andot/blog/465003

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值