Hprose 和 Yar 的性能比较

Stella981
• 阅读 387

之前总有人问我 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;

测试结果

下面是测试结果:

服务器与客户端

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秒

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Karen110 Karen110
2年前
​一篇文章总结一下Python库中关于时间的常见操作
前言本次来总结一下关于Python时间的相关操作,有一个有趣的问题。如果你的业务用不到时间相关的操作,你的业务基本上会一直用不到。但是如果你的业务一旦用到了时间操作,你就会发现,淦,到处都是时间操作。。。所以思来想去,还是总结一下吧,本次会采用类型注解方式。time包importtime时间戳从1970年1月1日00:00:00标准时区诞生到现在
Stella981 Stella981
2年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Easter79 Easter79
2年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这