Skip to content

Instantly share code, notes, and snippets.

@nkt
Last active September 27, 2023 08:24
Show Gist options
  • Save nkt/e49289321c744155484c to your computer and use it in GitHub Desktop.
Save nkt/e49289321c744155484c to your computer and use it in GitHub Desktop.
ReactPHP vs Node.js
var http = require('http');
var data = {
'code': 'ok',
'error': false,
'payload': 'Hello World'
};
var app = function (req, res) {
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify(data));
};
var server = http.createServer(app);
server.listen(1337, function() {
console.log("Server running at http://127.0.0.1:1337");
});
<?php
require 'vendor/autoload.php';
$data = [
'code' => 'ok',
'error' => false,
'payload' => 'Hello World'
];
$app = function ($request, $response) use($data) {
$response->writeHead(200, array('Content-Type' => 'application/json'));
$response->end(json_encode($data));
};
$loop = new React\EventLoop\LibEventLoop();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);
$http->on('request', $app);
echo "Server running at http://127.0.0.1:1337\n";
$socket->listen(1337);
$loop->run();

wrk -t4 -c400 -d10s http://127.0.0.1:1337/

PHP

Running 10s test @ http://127.0.0.1:1337/
  4 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     7.02ms    6.94ms  82.86ms   85.27%
    Req/Sec   827.17    552.85     2.38k    66.54%
  22543 requests in 10.07s, 3.61MB read
  Socket errors: connect 151, read 22680, write 86, timeout 0
Requests/sec:   2238.19
Transfer/sec:    367.20KB

Node

Running 10s test @ http://127.0.0.1:1337/
  4 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    20.34ms    4.50ms 125.04ms   97.89%
    Req/Sec     3.09k     1.93k    6.06k    55.00%
  122838 requests in 10.02s, 23.66MB read
  Socket errors: connect 151, read 69, write 73, timeout 0
Requests/sec:  12263.37
Transfer/sec:      2.36MB
@sinasalek
Copy link

@ifubar They both used the same number of CPU cores. Also note that raw swoole performance is in pair with nginx!! i couldn't believe it but i ran it several times and the result was consistent! Also note that in real world applications the difference is much less but nonetheless it proves that php can now be used even as a static file server!

@bawasaab
Copy link

@arunnabraham
Copy link

checkout the below one

https://medium.com/@mtrdesign/reactphp-6c65735138a0

https://reactphp.org/

React is good but no where near fast as Swoole

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment