Home PHP Framework Swoole Introduction and usage of Swoole's common tool Demonster

Introduction and usage of Swoole's common tool Demonster

Jun 13, 2023 pm 02:56 PM
Tool usage demonster swoole

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use swoole coroutine in laravel How to use swoole coroutine in laravel Apr 09, 2024 pm 06:48 PM

Using Swoole coroutines in Laravel can process a large number of requests concurrently. The advantages include: Concurrent processing: allows multiple requests to be processed at the same time. High performance: Based on the Linux epoll event mechanism, it processes requests efficiently. Low resource consumption: requires fewer server resources. Easy to integrate: Seamless integration with Laravel framework, simple to use.

How to use Swoole to implement a high-performance HTTP reverse proxy server How to use Swoole to implement a high-performance HTTP reverse proxy server Nov 07, 2023 am 08:18 AM

How to use Swoole to implement a high-performance HTTP reverse proxy server Swoole is a high-performance, asynchronous, and concurrent network communication framework based on the PHP language. It provides a series of network functions and can be used to implement HTTP servers, WebSocket servers, etc. In this article, we will introduce how to use Swoole to implement a high-performance HTTP reverse proxy server and provide specific code examples. Environment configuration First, we need to install the Swoole extension on the server

How to restart the service in swoole framework How to restart the service in swoole framework Apr 09, 2024 pm 06:15 PM

To restart the Swoole service, follow these steps: Check the service status and get the PID. Use "kill -15 PID" to stop the service. Restart the service using the same command that was used to start the service.

How does swoole_process allow users to switch? How does swoole_process allow users to switch? Apr 09, 2024 pm 06:21 PM

Swoole Process allows users to switch. The specific steps are: create a process; set the process user; start the process.

Which one is better, swoole or workerman? Which one is better, swoole or workerman? Apr 09, 2024 pm 07:00 PM

Swoole and Workerman are both high-performance PHP server frameworks. Known for its asynchronous processing, excellent performance, and scalability, Swoole is suitable for projects that need to handle a large number of concurrent requests and high throughput. Workerman offers the flexibility of both asynchronous and synchronous modes, with an intuitive API that is better suited for ease of use and projects that handle lower concurrency volumes.

Which one has better performance, swoole or java? Which one has better performance, swoole or java? Apr 09, 2024 pm 07:03 PM

Performance comparison: Throughput: Swoole has higher throughput thanks to its coroutine mechanism. Latency: Swoole's coroutine context switching has lower overhead and smaller latency. Memory consumption: Swoole's coroutines occupy less memory. Ease of use: Swoole provides an easier-to-use concurrent programming API.

Swoole in action: How to use coroutines for concurrent task processing Swoole in action: How to use coroutines for concurrent task processing Nov 07, 2023 pm 02:55 PM

Swoole in action: How to use coroutines for concurrent task processing Introduction In daily development, we often encounter situations where we need to handle multiple tasks at the same time. The traditional processing method is to use multi-threads or multi-processes to achieve concurrent processing, but this method has certain problems in performance and resource consumption. As a scripting language, PHP usually cannot directly use multi-threading or multi-process methods to handle tasks. However, with the help of the Swoole coroutine library, we can use coroutines to achieve high-performance concurrent task processing. This article will introduce

How is the swoole coroutine scheduled? How is the swoole coroutine scheduled? Apr 09, 2024 pm 07:06 PM

Swoole coroutine is a lightweight concurrency library that allows developers to write concurrent programs. The Swoole coroutine scheduling mechanism is based on the coroutine mode and event loop, using the coroutine stack to manage coroutine execution, and suspend them after the coroutine gives up control. The event loop handles IO and timer events. When the coroutine gives up control, it is suspended and returns to the event loop. When an event occurs, Swoole switches from the event loop to the pending coroutine, completing the switch by saving and loading the coroutine state. Coroutine scheduling uses a priority mechanism and supports suspend, sleep, and resume operations to flexibly control coroutine execution.

See all articles