php实现的thrift socket server

转载自:  http://www.ooso.net/archives/537

这些天用php写了个thrift的socket server,因为原来thrift的源码里php部分只有基于apache的服务器端代码,再加上前些日子看到php也能直接使用libevent构建web服务器,所以才会想到写这个玩玩。

php-thrift-server源码

代码直接从apache的thrift项目clone过来,托管在github上:

http://github.com/volca/thrift

新增或改动的代码如下:

lib/php/`-- src
    |-- server
    |   |-- TNonblockingServer.php
    |   `--TServer.php
    `-- transport
        |-- TNonblockingServerSocket.php
        |-- TNonblockingSocket.php
        |-- TServerSocket.php
        |-- TServerTransport.php
test/php
|-- TestClient.php
|-- TestNonblockingServer.php

使用示例

获取thrift的源码,并编译出thrift工具,编译过程请搜索


git clone git://github.com/volca/thrift.git

安装php,以及apc, libevent扩展:


pecl install apc
#需要先libevent-devel之类的包包
pecl install libevent

运行php的socket服务器,我直接从thrift的test代码中修改了一个独立运行的php server,见thrift/test/php/TestNonblockingServer.php,这里也包含一个测试业务代码的实现。


cd thrift/test/php
#用thrift命令行工具生成php的测试类库
make 
#启动thrift服务,会监听本机的9090端口
php TestNonblockingServer.php

客户端的代码也一并提供,对各种数据类型比如int, float, string, list等等进行测试。

扫描二维码关注公众号,回复: 279331 查看本文章

php TestClient.php

性能测试

apache + php的测试结果

testVoid()=void
testString("Test")="Test"
testByte(1)=1
testI32(-1)=-1
testI64(-34359738368)=-34359738368
testDouble(-852.234234234)=-852.234234234
testStruct({"Zero",1,-3,-5})={"Zero",1,-3,-5}
testNest({1,{"Zero",1,-3,-5}),5}={1,{"Zero",1,-3,-5},5}
testMap({0=>-10,1=>-9,2=>-8,3=>-7,4=>-6})={0=>-10,1=>-9,2=>-8,3=>-7,4=>-6}
testSet({-2,-1,0,1,2})={1,1,1,1,1}
testList({-2,-1,0,1,2})={-2,-1,0,1,2}
testEnum(ONE)=1
testEnum(TWO)=2
testEnum(THREE)=3
testEnum(FIVE)=5
testEnum(EIGHT)=8
testTypedef(309858235082523)=309858235082523Total time:41 ms

php + libevent的socket server测试结果

testVoid()=void
testString("Test")="Test"
testByte(1)=1
testI32(-1)=-1
testI64(-34359738368)=-34359738368
testDouble(-852.234234234)=-852.234234234
testStruct({"Zero",1,-3,-5})={"Zero",1,-3,-5}
testNest({1,{"Zero",1,-3,-5}),5}={1,{"Zero",1,-3,-5},5}
testMap({0=>-10,1=>-9,2=>-8,3=>-7,4=>-6})={0=>-10,1=>-9,2=>-8,3=>-7,4=>-6}
testSet({-2,-1,0,1,2})={1,1,1,1,1}
testList({-2,-1,0,1,2})={-2,-1,0,1,2}
testEnum(ONE)=1
testEnum(TWO)=2
testEnum(THREE)=3
testEnum(FIVE)=5
testEnum(EIGHT)=8
testTypedef(309858235082523)=309858235082523Total time:8 ms

这个测试中,没有耗时很长的请求,处理逻辑完全一样,php socket server耗时仅为apache + php的五分之一。

猜你喜欢

转载自hongtoushizi.iteye.com/blog/2333841
今日推荐