Init Repo

This commit is contained in:
root
2019-09-06 23:53:10 +08:00
commit f0ef89dfbb
7905 changed files with 914138 additions and 0 deletions

4
vendor/swoole/benchmark/ab.sh vendored Executable file
View File

@ -0,0 +1,4 @@
ab -c 1000 -n 1000000 -k http://127.0.0.1:9501/
ab -c 1000 -n 1000000 -k -p post.data -T 'application/x-www-form-urlencoded' http://127.0.0.1:9501/
#post 24K
ab -c 100 -n 100000 -k -p post.big.data -T 'application/x-www-form-urlencoded' http://127.0.0.1:9501/

253
vendor/swoole/benchmark/async.php vendored Executable file
View File

@ -0,0 +1,253 @@
<?php
//关闭错误输出
//error_reporting(0);
$shortopts = "c:";
$shortopts .= "n:";
$shortopts .= "s:";
$shortopts .= "f:";
$shortopts .= "p::";
$opt = getopt($shortopts);
//并发数量
if (!isset($opt['c'])) {
exit("require -c [process_num]. ep: -c 100\n");
}
if (!isset($opt['n'])) {
exit("require -n [request_num]. ep: -n 10000\n");
}
if (!isset($opt['s'])) {
exit("require -s [server_url]. ep: -s tcp://127.0.0.1:9999\n");
}
if (!isset($opt['f'])) {
exit("require -f [test_function]. ep: -f websocket|http|tcp|udp\n");
}
class BenchMark
{
protected $nConcurrency;
protected $nRequest;
protected $host;
protected $port;
protected $clients = array();
protected $nRecvBytes = 0;
protected $nSendBytes = 0;
protected $requestCount = 0;
protected $connectErrorCount = 0;
protected $connectTime = 0;
protected $startTime;
protected $beginSendTime;
protected $testMethod;
static $sentData = "hello world\n";
function __construct($opt)
{
$this->nConcurrency = $opt['c'];
$this->nRequest = $opt['n'];
$serv = parse_url($opt['s']);
$this->host = $serv['host'];
$this->port = $serv['port'];
$this->testMethod = $opt['f'];
if (!method_exists($this, $this->testMethod))
{
throw new RuntimeException("method [{$this->testMethod}] is not exists.");
}
}
protected function finish()
{
foreach($this->clients as $k => $cli)
{
/**
* @var $cli swoole\client
*/
if ($cli->isConnected())
{
$cli->close();
}
unset($this->clients[$k]);
}
echo "============================================================\n";
echo " Swoole Version ".SWOOLE_VERSION."\n";
echo "============================================================\n";
echo "{$this->requestCount}\tbenchmark tests is finished.\n";
echo "SendBytes:\t".number_format($this->nSendBytes)."\n";
echo "nReceBytes:\t".number_format($this->nRecvBytes)."\n";
echo "concurrency:\t".$this->nConcurrency,"\n";
echo "connect failed:\t" . $this->connectErrorCount, "\n";
echo "request num:\t" . $this->nRequest, "\n";
$costTime = $this->format(microtime(true) - $this->startTime);
echo "total time:\t" . ($costTime) . "\n";
if ($this->requestCount > 0)
{
echo "request per second:\t" . intval($this->requestCount / $costTime), "\n";
}
else
{
echo "request per second:\t0\n";
}
echo "connection time:\t" . $this->format($this->connectTime) . "\n";
}
function format($time)
{
return round($time, 4);
}
function onReceive($cli, $data)
{
$this->nRecvBytes += strlen($data);
/**
* 请求已经发完了,关闭连接,等待所有连接结束
*/
if ($this->requestCount >= $this->nRequest)
{
$cli->close();
unset($this->clients[$cli->sock]);
if (count($this->clients) == 0)
{
$this->finish();
}
}
else
{
$this->send($cli);
}
}
function send($cli)
{
$data = self::$sentData;
$cli->send($data);
$this->nSendBytes += strlen($data);
$this->requestCount++;
}
function push($cli)
{
$data = self::$sentData;
$cli->push($data);
$this->nSendBytes += strlen($data);
$this->requestCount++;
}
function onClose($cli)
{
//echo "close\n";
}
function onError($cli)
{
$this->connectErrorCount ++;
if ($this->connectErrorCount >= $this->nConcurrency)
{
$this->finish();
}
}
function onConnect($cli)
{
$this->send($cli);
}
function websocket()
{
$cli = new swoole\http\client($this->host, $this->port);
$cli->set(array('websocket_mask' => true));
$cli->on('Message', function($cli, $frame) {
$this->nRecvBytes += strlen($frame->data);
/**
* 请求已经发完了,关闭连接,等待所有连接结束
*/
if ($this->requestCount >= $this->nRequest)
{
$cli->close();
unset($this->clients[$cli->sock]);
if (count($this->clients) == 0)
{
$this->finish();
}
}
else
{
$this->push($cli);
}
});
$cli->upgrade('/', function ($cli) {
$this->push($cli);
});
return $cli;
}
function long_tcp()
{
$cli = new swoole\client(SWOOLE_TCP | SWOOLE_ASYNC);
$cli->on('receive', [$this, 'onReceive']);
$cli->on('close', [$this, 'onClose']);
$cli->on('connect', [$this, 'onConnect']);
$cli->on('error', [$this, 'onError']);
$cli->connect($this->host, $this->port);
return $cli;
}
function eof()
{
$eof = "\r\n\r\n";
$cli = new swoole\client(SWOOLE_TCP | SWOOLE_ASYNC);
$cli->set(array('open_eof_check' => true, "package_eof" => $eof));
$cli->on('receive', [$this, 'onReceive']);
$cli->on('close', [$this, 'onClose']);
$cli->on('connect', [$this, 'onConnect']);
$cli->on('error', [$this, 'onError']);
$cli->connect($this->host, $this->port);
self::$sentData .= $eof;
return $cli;
}
function length()
{
$cli = new swoole\client(SWOOLE_TCP | SWOOLE_ASYNC);
$cli->set(array(
'open_length_check' => true,
"package_length_type" => 'N',
'package_body_offset' => 4,
));
$cli->on('receive', [$this, 'onReceive']);
$cli->on('close', [$this, 'onClose']);
$cli->on('connect', [$this, 'onConnect']);
$cli->on('error', [$this, 'onError']);
$cli->connect($this->host, $this->port);
self::$sentData = pack('N', strlen(self::$sentData)) . self::$sentData;
return $cli;
}
function udp()
{
$cli = new swoole\client(SWOOLE_UDP | SWOOLE_ASYNC);
$cli->on('receive', [$this, 'onReceive']);
$cli->on('close', [$this, 'onClose']);
$cli->on('connect', [$this, 'onConnect']);
$cli->on('error', [$this, 'onError']);
$cli->connect($this->host, $this->port);
return $cli;
}
function run()
{
$this->startTime = microtime(true);
for ($i = 0; $i < $this->nConcurrency; $i++)
{
$cli = call_user_func([$this, $this->testMethod]);
$this->clients[$cli->sock] = $cli;
}
$this->beginSendTime = microtime(true);
$this->connectTime = $this->beginSendTime - $this->startTime;
}
}
$bench = new BenchMark($opt);
$bench->run();

84
vendor/swoole/benchmark/coroutine.php vendored Executable file
View File

@ -0,0 +1,84 @@
<?php
class Server
{
public $server;
public $redisPool = [];
public $mysqlPool = [];
public function run()
{
$this->server = new Swoole\Http\Server("0.0.0.0", 9501, SWOOLE_BASE);
$this->server->set([
'worker_num' => 1,
]);
// $this->server->on('Connect', [$this, 'onConnect']);
$this->server->on('Request', [$this, 'onRequest']);
// $this->server->on('Close', [$this, 'onClose']);
$this->server->start();
}
function log($msg)
{
echo $msg."\n";
}
public function onConnect($serv, $fd, $reactorId)
{
// $this->log("onConnect: fd=$fd");
// $redis = new Swoole\Coroutine\Redis();
// $redis->connect('127.0.0.1', 6379);
// $this->redisPool[$fd] = $redis;
}
public function onClose($serv, $fd, $reactorId)
{
// $this->log("onClose: fd=$fd");
// $redis = $this->redisPool[$fd];
// $redis->close();
// unset($this->redisPool[$fd]);
}
public function onRequest($request, $response)
{
// $this->log("onRequest: fd=$request->fd");
if (empty($this->redisPool[$request->fd]))
{
$redis = new Swoole\Coroutine\Redis();
$redis->connect('127.0.0.1', 6379);
$this->redisPool[$request->fd] = $redis;
}
else
{
$redis = $this->redisPool[$request->fd];
}
$ret = $redis->get('key');
if (empty($this->mysqlPool[$request->fd]))
{
$mysql = new Swoole\Coroutine\MySQL();
$mysql->connect([
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'password' => 'root',
'database' => 'test',
]);
}
else
{
$mysql = $this->mysqlPool[$request->fd];
}
$ret2 = $mysql->query('show tables');
$response->end('redis value=' . $ret.', mysql talbes='.var_export($ret2, true));
}
}
$server = new Server();
$server->run();

33
vendor/swoole/benchmark/eof_server.php vendored Executable file
View File

@ -0,0 +1,33 @@
<?php
Swoole\Async::set(array('enable_reuse_port' => true));
$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_BASE);
//$serv = new swoole_server("0.0.0.0", 9502);
$serv->set(array(
'worker_num' => 8,
'open_eof_check' => true,
'package_eof' => "\r\n\r\n",
));
$serv->on('workerstart', function ($server, $id)
{
global $argv;
swoole_set_process_name("php {$argv[0]}: worker");
});
$serv->on('connect', function (swoole_server $serv, $fd, $from_id)
{
//echo "connect\n";;
});
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data)
{
$serv->send($fd, "Swoole: " . $data);
//$serv->close($fd);
});
$serv->on('close', function (swoole_server $serv, $fd, $from_id)
{
//var_dump($serv->connection_info($fd));
//echo "onClose\n";
});
$serv->start();

23
vendor/swoole/benchmark/http.go vendored Executable file
View File

@ -0,0 +1,23 @@
package main
import (
"fmt"
"log"
"net/http"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Last-Modified", "Thu, 18 Jun 2015 10:24:27 GMT")
w.Header().Add("Accept-Ranges", "bytes")
w.Header().Add("E-Tag", "55829c5b-17")
w.Header().Add("Server", "golang-http-server")
fmt.Fprint(w, "<h1>\nHello world!\n</h1>\n")
})
log.Printf("Go http Server listen on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}

8
vendor/swoole/benchmark/http.js vendored Executable file
View File

@ -0,0 +1,8 @@
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {
'Server': "node.js"}
);
res.end("<h1>Hello World</h2>");
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');

15
vendor/swoole/benchmark/http.php vendored Executable file
View File

@ -0,0 +1,15 @@
<?php
$http = new swoole_http_server("127.0.0.1", 9501, SWOOLE_BASE);
$http->set([
'worker_num' => 4,
]);
$http->on('request', function ($request, swoole_http_response $response) {
$response->header('Last-Modified', 'Thu, 18 Jun 2015 10:24:27 GMT');
$response->header('E-Tag', '55829c5b-17');
$response->header('Accept-Ranges', 'bytes');
$response->end("<h1>\nHello Swoole.\n</h1>");
});
$http->start();

27
vendor/swoole/benchmark/http2.go vendored Executable file
View File

@ -0,0 +1,27 @@
package main
import (
"log"
"github.com/valyala/fasthttp"
"runtime"
"fmt"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
m := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/":
ctx.Response.Header.Set("Last-Modified", "Thu, 18 Jun 2015 10:24:27 GMT")
ctx.Response.Header.Set("Accept-Ranges", "bytes")
ctx.Response.Header.Set("E-Tag", "55829c5b-17")
ctx.Response.Header.Set("Server", "golang-http-server")
fmt.Fprint(ctx, "<h1>\nHello world!\n</h1>\n")
default:
}
}
log.Printf("Go fatshttp Server listen on :8888")
log.Fatal(fasthttp.ListenAndServe(":8888", m))
}

34
vendor/swoole/benchmark/length_server.php vendored Executable file
View File

@ -0,0 +1,34 @@
<?php
Swoole\Async::set(array('enable_reuse_port' => true));
$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_BASE);
//$serv = new swoole_server("0.0.0.0", 9502);
$serv->set(array(
'worker_num' => 8,
'open_length_check' => true,
"package_length_type" => 'N',
'package_body_offset' => 4,
));
$serv->on('workerstart', function ($server, $id)
{
global $argv;
swoole_set_process_name("php {$argv[0]}: worker");
});
$serv->on('connect', function (swoole_server $serv, $fd, $from_id)
{
//echo "connect\n";;
});
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data)
{
$serv->send($fd, $data);
//$serv->close($fd);
});
$serv->on('close', function (swoole_server $serv, $fd, $from_id)
{
//var_dump($serv->connection_info($fd));
//echo "onClose\n";
});
$serv->start();

1
vendor/swoole/benchmark/post.big.data vendored Executable file

File diff suppressed because one or more lines are too long

1
vendor/swoole/benchmark/post.data vendored Executable file
View File

@ -0,0 +1 @@
test1=hello&test2=world&myname=rango&go=start&language=php

37
vendor/swoole/benchmark/redis.go vendored Executable file
View File

@ -0,0 +1,37 @@
package main
import (
"fmt"
"log"
"net/http"
"github.com/hoisie/redis"
"runtime"
)
var (
client *redis.Client
)
func main() {
client = &redis.Client{
Addr: "127.0.0.1:6379",
Db: 0,
MaxPoolSize: 10000,
}
// 限制为CPU的数量减一
runtime.GOMAXPROCS( runtime.NumCPU() - 1 )
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
result, err := client.Get("hello")
if err != nil {
fmt.Fprint(w, err.Error())
println(err.Error())
} else {
fmt.Fprint(w, "<h1>Hello world!. result="+ string(result)+"</h1>")
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}

574
vendor/swoole/benchmark/run.php vendored Executable file
View File

@ -0,0 +1,574 @@
<?php
require __DIR__.'/../examples/websocket/WebSocketClient.php';
//关闭错误输出
//error_reporting(0);
$shortopts = "c:";
$shortopts .= "n:";
$shortopts .= "s:";
$shortopts .= "f:";
$shortopts .= "p::";
$opt = getopt($shortopts);
//并发数量
if(!isset($opt['c'])) exit("require -c [process_num]. ep: -c 100\n");
if(!isset($opt['n'])) exit("require -n [request_num]. ep: -n 10000\n");
if(!isset($opt['s'])) exit("require -s [server_url]. ep: -s tcp://127.0.0.1:9999\n");
if(!isset($opt['f'])) exit("require -f [test_function]. ep: -f short_tcp\n");
$bc = new Swoole_Benchmark(trim($opt['f']));
$bc->process_num = (int)$opt['c'];
$bc->request_num = (int)$opt['n'];
$bc->server_url = trim($opt['s']);
$bc->server_config = parse_url($bc->server_url);
$bc->send_data = "GET / HTTP/1.1\r\n";
$bc->send_data .= "Host: www.baidu.com\r\n";
$bc->send_data .= "Connection: keep-alive\r\n";
$bc->send_data .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n";
$bc->send_data .= "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36\r\n\r\n";
$bc->read_len = 65536;
if(!empty($opt['p'])) $bc->show_detail = true;
function eof(Swoole_Benchmark $bc)
{
static $client = null;
static $i;
$start = microtime(true);
if (empty($client)) {
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$client->set(array('open_eof_check' => true, "package_eof" => "\r\n\r\n"));
$end = microtime(true);
$conn_use = $end - $start;
$bc->max_conn_time = $conn_use;
$i = 0;
//echo "connect {$bc->server_url} \n";
if (!$client->connect($bc->server_config['host'], $bc->server_config['port'], 2)) {
error:
echo "Error: " . swoole_strerror($client->errCode) . "[{$client->errCode}]\n";
$client = null;
return false;
}
$start = $end;
}
/*--------写入Sokcet-------*/
$data = str_repeat('A', rand(100, 200))."\r\n\r\n";
if (!$client->send($data))
{
goto error;
}
$end = microtime(true);
$write_use = $end - $start;
if ($write_use > $bc->max_write_time) $bc->max_write_time = $write_use;
$start = $end;
/*--------读取Sokcet-------*/
$i ++;
$ret = $client->recv();
if (empty($ret))
{
echo $bc->pid, "#$i", " is lost\n";
return false;
}
elseif(strlen($ret) != strlen($data))
{
echo "#$i\tlength error\n";
var_dump($ret);
echo "-----------------------------------\n";
var_dump($data);
}
$end = microtime(true);
$read_use = $end - $start;
if ($read_use > $bc->max_read_time) $bc->max_read_time = $read_use;
return true;
}
function long_tcp(Swoole_Benchmark $bc)
{
static $fp = null;
static $i;
$start = microtime(true);
if(empty($fp))
{
$fp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$end = microtime(true);
$conn_use = $end-$start;
$bc->max_conn_time = $conn_use;
$i = 0;
//echo "connect {$bc->server_url} \n";
if (!$fp->connect($bc->server_config['host'], $bc->server_config['port'], 2))
{
error:
echo "Error: ".swoole_strerror($fp->errCode)."[{$fp->errCode}]\n";
$fp = null;
return false;
}
$start = $end;
}
/*--------写入Sokcet-------*/
if (!$fp->send($bc->send_data))
{
goto error;
}
$end = microtime(true);
$write_use = $end - $start;
if ($write_use > $bc->max_write_time)
{
$bc->max_write_time = $write_use;
}
$start = $end;
/*--------读取Sokcet-------*/
while(true)
{
$ret = $fp->recv(65530);
if (empty($ret) or substr($ret, -1, 1) == "\n")
{
break;
}
}
//var_dump($ret);
$i++;
if (empty($ret))
{
echo $bc->pid, "#$i@", " is lost\n";
return false;
}
$end = microtime(true);
$read_use = $end - $start;
if ($read_use > $bc->max_read_time)
{
$bc->max_read_time = $read_use;
}
return true;
}
function websocket(Swoole_Benchmark $bc)
{
static $client = null;
static $i;
$start = microtime(true);
if (empty($client))
{
$client = new WebSocketClient($bc->server_config['host'], $bc->server_config['port']);
if (!$client->connect())
{
echo "connect failed\n";
return false;
}
$end = microtime(true);
$conn_use = $end - $start;
$bc->max_conn_time = $conn_use;
$i = 0;
$start = $end;
}
/*--------写入Sokcet-------*/
if (!$client->send($bc->send_data))
{
echo "send failed\n";
return false;
}
$end = microtime(true);
$write_use = $end - $start;
if ($write_use > $bc->max_write_time)
{
$bc->max_write_time = $write_use;
}
$start = $end;
/*--------读取Sokcet-------*/
$ret = $client->recv();
//var_dump($ret);
$i++;
if (empty($ret))
{
echo $bc->pid, "#$i@", " is lost\n";
return false;
}
$end = microtime(true);
$read_use = $end - $start;
if ($read_use > $bc->max_read_time)
{
$bc->max_read_time = $read_use;
}
return true;
}
/**
* 去掉计时信息的UDP
* @param $bc
* @return bool
*/
function udp(Swoole_Benchmark $bc)
{
static $fp;
if (empty($fp))
{
$fp = stream_socket_client($bc->server_url, $errno, $errstr, 1);
if (!$fp)
{
echo "{$errstr}[{$errno}]\n";
return false;
}
}
/*--------写入Sokcet-------*/
fwrite($fp, $bc->send_data);
/*--------读取Sokcet-------*/
$ret = fread($fp, $bc->read_len);
if (empty($ret))
{
return false;
}
return true;
}
function udp2(Swoole_Benchmark $bc)
{
static $fp;
$start = microtime(true);
if (empty($fp))
{
$u = parse_url($bc->server_url);
$fp = new swoole_client(SWOOLE_SOCK_UDP);
$fp->connect($u['host'], $u['port'], 0.5, 0);
$end = microtime(true);
$conn_use = $end - $start;
$bc->max_conn_time = $conn_use;
$start = $end;
}
/*--------写入Sokcet-------*/
$fp->send($bc->send_data);
$end = microtime(true);
$write_use = $end - $start;
if ($write_use > $bc->max_write_time)
{
$bc->max_write_time = $write_use;
}
$start = $end;
/*--------读取Sokcet-------*/
$ret = $fp->recv();
if (empty($ret))
{
return false;
}
$end = microtime(true);
$read_use = $end - $start;
if ($read_use > $bc->max_read_time)
{
$bc->max_read_time = $read_use;
}
return true;
}
function short_tcp($bc)
{
$fp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
if(!$fp->connect($bc->server_config['host'], $bc->server_config['port'], 1))
{
error:
echo "Error: ".socket_strerror($fp->errCode)."[{$fp->errCode}]\n";
return false;
}
else
{
if(!$fp->send($bc->send_data))
{
goto error;
}
$ret = $fp->recv();
$fp->close();
if(!empty($ret)) return true;
else return false;
}
}
function long_socks5($bc)
{
static $fp = null;
static $i;
$start = microtime(true);
if(empty($fp))
{
$fp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC,5);
$end = microtime(true);
$conn_use = $end-$start;
$bc->max_conn_time = $conn_use;
$i = 0;
//echo "connect {$bc->server_url} \n";
if (!$fp->connect($bc->server_config['host'], $bc->server_config['port'], 2))
{
error:
echo "Error: ".swoole_strerror($fp->errCode)."[{$fp->errCode}]\n";
$fp = null;
return false;
}
$fp->send(pack("C3", 0x05, 0x01, 0x00));//greet
$data = $fp->recv();
$response = unpack("Cversion/Cmethod", $data);
if ($response['version'] != 0x05)
{
exit('SOCKS version is not supported.');
}
$headers = getHeader($bc->send_data);
if (empty($headers['port'])) {
$headers['port'] = 80;
}
$g = pack("C5", 0x05, 0x01, 0x00, 0x03, strlen($headers['host'])) . $headers['host'] . pack("n", $headers['port']);
$fp->send($g);
$data = $fp->recv();
$response = unpack("Cversion/Cresult/Creg/Ctype/Lip/Sport", $data);
if ($response['result'] != 0x00)
{
echo 'SOCKS connection request failed: ' . getSocksRefusalMsg($response['result']), $response['result'];exit;
}
$start = $end;
}
/*--------写入Sokcet-------*/
if (!$fp->send($bc->send_data))
{
goto error;
}
$end = microtime(true);
$write_use = $end - $start;
if ($write_use > $bc->max_write_time)
{
$bc->max_write_time = $write_use;
}
$start = $end;
/*--------读取Sokcet-------*/
while(true)
{
$ret = $fp->recv(65530);
if (empty($ret) or substr($ret, -1, 1) == "\n")
{
break;
}
}
//var_dump($ret);
$i++;
if (empty($ret))
{
echo $bc->pid, "#$i@", " is lost\n";
return false;
}
$end = microtime(true);
$read_use = $end - $start;
if ($read_use > $bc->max_read_time)
{
$bc->max_read_time = $read_use;
}
return true;
}
function getHeader($message)
{
// 标准每行应该以"\r\n"行终止,这里兼容以"\n"作为行终止的情况,所以按"\n"分割行
$lines = explode("\n", $message);
foreach ($lines as &$line)
{
// 按"\n"分割行以后,某些行末可能存在"\r"字符,这里将其过滤掉
$line = rtrim($line, "\r");
}
unset($line);
if (count($lines) <= 0)
{
return false;
}
$headers = [];
foreach ($lines as $line)
{
$pos = strpos($line, ':');
// 非标准首部,抛弃
if ($pos === false)
{
continue;
}
$field = trim(substr($line, 0, $pos));
$value = trim(substr($line, $pos + 1));
// 如果有host头部重新设置host和port
if (strtolower($field) === 'host')
{
$segments = explode(':', $value);
$host = $segments[0];
$headers['host'] = $host;
if (isset($segments[1]))
{
$port = intval($segments[1]);
$headers['port'] = $port;
}
}
$headers[$field] = $value;
}
return $headers;
}
//请求数量最好是进程数的倍数
$bc->process_req_num = intval($bc->request_num/$bc->process_num);
$bc->run();
$bc->report();
$bc->end();
class Swoole_Benchmark
{
public $test_func;
public $process_num;
public $request_num;
public $server_url;
public $server_config;
public $send_data;
public $read_len;
public $time_end;
private $shm_key;
public $main_pid;
public $child_pid = array();
public $show_detail = false;
public $max_write_time = 0;
public $max_read_time = 0;
public $max_conn_time = 0;
public $pid;
protected $tmp_dir = '/tmp/swoole_bench/';
function __construct($func)
{
if (!function_exists($func))
{
exit(__CLASS__ . ": function[$func] not exists\n");
}
if (!is_dir($this->tmp_dir))
{
mkdir($this->tmp_dir);
}
$this->test_func = $func;
}
function end()
{
unlink($this->shm_key);
foreach($this->child_pid as $pid)
{
$f = $this->tmp_dir . 'lost_' . $pid . '.log';
if (is_file($f)) unlink($f);
}
}
function run()
{
$this->main_pid = posix_getpid();
$this->shm_key = $this->tmp_dir.'t.log';
for ($i = 0; $i < $this->process_num; $i++)
{
$this->child_pid[] = $this->start(array($this, 'worker'));
}
for ($i = 0; $i < $this->process_num; $i++)
{
$status = 0;
$pid = pcntl_wait($status);
}
$this->time_end = microtime(true);
}
function init_signal()
{
pcntl_signal(SIGUSR1,array($this, "sig_handle"));
}
function sig_handle($sig)
{
switch ($sig)
{
case SIGUSR1:
return;
}
$this->init_signal();
}
function start($func)
{
$pid = pcntl_fork();
if($pid>0)
{
return $pid;
}
elseif($pid==0)
{
$this->worker();
}
else
{
echo "Error:fork fail\n";
}
}
function worker()
{
$lost = 0;
if(!file_exists($this->shm_key))
{
file_put_contents($this->shm_key,microtime(true));
}
if($this->show_detail) $start = microtime(true);
$this->pid = posix_getpid();
for ($i = 0; $i < $this->process_req_num; $i++)
{
$func = $this->test_func;
if(!$func($this)) $lost++;
}
if ($this->show_detail)
{
$log = $this->pid . "#\ttotal_use(s):" . substr(microtime(true) - $start, 0, 5);
$log .= "\tconnect(ms):" . substr($this->max_conn_time * 1000, 0, 5);
$log .= "\twrite(ms):" . substr($this->max_write_time * 1000, 0, 5);
$log .= "\tread(ms):" . substr($this->max_read_time * 1000, 0, 5);
file_put_contents($this->tmp_dir.'lost_' . $this->pid . '.log', $lost . "\n" . $log);
}
else
{
file_put_contents($this->tmp_dir.'lost_'.$this->pid.'.log', $lost);
}
exit(0);
}
function report()
{
$time_start = file_get_contents($this->shm_key);
$usetime = $this->time_end - $time_start;
$lost = 0;
foreach ($this->child_pid as $f)
{
$file = $this->tmp_dir.'lost_'.$f.'.log';
if (is_file($file))
{
$_lost = file_get_contents($file);
$log = explode("\n",$_lost,2);
}
if (!empty($log))
{
$lost += intval($log[0]);
if ($this->show_detail) echo $log[1], "\n";
}
}
//并发量
echo "concurrency:\t".$this->process_num,"\n";
//请求量
echo "request num:\t".$this->request_num,"\n";
//请求量
echo "lost num:\t".$lost,"\n";
//请求量
echo "success num:\t".($this->request_num-$lost),"\n";
//总时间
echo "total time:\t".substr($usetime,0,5),"\n";
//每秒处理能力
echo "req per second:\t".intval($this->request_num/$usetime),"\n";
//每次请求平均时间ms
echo "one req use(ms):\t".substr($usetime/$this->request_num*1000,0,5),"\n";
}
}

483
vendor/swoole/benchmark/seria_bench.php vendored Executable file
View File

@ -0,0 +1,483 @@
<?php
$target = array (
'battle_id'=> 257
,'user_id'=> 41248
,'user_id2'=> 23989
,'player'=> 41248
,'formation'=> Array ('41248'=> 1 ,'23989'=> 2,'ssss'=>3)
,'result'=> 1
,'battle_type'=> 1
,'speed'=> Array( '41248'=> 0,'23989'=> 0 )
,'attacker'=> Array(
'1'=> Array (
'user_id'=> 41248
,'soldier_id'=> 28
,'prototype_id'=> 4
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3997
,'hp'=> 3997
,'attack_general'=> 346
,'attack_skill'=> 596
,'attack_explode'=> 458
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 2
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0)
,'4'=> Array (
'user_id'=> 41248
,'soldier_id'=> 29
,'prototype_id'=> 2
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3555
,'hp'=> 3555
,'attack_general'=> 396
,'attack_skill'=> 581
,'attack_explode'=> 418
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0
)
,'5'=> Array (
'user_id'=> 41248
,'soldier_id'=> 30
,'prototype_id'=> 6
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3043
,'hp'=> 3043
,'attack_general'=> 351
,'attack_skill'=> 540
,'attack_explode'=> 474
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0)
,'7'=> Array (
'user_id'=> 41248
,'soldier_id'=> 37
,'prototype_id'=> 2
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3491
,'hp'=> 3491
,'attack_general'=> 393
,'attack_skill'=> 532
,'attack_explode'=> 456
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0 ))
,'defender'=> Array(
'2'=> Array(
'user_id'=> 23989
,'soldier_id'=> 24
,'prototype_id'=> 1
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3230
,'hp'=> 3230
,'attack_general'=> 390
,'attack_skill'=> 567
,'attack_explode'=> 442
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0)
,'5'=> Array(
'user_id'=> 23989
,'soldier_id'=> 25
,'prototype_id'=> 2
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3400
,'hp'=> 3400
,'attack_general'=> 379
,'attack_skill'=> 536
,'attack_explode'=> 405
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0 )
,'7'=> Array(
'user_id'=> 23989
,'soldier_id'=> 26
,'prototype_id'=> 6
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3669
,'hp'=> 3669
,'attack_general'=> 362
,'attack_skill'=> 549
,'attack_explode'=> 426
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0 )
,'9'=> Array(
'user_id'=> 23989
,'soldier_id'=> 27
,'prototype_id'=> 1
,'bid'=> 1
,'level'=> 1
,'rare'=> 1
,'skill_id'=> 1
,'totalhp'=> 3618
,'hp'=> 3618
,'attack_general'=> 326
,'attack_skill'=> 510
,'attack_explode'=> 419
,'attack_type'=> 1
,'defense'=> 0
,'anger'=> 50
,'dodge'=> 2
,'crit'=> 2
,'block'=> 0
,'block_effect'=> 0.5
,'crit_effect'=> 2
,'foramtion_effect'=> 0) )
,'battle_process'=> Array(
'0'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'1'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'2'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'3'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'4'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'5'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'6'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'7'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'8'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'9'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'10'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'11'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'12'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'13'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'14'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'15'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'16'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'17'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'18'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'19'=> Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
,'20'=>Array(
'user_id'=> 41248
,'asid'=> 28
,'bsid'=> 'danger'
,'harm'=> 'danger2'
,'dhp'=> Array('0'=> 2019 )
,'attacker_anger'=> 66
,'defender_anger'=> Array('0'=> 94 )
,'skill'=> 0
,'state'=> 0
)
)
);
$json = json_encode($target);
$seri = serialize($target);
$sw = swoole_serialize::pack($target);
var_dump("json :". strlen($json));
var_dump("serialize :". strlen($seri));
var_dump("swoole :". strlen($sw));
$stime = microtime(true);
for ($i = 0; $i < 50000; $i ++) {
json_encode($target);
}
$etime = microtime(true);
var_dump("json_encode :". ($etime - $stime));
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 50000; $i ++) {
json_decode($json. true);
}
$etime = microtime(true);
var_dump("json_decode :". ($etime - $stime));
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 50000; $i ++) {
serialize($target);
}
$etime = microtime(true);
var_dump("serialize :". ($etime - $stime));
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 50000; $i ++) {
unserialize($seri);
}
$etime = microtime(true);
var_dump("unserialize :". ($etime - $stime));
//----------------------------------
//----------------------------------
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 50000; $i ++) {
swoole_serialize::pack($target);
}
$etime = microtime(true);
var_dump("swoole_serialize :". ($etime - $stime));
$stime = microtime(true);
for ($i = 0; $i < 50000; $i ++) {
swoole_serialize::unpack($sw);
}
$etime = microtime(true);
var_dump("swoole_unserialize :". ($etime - $stime));
?>

46
vendor/swoole/benchmark/table.php vendored Executable file
View File

@ -0,0 +1,46 @@
<?php
$table = new swoole_table(1 << 18);
$table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8
$table->column('name', swoole_table::TYPE_STRING, 17623);
$table->column('num', swoole_table::TYPE_FLOAT);
$table->create();
define('N', 100000);
//$worker = new swoole_process('child1', false, false);
//$worker->start();
//
//child
function child1($worker)
{
global $table;
$s = microtime(true);
for($i =0; $i < N; $i++)
{
$table->set('tianfenghan@qq.com', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
$table->set('350749960@qq.com', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415));
$table->set('hello@qq.com', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415));
$table->set('tianfenghan@chelun.com', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
$table->set('12811247@qq.com', array('id' => 1358, 'name' => "Swoole", 'num' => 3.1415));
}
echo "set - ".(5*N)." use: ".round((microtime(true) - $s) * 1000, 4)."ms\n";
}
//master
sleep(1);
child1(1245);
$s = microtime(true);
for($i =0; $i < N; $i++)
{
$arr1 = $table->get('tianfenghan@qq.com');
$arr2 = $table->get('350749960@qq.com');
$arr3 = $table->get('hello@qq.com');
$arr4 = $table->get('tianfenghan@chelun.com');
$arr5 = $table->get('12811247@qq.com');
}
echo "get - ".(5*N)." use: ".round((microtime(true) - $s) * 1000, 4)."ms\n";
$s = microtime(true);
var_dump($arr1, $arr2, $arr3, $arr4, $arr5);

41
vendor/swoole/benchmark/tcp.go vendored Executable file
View File

@ -0,0 +1,41 @@
package main
import (
"bufio"
"fmt"
"net"
"runtime"
)
func Echo(c net.Conn) {
defer c.Close()
for {
line, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
//fmt.Printf("Failure to read:%s\n", err.Error())
return
}
_, err = c.Write([]byte(line))
if err != nil {
//fmt.Printf("Failure to write: %s\n", err.Error())
return
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
fmt.Printf("Server is ready...\n")
l, err := net.Listen("tcp", ":8053")
if err != nil {
fmt.Printf("Failure to listen: %s\n", err.Error())
}
for {
if c, err := l.Accept(); err == nil {
go Echo(c) //new thread
}
}
}

8
vendor/swoole/benchmark/tcp.js vendored Executable file
View File

@ -0,0 +1,8 @@
var net = require('net');
var server = net.createServer(function (socket) {
socket.write("Echo server\r\n");
socket.pipe(socket);
});
server.listen(7001, "127.0.0.1");

31
vendor/swoole/benchmark/tcp.php vendored Executable file
View File

@ -0,0 +1,31 @@
<?php
Swoole\Async::set(array('enable_reuse_port' => true));
$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_BASE);
//$serv = new swoole_server("0.0.0.0", 9502);
$serv->set(array(
'worker_num' => 8,
));
$serv->on('workerstart', function ($server, $id)
{
global $argv;
swoole_set_process_name("php {$argv[0]}: worker");
});
$serv->on('connect', function (swoole_server $serv, $fd, $from_id)
{
//echo "connect\n";;
});
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data)
{
$serv->send($fd, "Swoole: " . $data);
//$serv->close($fd);
});
$serv->on('close', function (swoole_server $serv, $fd, $from_id)
{
//var_dump($serv->connection_info($fd));
//echo "onClose\n";
});
$serv->start();

37
vendor/swoole/benchmark/timer.php vendored Executable file
View File

@ -0,0 +1,37 @@
<?php
const N = 100000;
function test()
{
global $timers;
shuffle($timers);
$stime = microtime(true);
foreach($timers as $id)
{
swoole_timer_clear($id);
}
$etime = microtime(true);
echo "del ".N." timer :". ($etime - $stime)."s\n";
}
class TestClass
{
static function timer()
{
}
}
$timers = [];
$stime = microtime(true);
for($i = 0; $i < N; $i++)
{
$timers[] = swoole_timer_after(rand(1, 9999999), 'test');
//swoole_timer_after(rand(1, 9999999), function () {
// echo "hello world\n";
//});
//swoole_timer_after(rand(1, 9999999), array('TestClass', 'timer'));
}
$etime = microtime(true);
echo "add ".N." timer :". ($etime - $stime)."s\n";
swoole_event_wait();

36
vendor/swoole/benchmark/udp.php vendored Executable file
View File

@ -0,0 +1,36 @@
<?php
//Swoole\Async::set(array('enable_reuse_port' => true));
//$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_BASE, SWOOLE_SOCK_UDP);
$serv = new swoole_server("0.0.0.0", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
$serv->set(array(
'dispatch_mode' => 1,
'worker_num' => 8, //worker process num
// //'log_file' => '/tmp/swoole.log',
// //'daemonize' => true,
));
function my_onStart($serv)
{
echo "MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}\n";
echo "Server: start.Swoole version is [" . SWOOLE_VERSION . "]\n";
}
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
//var_dump($serv->connection_info($fd, $from_id));
//echo "worker_pid=".posix_getpid().PHP_EOL;
//var_dump($fd, $from_id);
$serv->send($fd, 'Swoole: ' . $data, $from_id);
}
function my_onPacket(swoole_server $serv, $data, $addr)
{
// var_dump($addr);
$serv->sendto($addr['address'], $addr['port'], 'Swoole: ' . $data);
}
$serv->on('Start', 'my_onStart');
$serv->on('Receive', 'my_onReceive');
//$serv->on('Packet', 'my_onPacket');
$serv->start();

25
vendor/swoole/benchmark/websocket.php vendored Executable file
View File

@ -0,0 +1,25 @@
<?php
//$server = new swoole_websocket_server("0.0.0.0", 9502);
$server = new swoole_websocket_server("0.0.0.0", 9502, SWOOLE_BASE);
$server->set(['worker_num' => 4]);
//
//$server->on('open', function (swoole_websocket_server $_server, swoole_http_request $request) {
// //echo "server#{$_server->worker_pid}: handshake success with fd#{$request->fd}\n";
//
//// var_dump($request);
//});
$server->on('message', function (swoole_websocket_server $_server, $frame) {
//var_dump($frame);
//echo "received ".strlen($frame->data)." bytes\n";
//echo "receive from {$fd}:{$data},opcode:{$opcode},fin:{$fin}\n";
$_server->push($frame->fd, "server:" . $frame->data);
// $_server->close($frame->fd);
});
//$server->on('close', function ($_server, $fd) {
// echo "client {$fd} closed\n";
//});
$server->start();