thrift socket server implemented by php

Reprinted from:   http://www.ooso.net/archives/537

 

 

I wrote a thrift socket server with php these days, because the php part of the thrift source code only has server-side code based on apache, and I saw that php can also directly use libevent to build a web server a few days ago, so I thought of it Write this for fun.

php-thrift-server source code

The code is cloned directly from apache's thrift project and hosted on github:

http://github.com/volca/thrift

The added or modified code is as follows:

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

Example of use

Obtain the source code of thrift and compile the thrift tool. Please search for the compilation process.


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

Install php, as well as apc, libevent extensions:


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

To run the php socket server, I directly modified an independent running php server from the test code of thrift, see thrift/test/php/TestNonblockingServer.php, which also contains an implementation of the test business code.


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

The client-side code is also provided to test various data types such as int, float, string, list, etc.


php TestClient.php

Performance Testing

apache + php test results

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

Socket server test results of php + libevent

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

In this test, there are no requests that take a long time, the processing logic is exactly the same, and the php socket server time is only one-fifth of that of apache + php.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614114&siteId=291194637