DEV Community

Cover image for PHP HyperF -> Overhead + Circuit Breaker
Thiago Silva
Thiago Silva

Posted on • Edited on

PHP HyperF -> Overhead + Circuit Breaker

HyperF - Project

This test execute calc of Fibonacci to show how HyperF behaves with overload, after use the maximum CPU and requests, the next requests do not respond, the system get into a circuit breaker.

Create - Project

composer create-project hyperf/hyperf-skeleton "project"
Enter fullscreen mode Exit fullscreen mode

Install - Watcher

composer require hyperf/watcher --dev
Enter fullscreen mode Exit fullscreen mode

Server - Start

cd project ;
php bin/hyperf.php server:watch ;
Enter fullscreen mode Exit fullscreen mode

HyperF - PHP

Set a time limit of one minute just for testing.

set_time_limit(60);
Enter fullscreen mode Exit fullscreen mode

Path: project/bin/hyperf.php

HyperF - APP

APP - Config - Server

Limit to one process and one request at a time.

return [
    'settings' => [
        Constant::OPTION_WORKER_NUM => 1,
        Constant::OPTION_MAX_REQUEST => 1,
    ],
];
Enter fullscreen mode Exit fullscreen mode

Path: project/config/autoload/server.php

APP - Router

Router::addRoute(['GET', 'POST'], '/stress', 'App\Controller\ControllerOverhead@stress');
Router::addRoute(['GET', 'POST'], '/data', 'App\Controller\ControllerOverhead@data');
Enter fullscreen mode Exit fullscreen mode

Path: project/config/routes.php

APP - Controller - Overhead

namespace App\Controller;

class ControllerOverhead
{
    public function fibonacci($number)
    {
        if($number==0)
            return 0;
        elseif($number==1)
            return 1;
        else
            return ($this->fibonacci($number-1)+$this->fibonacci($number-2));
    }
    public function stress()
    {
        $time=microtime(true);
        $fibonacci=$this->fibonacci(40);
        $data=[
            'time'=>microtime(true)-$time,
            'fibonacci'=>$fibonacci,
        ];
        return $data;
    }
    public function data()
    {
        $time=microtime(true);
        $content='data';
        $data=[
            'time'=>microtime(true)-$time,
            'content'=>$content,
        ];
        return $data;
    }
}
Enter fullscreen mode Exit fullscreen mode

Path: project/app/Controller/ControllerOverhead.php

Execute

GET - Data

curl "http://127.0.0.1:9501/data"
Response OK
Enter fullscreen mode Exit fullscreen mode

GET - Stress

curl "http://127.0.0.1:9501/stress"
Wait... Response OK
Enter fullscreen mode Exit fullscreen mode

GET - Overload

curl "http://127.0.0.1:9501/stress" && curl "http://127.0.0.1:9501/data"
Wait... Error -> Response only first request
Enter fullscreen mode Exit fullscreen mode

Conclusion

The greater the processing power and network of your server, the greater the HyperF configuration can be.
In HyperF is default settings, the number of workers is equal the number of CPUs, and 100k requests maximum.


https://github.com/thiagoeti/php-hyperf-overhead-circuit-breaker

Top comments (0)