前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何用PHP写网站压力测试工具

如何用PHP写网站压力测试工具

作者头像
用户8099761
发布2023-05-11 13:23:49
4.2K0
发布2023-05-11 13:23:49
举报
文章被收录于专栏:私人订制

免职说明

  • 该文章请以学习的角度以及系统做高并发压力测试进行阅读。
  • 请勿使用本代码对任何网站做压力测试以及恶意攻击。
  • 仅供测试自己的网站,禁止非法使用,否则后果自负!

该压力测试工具使用了php的Swoole协程扩展,以及swoole的连接池,通过连接池来实现一次性请求的并发次数。仅供测试自己的网站,禁止非法使用,否则后果自负!

使用说明

  • php版本>=7.2,并且安装了swoole扩展(如果你是宝塔环境,可以在php扩展里面自行安装)
  • 下载好的工具代码上传到服务器任意地方,然后全部解压出来
  • 在根目录执行命令php start.php
  • 关闭工具,在服务器任意地方执行: kill -9 (ps -ef|grep test|gawk '0 !~/grep/ {print
代码语言:javascript
复制
请求方法:
GET压力测试:http://服务器IP:9000/?url={请求URL地址}&action=get&time={压测时间}&num={并发数量}
POST压力测试:http://服务器IP:9000/?url={请求URL地址}&action=post&time={压测时间}&num={并发数量}&data={urlencode后的post数据}
代码语言:javascript
复制
//修改当前文件资源上限
shell_exec("ulimit -n 100960");

$http = new Swoole\Http\Server("0.0.0.0", 9000, SWOOLE_BASE);

$http->on("start", function (\Swoole\Http\Server $server) {
    swoole_set_process_name("test");
    echo "start success!\n";
});

$http->set(['daemonize' => 1]);

$http->on("request", function (\Swoole\Http\Request $request, \Swoole\Http\Response $response) {
    $response->header("Content-Type", "text/plain");
    $url = (string)$request->get['url'];
    $action = strtolower((string)$request->get['action']);
    $time = (int)$request->get['time'] + time(); //如果这个小于了当前时间表示过期了
    $num = (int)$request->get['num'];
    $data = urldecode((string)$request->get['data']);
    if ($action != "get" && $action != "post") {
        $response->end("action error");
        return;
    }
    \Swoole\Coroutine::create(function () use ($data, $action, $num, $url, $time) {
        $connectionPool = new \Swoole\ConnectionPool(function () use ($url, $time) {
            return new Request($url);
        }, $num);
        $connectionPool->fill();
        $i = 1;
        while (true) {
            if ($time < time()) {
                $connectionPool->close();
                break;
            }
            $request = $connectionPool->get();
            if ($request instanceof Request) {
                \Swoole\Coroutine::create(function () use ($data, $action, $connectionPool, $i, $request) {
                    try {
                        if ($action == 'get') {
                            $request->get();
                        } else if ($action == 'post') {
                            $request->post($data);
                        }
                        $connectionPool->put($request);
                    } catch (Exception $e) {
                        $connectionPool->put($request);
                    }
                });
            }
            $i++;
        }
    });
    $message = sprintf('stress tests url:%s - thread:%s - expire:%s', $url, $num, date("Y/m/d H:i:s", $time));
    @$response->end((string)$message);
});

$http->start();
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-1-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 免职说明
  • 使用说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档