public function main()
{
$worker = new Worker();
$worker->onWorkerStart = function () {
Timer::add(1, function () {
//这里的代码,并不能在一秒内执行完成
for ($i = 0; $i <= 1000; $i++) {
$this->wsClient();
}
});
Timer::add(1, function () {
dump("qps :{$this->qps}");
$count = count($this->conns);
dump("连接数:$count");
$this->qps = 0;
});
};
$this->worker = new Worker('websocket://127.0.0.1:7272');
$this->worker->onConnect = function (ConnectionInterface $conn) {
$id = spl_object_hash($conn);
$this->conns[$id] = $conn;
};
$this->worker->onConnect = function (ConnectionInterface $conn) {
$id = spl_object_hash($conn);
$this->conns[$id] = $conn;
};
$this->worker->onClose = function (ConnectionInterface $conn) {
$id = spl_object_hash($conn);
unset($this->conns[$id]);
};
Worker::runAll();
}
private function wsClient()
{
$ws = new AsyncTcpConnection('ws://127.0.0.1:7272');
$ws->onConnect = function (ConnectionInterface $conn) {
Timer::add(1, function () use ($conn) {
$conn->send('hello world');
$this->qps++;
});
};
$ws->onError = function ($conn, $code, $msg) {
dump($msg);
};
$ws->connect();
}
机器是m1 MacBook air 16G
无论是用event还是select都是差不多的每秒连接数
笔记本有文件打开数限制,有的默认可能就256
压测上linux服务器吧
我笔记本ulimit -n 10240
我上服务器确实速度飞快(比我笔记本配置低多了),谢谢老大解答
count($this->conns);这种写法只支持单台ws服务,如果分布式使用什么方式合适?GlobalData变量共享组件?
这种东西写到redis吧,我上面代码写错了,实际上统计不到,两个worker不是一个进程