首页 > 作文

压测 swoole

更新时间:2023-04-07 03:50:04 阅读: 评论:0

概述

这是关于 swoole 入门学习的第十篇文章:压测 swoole_websocket_rver 性能。

第九篇:swoole redis 连接池的实现第八篇:swoole mysql 连接池的实现第七篇:swoole rpc 的实现第六篇:swoole 整合成一个小框架第五篇:swoole 多协议 多端口 的应用第四篇:swoole http 的应用第三篇:swoole websocket 的应用第二篇:swoole task 的应用第一篇:swoole timer 的应用

收到读者提问 “使用 swoole 开发的群聊功能,想知道并发情况,也就是想压测下 qps,一直未找到方法 …”

对 swoole_http_rver 压测,咱们可以使用 apache 的 ab 命令。

对 swoole_websocket_rver 压测,使用 ab 命令是不能压测的,我从网上一直也没找到合适的方法,看官方提供的代码 benchmark/async.php 中,使用的异步模块 swoole\http\client 方法进行压测的,但大学英语四级总分在 swoole 4.3 版本就移除了异步模块,让使用 coroutine 协程模块。

在本地我用 coroutine 协程实现了一下, 测的差不多的时候,一直不确定是否正确,就在 gmentfaul六一节快乐图片t 发了个提问,没想到韩老师回答了,’如果的如果’老师也回答了,非常感谢两位老师的答皖南古村落案,然后整理出文章分享给大家。

测试机

mac 上安装的 parallels desktop 虚拟机

系统:ubuntu 16.04.3 lts

内存:

数量:1核数:2

cpu:

数量:1大小:2g

rver 代码

<?phpclass rver{    private $rv;    public function __construct() {        $this->rv = new swoole\websocket\rver("0.0.0.0", 9501);        $this->rv->t([            'task_worker_num'       => 10,            'enable_coroutine'      => true,            'task_enable_coroutine' => true        ]);        $this->rv->on('open', function ($辽宁师范大学在哪rv, $request) {});        $this->rv->on('message', function ($rv, $frame) {            $rv->task($frame->data);        });        $this->rv->on('task', function ($rv, $task) {            foreach ($rv->connections as $fd) {                $connectioninfo = $rv->connection_info($fd);                if (ist($connectioninfo['websocket_status']) && intval($connectioninfo['websocket_status']) == 3) {                    $rv->push($fd, $task->data);                }            }        });        $this->rv->on('finish', function ($rv, $task_id, $data) {});        $this->rv->on('clo', function ($rv, $fd) {});        $this->rv->start();    }}$rver = new rver();

压测脚本

class test{    protected $concurrency; //并发量    protected $request;     //请求量    protected $requested = 0;    protected $start_time;    function __construct()    {        $this->concurrency = 100;        $this->request     = 10000;    }    protected function websocket()    {        go(function () {            for ($c = 1; $c <= $this->concurrency; $c++ ) {                $cli = new \swoole\coroutine\http\client('127.0.0.1', 9501);                $cli->t(['websocket_mask' => fal]);                $ret = $cli->upgrade('/');                if ($ret) {                    $i = $this->request / $this->concurrency;                    while ($i >= 1) {                        $this->push($cli);                        $cli->recv();                        $i--;                    }                }            }            $this->finish();        });    }    protected function push($cli)    {        $ret = $cli->push('hello world');        if ($ret === true) {            $this->requested ++ ;        }    }    protected function finish()    {        $cost_time = round(microtime(true) - $this->start_time, 4);        echo "concurrency:".$this->concurrency.php_eol;        echo "request num:".$this->request.php_eol;        echo "success num:".$this->requested.php_eol;        echo "total time:".$cost_time.php_eol;        echo "request per cond:" . intval($this->request / $cost_time).php_eol;    }    public function run()    {        $this->start_time = microtime(true);        $this->websocket();    }}$test = new test();$test->run();

压测结果

第 1 次:concurrency:卡通桌面100request num:10000success num:10000total time:0.846request per cond:11820第 2 次:concurrency:100request num:10000success num:10000total time:0.9097request per cond:10992第 3 次:concurrency:100request num:10000success num:10000total time:0.903request per cond:11074

以上是压测结果,供参考。

小结

通过这个压测结果,表明 swoole 的执行效率是杠杠的!

当然还有一些参数是可以调优的,比如:worker_num、max_request、task_worker_num 等。

在真实的业务场景中,肯定会有逻辑处理,也会使用到 mysql、redis。

那么问题来了,前两篇文章已经分享了,swoole redis 连接池、swoole mysql 连接池,感兴趣的同学,可以使用上两种连接池,然后再进行压测。

不知不觉,swoole 入门文章已经写了 10 篇了,非常感谢大家的捧场,真心希望能够对 swoole 入门学习的同学,有点帮助。

本文欢迎转发,转发请注明作者和出处,谢谢!

本文发布于:2023-04-07 03:49:57,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/f89f83e1ae09f327e21014463c4593db.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:压测 swoole.doc

本文 PDF 下载地址:压测 swoole.pdf

标签:连接池   模块   入门   可以使用
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图