Home > PHP Framework > Swoole > body text

Introduction and usage of Swoole's common tool Demonster

WBOY
Release: 2023-06-13 14:56:07
Original
1453 people have browsed it

Swoole常用工具Demonster介绍与使用方法

Swoole是一款常用的高性能网络框架,它的异步多进程模型和强大的网络编程功能,使得它在Web服务、游戏服务等领域有着广泛的应用。然而,开发人员在使用Swoole过程中常常会遇到一些烦琐的问题,例如:如何排查Swoole进程故障、如何分析Swoole进程内存占用情况等等。这些问题对于Swoole的高效运行来说非常重要。因此,本文将向大家介绍一款Swoole常用工具——Demonster。

一、什么是Demonster?

Demonster是一款开源的Swoole常用工具,它能够帮助开发人员排查Swoole进程故障、分析Swoole进程内存占用情况、并监控Swoole进程的状态等等。Demonster提供了一系列命令行工具和UI界面,让开发人员可以方便地操作、查看和分析Swoole进程的各种信息。

二、Demonster的安装

Demonster的安装非常简单,只需要使用Composer即可。步骤如下:

1.在项目根目录下创建composer.json文件,添加以下内容:

{
    "require": {
        "yueliangdali/demonster": "dev-master"
    }
}
Copy after login

2.在命令行中运行composer update命令下载并安装必要的库和依赖。

3.在nginx或apache等Web服务器上配置一个虚拟主机,并将根目录指向Demonster项目的public目录。例如,在nginx服务器上的配置:

server {
    listen 80;
    server_name demo.demonster.com;
    root /var/www/demonster/public;

    location / {
        index index.php;
        if (!-e $request_filename){
            rewrite ^/(.*)$ /index.php?$1 last;
            break;
        }
    }

    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/demonster/public$fastcgi_script_name;
        include        fastcgi_params;
    }
}
Copy after login

4.在Swoole项目中引入Demonster的命名空间和启动类即可开始使用。例如,在一个WebSocket服务器中:

<?php
require_once __DIR__ . '/vendor/autoload.php';//加载Composer自动生成的autoload.php文件
use yueliangdalidemonsterDemonster;
use SwooleWebSocketServer;

$server = new Server("127.0.0.1", 9501);
$server->set([
    'worker_num' => 2,
    'task_worker_num' => 2,
    'daemonize' => false,
]);

Demonster::start();//启动Demonster

$server->on('open', function (Server $server, $request) {
    echo "server: handshake success with fd{$request->fd}
";
    $server->task('hello, task');//触发一个task任务
});

$server->on('message', function (Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}
";
    $server->push($frame->fd, "this is server");
});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed
";
});

$server->on('task', function ($server, $task_id, $worker_id, $data) {
    echo "New AsyncTask[id=$task_id]" . PHP_EOL;
    $server->finish("$data -> OK");
});

$server->on('finish', function ($server, $task_id, $data) {
    echo "AsyncTask[$task_id] Finish: $data" . PHP_EOL;
});

$server->start();
Copy after login

三、Demonster的功能

1.展示服务器的状态

Demonster提供命令demonster status,可以显示服务器的状态,包括:Swoole服务器的死循环是否正在执行、已启动的Worker进程和Task进程数量、TCP和UDP连接数等等。例如:

$ demonster status

                    _____              ____       
                   / ____|            / __       
                  | (___   ___  ___ _| |  | |_ __ 
                   ___  / _ / _  '__| |  | | '__|
                   ____) |  __/  __/ |  | |__| | |   
                  |_____/ ___|___|_|   ____/|_|   
                    
                      Swoole Server Status

Swoole version:4.4.17
 php version:7.2.24
  PHP memory:1.37MB
 
-----------------------------------------  
start at:2021-07-05 12:50:12
swoole version:4.4.17 (reactor_num:2, worker_num:2, task_worker_num:2)
listen:tcp://0.0.0.0:9501

---Processes---
 master  pid:  23291   status: start     memory: 1MB
   ├─worker  pid:  23295   status: start     memory: 2MB
   ├─worker  pid:  23296   status: start     memory: 2MB
   ├─task    pid:  23297   status: start     memory: 2MB
   ├─task    pid:  23298   status: start     memory: 2MB
   └─manager pid:  23294   status: start     memory: 2MB
 
---Connect Info---
TCP LiveConnections: 8
                 Not ESTABConnections: 0
            SWNotsTCP TimeWaitConnections: 0
              UDPLiveConnections: 0
                    Lastreloadtime: 2021-07-06 10:35:38
                           reload: DYNAMIC
Copy after login

2.分析服务器内存使用情况

Demonster提供命令demonster memory,可以显示Swoole服务器的内存情况,包括:进程数量、PID、内存占用情况等等。该命令还可以传递一个-c参数,用于对所有进程的内存占用情况进行统计和排序。例如:

$ demonster memory

                   _____              ____       
                  / ____|            / __       
                 | (___   ___  ___ _| |  | |_ __ 
                  ___  / _ / _  '__| |  | | '__|
                  ____) |  __/  __/ |  | |__| | |   
                 |_____/ ___|___|_|   ____/|_|   
                
            Swoole Server Memory Usage

Swoole version:4.4.17
 php version:7.2.24
  PHP memory:1.37MB
 
-------------------------------
Swoole worker process mask memory usage
PID       POW   Used(M)    RealUsed(M) Diff(M)
23295     22      3.860      1.426      2.434
23296     22      3.860      1.427      2.433

-------------------------------
Swoole task process mask memory usage
PID      POW   Used(M)    RealUsed(M) Diff(M)
23297    22      3.860      1.926      1.934
23298    22      3.860      1.925      1.935

-------------------------------
Swoole manager process mask memory usage
PID          Used(M)   %  RealUsed(M) % Diff(M)
23294      1.038M    0.06%      1.124M 0.07% 85.76KB

-------------------------------
Total Memory Usage
           Used(M) RealUsed(M)
Total       33MB      13.77MB
Avg(each)   3.7MB     1.53MB
Copy after login

3.Demonster UI界面

除了命令行工具外,Demonster还提供了一个UI界面,方便开发人员在Web端对Swoole服务器进行监控和管理。

UI界面的访问地址为:http://yourhost.com/demonster/index.php(需要根据实际的Web服务器配置进行修改)。打开后可以看到一个简单的页面,其中显示了Swoole服务器的状态和进程信息,同时提供了进程监控和CPU、内存情况分析等功能。

四、小结

Demonster是一款非常实用的Swoole常用工具,它提供了丰富的功能和简单易用的操作界面,方便了开发人员在Swoole开发过程中的排错和监控工作。同时,Demonster是一个开源、高可靠的工具,值得开发人员在Swoole开发中加以尝试和使用。

The above is the detailed content of Introduction and usage of Swoole's common tool Demonster. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template