dvnc0/chaos-gremlin

PHP的混沌代理

v0.2.0 2024-03-24 04:20 UTC

This package is auto-updated.

Last update: 2024-09-04 05:50:57 UTC


README

   )\.-.       .'(     /`-.      .-./(    )\.--.          )\.-.      /`-.   )\.---.   )\   )\   .')      .'(   )\  )\  
 ,' ,-,_)  ,') \  )  ,' _  \   ,'     )  (   ._.'       ,' ,-,_)   ,' _  \ (   ,-._( (  ',/ /  ( /       \  ) (  \, /  
(  .   _  (  '-' (  (  '-' (  (  .-, (    `-.`.        (  .   __  (  '-' (  \  '-,    )    (    ))       ) (   ) \ (   
 ) '..' )  ) .-.  )  )   _  )  ) '._\ )  ,_ (  \        ) '._\ _)  ) ,_ .'   ) ,-`   (  \(\ \   )'._.-.  \  ) ( ( \ \  
(  ,   (  (  ,  ) \ (  ,' ) \ (  ,   (  (  '.)  )      (  ,   (   (  ' ) \  (  ``-.   `.) /  ) (       )  ) \  `.)/  ) 
 )/'._.'   )/    )/  )/    )/  )/ ._.'   '._,_.'        )/'._.'    )/   )/   )..-.(       '.(   )/,__.'    )/     '.(  
                                                                                                                       

Chaos Gremlin

Chaos Gremlin 是一个PHP混沌测试工具,它将随机问题引入您的应用程序中,以模拟延迟、CPU使用、内存使用、高磁盘使用等。此工具用于测试您的应用程序从未知事件中恢复的能力。

谨慎使用 些Gremlins 将不会停止,直到您手动终止进程。只启用您想要使用的Gremlins 并审查设置。

此工具旨在测试您自己的应用程序或您有权限测试的应用程序

使用以下命令安装: composer require dvnc0/chaos-gremlin

要求

  • PHP pcntl 扩展必须安装。
  • 日志目录必须可由PHP进程写入。
  • 磁盘Gremlin目录必须可由PHP进程写入。

默认设置

protected array $settings = [
	'probability' => 30,
	'min_latency_seconds' => 2,
	'max_latency_seconds' => 10,
	'exception_message' => 'Chaos Gremlin Exception',
	'dice_roll_over_under' => 3.5,
	'max_memory_percent' => 90,
	'disk_gremlin_directory' => '',
	'disk_gremlin_number_files' => 100,
	'disk_gremlin_file_size' => 5 * 1024 * 1024,
	'traffic_requests' => 100,
	'traffic_url' => 'https://:8080',
	'log_directory' => '',
	'traffic_gremlin_spawns_gremlins' => false,
];

这些是Chaos Gremlin的默认设置,可以通过使用settings方法进行更改。我们将在本README的后面部分介绍这一点。

使用Chaos Gremlin

要使用Chaos Gremlin,您需要创建Gremlin类的实例,启用您想要使用的任何Gremlins,然后调用release方法。这可能会将Gremlins释放到您的应用程序中。

<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';

use ChaosGremlin\Chaos_Gremlin;

$Gremlin = Chaos_Gremlin::getInstance();
$Gremlin->enableGremlin('Memory_Gremlin');

$Gremlin->release();

上述代码将创建Gremlin类的实例,启用Memory_Gremlin,然后将Gremlins释放到应用程序中。Gremlin将使用PHP可用的90%的内存或系统内存的90%。这是使用上述默认设置。

可用的Gremlins

Chaos Gremlin中有许多Gremlins可供使用。您可以通过使用enableGremlin方法启用以下任何Gremlins。

流量Gremlin

流量Gremlin会向设置数组中设置的URL发起请求。这可以用来测试您的应用程序如何处理大量请求。默认情况下,来自流量Gremlins的请求不会孵化其他Gremlins,这可以通过将traffic_gremlin_spawns_gremlins设置设置为true来更改。再次提醒,对此设置要小心,因为它可以根据您的设置孵化大量Gremlins,包括更多的流量Gremlins,这可能会创建更多的Gremlins等,等等。

添加自定义设置

<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';

use ChaosGremlin\Chaos_Gremlin;

$Gremlin = Chaos_Gremlin::getInstance();
$Gremlin->settings([
	'probability' => 75,
	'min_latency_seconds' => 5,
	'max_latency_seconds' => 10,
	'max_memory_percent' => 90,
	'disk_gremlin_number_files' => 1000,
	'disk_gremlin_file_size' => 5 * 1024 * 1024,
	'traffic_requests' => 500,
	'traffic_url' => 'https://:9000',
]);
$Gremlin->enableGremlin('Memory_Gremlin');
$Gremlin->enableGremlin('Disk_Gremlin');
$Gremlin->enableGremlin('Traffic_Gremlin');
$Gremlin->enableGremlin('Cpu_Gremlin');
$Gremlin->enableGremlin('Latency_Gremlin');

$Gremlin->release();

自定义Gremlins

您可以通过扩展Gremlin类并实现attack方法来创建自己的Gremlins。

abstract public function attack(): void;

设置数组将传递到自定义Gremlin,以下方法可供Gremlin使用。

要启用自定义Gremlin,请使用addGremlin方法,该方法接受一个string键和一个自定义Gremlin的实例。

<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';

use ChaosGremlin\Chaos_Gremlin;
use Custom_Database_Gremlin;

$Gremlin = Chaos_Gremlin::getInstance();
$Gremlin->settings([
	'probability' => 75,
	'min_latency_seconds' => 5,
	'max_latency_seconds' => 10,
	'max_memory_percent' => 90,
	'disk_gremlin_number_files' => 1000,
	'disk_gremlin_file_size' => 5 * 1024 * 1024,
	'traffic_requests' => 500,
	'traffic_url' => 'https://:9000',
]);
$Gremlin->enableGremlin('Memory_Gremlin');
$Gremlin->enableGremlin('Disk_Gremlin');

$Gremlin->addGremlin('Database_Gremlin', new Custom_Database_Gremlin());

$Gremlin->release();

要释放自定义Gremlin,您需要调用使用启用Gremlin时设置的键的summonGremlin方法。这允许您决定何时尝试释放Gremlin,让您可以将Gremlins添加到应用程序的特定部分。

<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';

use ChaosGremlin\Chaos_Gremlin;

$Gremlin = Chaos_Gremlin::getInstance();

$Gremlin->summonGremlin('Database_Gremlin');