Replies: 7 comments 10 replies
-
Oh I'm super on board with this, I just think that this will take more massaging to get to that point. I've done this for Fat-Free in https://github.com/n0nag0n/fatfree-swoole. My goal is to get flight to that point, but it's not the goal right now. |
Beta Was this translation helpful? Give feedback.
-
If you want, you could fork core and make changes to see if you can get swoole running properly and then based on what you learn I could make the changes in core to handle a swoole/roadrunner/workerman/reactphp implementation. You could base it off of my fat-free swoole project, and I based that a lot off of https://github.com/pachico/slim-swoole which is now archived. It would just help save me a step of figuring out what needs to be unset/reset/rebuilt to work with swoole or some other framework. |
Beta Was this translation helpful? Give feedback.
-
Hello, Unless I am mistaken, there is a new library for such needs which do not require (much) code change called FrankenPHP. |
Beta Was this translation helpful? Give feedback.
-
I was not able to run it (when trying it for first time) through custom 8000 port. as Caddyfile was somehow getting assigned to port 80 and running with sudo permission I was not very sure. Can you share any working code for running FrakenPHP with FlightPHP. It would be great. |
Beta Was this translation helpful? Give feedback.
-
Hello, As I tested, there is no need to create a pool of connections as the database connection could be instantiated in the constructor of the Kernel class above, and then it can be used later on. FrankenPHP might be a solution to the MySQL's problem I tried to do some benchmarks (see code below), and I got impressive results as early I request a bit on the API (just continuous clicking on Refresh in browser). In standard mode, For 1 route definition + Another good news is that FlightPHP can catch $_POST, $_GET, etc.. in worker mode without being injected. However, I found one problem: I tried to use vanilla PHP My code: <?php
// index.php
use Symfony\Component\HttpFoundation\JsonResponse;
require 'vendor/autoload.php';
class Kernel
{
private string $createdAt;
private PDO $db;
public function __construct()
{
$this->createdAt = date('Y-m-d H:i:s');
$this->db = new PDO(
'mysql:host=host.docker.internal;port=3306;dbname=XXX;charset=utf8mb4',
'XXX',
'XXX'
);
}
public function handle(string $mode): void
{
$start_time = microtime(true);
Flight::route('/', function () {});
Flight::start();
$query = $this->db->prepare("
SELECT name
FROM item
LIMIT 1
");
$query->execute();
$item = $query->fetch();
$end_time = microtime(true);
// Calculate the execution time
$execution_time = $end_time - $start_time;
$execution_time_formatted = number_format($execution_time, 5);
$r = Flight::request();
(new JsonResponse([
'mode' => $mode,
'dbCreatedAt' => $this->createdAt,
'now' => date('Y-m-d H:i:s'),
'executionTime' => $execution_time_formatted,
'flightQuery' => $r->query->getData(),
'flightData' => $r->data->getData(),
]))->send();
}
}
$myApp = new \Kernel();
// Uncomment below for standard mode
// echo $myApp->handle('standard');
// Comment below for standard mode
$handler = static function () use ($myApp) {
echo $myApp->handle('worker');
};
for (
$nbRequests = 0, $running = true;
$nbRequests < 5 && $running;
++$nbRequests
) {
$running = \frankenphp_handle_request($handler);
gc_collect_cycles();
} |
Beta Was this translation helpful? Give feedback.
-
So I got this to work on Swoole, at least a crude example. It was the weird type of behavior I was expecting, and I needed a few hacks to get it to work properly (as well as some changes to the core to make it work) Things I needed to change:
$app->route('/', function() use ($app) {
$app->response()->header('Content-Type', 'application/json');
echo json_encode([
'hello' => 'world'
]);
}); And this was the gist of the $server_vars = $request->server;
$server_vars = array_change_key_case($server_vars, CASE_UPPER);
$_SERVER = $server_vars;
$_GET = $request->get ?? [];
$_POST = $request->post ?? [];
$_COOKIE = $request->cookie ?? [];
$_FILES = $request->files ?? [];
$_REQUEST = array_merge($_GET, $_POST, $_COOKIE);
$Engine->start();
foreach($Engine->response()->headers() as $header => $value) {
$response->header($header, $value);
}
$response->status($Engine->response()->status());
if($Engine->response()->getBody()) {
$response->write($Engine->response()->getBody());
}
$Engine->unregister('response');
$Engine->unregister('request');
$Engine->register('response', 'flight\net\Response');
$Engine->register('request', 'flight\net\Request');
$response->end(); Once I finally got it working, it was a fun performance boost :) With PHP Dev-Server:
With Swoole Server:
It was roughly 3x faster on Swoole! Pretty cool :) |
Beta Was this translation helpful? Give feedback.
-
I think that we are going in the wrong direction !! |
Beta Was this translation helpful? Give feedback.
-
Roadrunner is a high-performance PHP application server, load-balancer, and process manager written in Golang.
Integration existing FlightPHP application with Roadrunner would provide faster throughput.
https://roadrunner.dev/
https://openswoole.com/
Beta Was this translation helpful? Give feedback.
All reactions