Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78cdda5

Browse files
author
linzhong
committed
测试网络框架切换到swoole和使用异步swoole_redis带来的性能改变情况
1 parent 6c9c55c commit 78cdda5

3 files changed

Lines changed: 231 additions & 0 deletions

File tree

DqSwooleClient.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
class DqSwooleClient
3+
{
4+
5+
6+
private $serverList = array();
7+
private $fd = NULL;
8+
9+
public function addServer($server)
10+
{
11+
if (is_string($server)) {
12+
$this->serverList[] = $server;
13+
}
14+
if (is_array($server) && !empty($server)) {
15+
$this->serverList = array_merge($this->serverList, $server);
16+
}
17+
$this->serverList = array_unique($this->serverList);
18+
}
19+
20+
21+
public function connect()
22+
{
23+
if (!empty($this->fd)) {
24+
return $this->fd;
25+
}
26+
if (empty($this->serverList)) {
27+
DqLog::writeLog('empty server list');
28+
return false;
29+
}
30+
$serverList = $this->serverList;
31+
32+
while (count($serverList)) {
33+
try {
34+
$idx = rand(0, count($serverList) - 1);
35+
$server = $serverList[$idx];
36+
list($host, $port) = explode(':', $server);
37+
38+
$fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
39+
//1s内没处理完直接返回
40+
socket_set_option($fd, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 5, "usec" => 0));
41+
if (!is_resource($fd)) {
42+
$strMsg = 'socket_create error:' . socket_strerror(socket_last_error());
43+
throw new DqException($strMsg);
44+
}
45+
if (!socket_connect($fd, $host, $port)) {
46+
$strMsg = 'socket_create error:' . socket_strerror(socket_last_error() . ' ip=' . $host . ' port=' . $port);
47+
throw new DqException($strMsg);
48+
}
49+
$this->fd = $fd;
50+
return $fd;
51+
} catch (DqException $e) {
52+
unset($this->serverList[$idx]); //删除无用的配置
53+
54+
unset($serverList[$idx]);
55+
$serverList = array_values($serverList);
56+
57+
DqLog::writeLog($e->getDqMessage(), DqLog::LOG_TYPE_EXCEPTION);
58+
}
59+
60+
}
61+
return false;
62+
}
63+
64+
public function add($topic,$data){
65+
try{
66+
$fd = $this->connect();
67+
if($fd===false){
68+
throw new DqException(' connect server faild');
69+
}
70+
$data['cmd']='add';
71+
$data['topic'] = $topic;
72+
$message = json_encode($data);
73+
$sendMessage = pack("N", strlen($message)) . $message;
74+
if(socket_write($fd,$sendMessage)){
75+
echo socket_read($fd,1024)."\n\n";
76+
}else{
77+
echo 'send message fail'."\n";
78+
}
79+
80+
}catch (DqException $e){
81+
DqLog::writeLog($e->getDqMessage(),DqLog::LOG_TYPE_EXCEPTION);
82+
}
83+
return false;
84+
}
85+
86+
}

test_swoole_bench.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
include_once 'DqLoader.php';
4+
include_once 'DqClient.php';
5+
include_once 'DqComm.php';
6+
include_once 'DqSwooleClient.php';
7+
8+
9+
class DqBench extends Thread{
10+
private $name;
11+
public function __construct($name){
12+
$this->name = $name;
13+
}
14+
static $concurrency=10;
15+
static $nums = 2;
16+
17+
function run(){
18+
$server=array(
19+
'127.0.0.1:9055',
20+
);
21+
$time = self::msectime();
22+
$dqClient = new DqSwooleClient();
23+
$dqClient->addServer($server);
24+
25+
$topic ='mytest';
26+
for($i=0;$i<self::$nums;$i++) {
27+
$id = uniqid();
28+
$data = array(
29+
'id' => $id,
30+
'body' => array(
31+
'a' => 1,
32+
'b' => 2,
33+
'c' => 3,
34+
'ext' => str_repeat('a', 128),
35+
),
36+
//可选,设置后以这个通知时间为准,默认延时时间在注册topic的时候指定
37+
//'fix_time' => date('Y-m-d 23:50:50'),
38+
);
39+
$boolRet = $dqClient->add($topic, $data);
40+
echo 'add耗时:'.(self::msectime() - $time)."ms\n";
41+
}
42+
}
43+
44+
static function msectime() {
45+
list($msec, $sec) = explode(' ', microtime());
46+
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
47+
return $msectime;
48+
}
49+
}
50+
51+
DqBench::$concurrency =$_SERVER['argv'][1];
52+
DqBench::$nums = $_SERVER['argv'][2];
53+
54+
55+
56+
57+
for($i=0;$i<DqBench::$concurrency;$i++){
58+
$pool[] = new DqBench("name:".$i);
59+
}
60+
61+
$start = DqBench::msectime();
62+
foreach($pool as $worker){
63+
$worker->start();
64+
}
65+
66+
67+
foreach($pool as $worker) {
68+
$worker->join();//等待执行完成
69+
}
70+
71+
echo "总耗时:".(DqBench::msectime()-$start)."ms\n";
72+
73+
// php DqBench 100 2

test_swoole_server.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
include_once 'DqLoader.php';
3+
4+
5+
6+
$port = $_SERVER['argv'][1];
7+
8+
try {
9+
$serv = new swoole_server(DqConf::getLocalHost(), $port);
10+
$serv->set(array(
11+
'open_length_check' => true,
12+
'package_max_length' => 4096,
13+
'package_length_type' => 'N', //see php pack()
14+
'package_length_offset' => 0,
15+
'package_body_offset' => 4,
16+
));
17+
18+
19+
$serv->on('connect', function ($serv, $fd) {
20+
$clientInfo = $serv->getClientInfo($fd);
21+
DqLog::writeLog('new client connet,info='.json_encode($clientInfo));
22+
});
23+
24+
25+
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
26+
$length = unpack("N" , $data)[1];
27+
$data = substr($data,-$length);
28+
//$reply = "Get Message From Client {$fd}:{$length}:{$data}";
29+
//DqLog::writeLog($reply);
30+
//echo $reply."\n";
31+
$arrData=json_decode($data,true);
32+
switch ($arrData['cmd']) {
33+
case 'add':
34+
add($arrData,$serv,$fd);
35+
}
36+
37+
});
38+
$serv->on('close', function ($serv, $fd) {
39+
40+
});
41+
42+
}catch (Exception $e){
43+
echo $e->getMessage();
44+
}
45+
46+
47+
function add($arrData,$serv,$fd){
48+
$client = new Swoole_Redis();
49+
$client->connect('127.0.0.1', 6666, function (swoole_redis $client, $result)use($arrData,$serv,$fd){
50+
$body=json_encode($arrData['body']);
51+
$id = 135;
52+
$topic = $arrData['topic'];
53+
$tid = DqRedis::create_tid($topic,$id);
54+
$hkey='hkeytest';
55+
$score=time()+rand(1,5);
56+
$serv->send($fd, 'this is a test');
57+
$client->hset($hkey, $tid, $body, function (swoole_redis $client, $result) use($serv,$fd,$tid,$score) {
58+
if($result) {
59+
$zkey= 33333;
60+
$client->zadd($zkey, $score, $tid, function (swoole_redis $client, $result) use ($serv, $fd) {
61+
DqRedis::incr_nums(1,2);
62+
$serv->send($fd, 'this is a test');
63+
});
64+
}else{
65+
$serv->send($fd, 'this is a test');
66+
}
67+
});
68+
});
69+
70+
}
71+
72+
$serv->start();

0 commit comments

Comments
 (0)