PHP R.I.P.
So get ready to get your hands dirty with a vps. coz im going to show you how to install and setup a server with Swoole serving static files, serving websockets on the same port and have it ready for restful operations…in just 38 lines
I hear you asking..
WTF is Swoole? well.. Swoole is an high-performance network framework using an event-driven, asynchronous, non-blocking I/O model which makes it scalable and efficient. It is written in C language without 3rd party libraries as PHP extension.
That it makes PHP look like node, (No apache in the middle) actually better than nodejs , coz it also provides you with super fast memory management, sql hardcoded API, redis API, and the list goes on, but lets see a benchmark since we are going to use on this course ^php as nodejs – express etc^ but with the maturity and the power of php.
Node.js v4.2.6
wrk -t4 -c400 -d10s http://127.0.0.1:1337/ Running 10s test @ http://127.0.0.1:1337/ 4 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 16.08ms 2.70ms 237.82ms 90.94% Req/Sec 6.05k 510.34 7.10k 91.00% 240668 requests in 10.02s, 46.36MB read Requests/sec: 24026.00 Transfer/sec: 4.63MB
PHP7.1+Swoole-v1.9.5
wrk -t4 -c400 -d10s http://127.0.0.1:1337/ Running 10s test @ http://127.0.0.1:1337/ 4 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 1.46ms 1.42ms 27.96ms 85.66% Req/Sec 75.29k 36.43k 183.55k 72.75% 3007806 requests in 10.06s, 605.25MB read Requests/sec: 299103.32 Transfer/sec: 60.19MB
Source: https://www.w3c-lab.com/php-7-1-swoole-v1-9-5-vs-node-js-benchmark-test-php7-swoole-beats-node-js/
EDIT:
I had second thoughts about trusting a benchmark done from somebody else on the internet, so i decided to run my own tests.
It’s expected that swoole with the build-in C MySQL-redis is fast as these are written in C language and are compiled, but lately the trend is about Mongodb and mongo is what i like to use for new projects out of laziness for the lack of a schema.. so what about mongo? i wrote both on swoole and node express a db simple read with collection.findOne()
Javascript code:
await db.collection(“users”)
.findOne(
{ “email”: ‘test@testing.com’ },
{ ‘projection’: { ‘password’: 1 } }
).then((result) => {
res.send(JSON.stringify(result));
})
.catch(err => {
});
PHP Code:
$document = $c->findOne(
[’email’ => ‘test@testing.com’],
[
‘projection’ => [
‘password’ => 1,
],
‘limit’ => 1,
‘_id’ => 0,
] );
response->end($document);
I run a multiple tests and the results didn’t vary in any significant way. lol j/k see bellow
My benchmarking
Finished 50000 requests
Server Software:
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /api/benchmark/
Document Length: 112 bytes
Concurrency Level: 500
Time taken for tests: 74.847 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 28050000 bytes
HTML transferred: 5600000 bytes
Requests per second: 668.03 [#/sec] (mean)
Time per request: 748.466 [ms] (mean)
Time per request: 1.497 [ms] (mean, across all concurrent requests)
Transfer rate: 365.98 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 106 788.3 5 15465
Processing: 218 641 156.3 635 1784
Waiting: 110 460 122.8 457 1607
Total: 221 747 803.1 646 16408
Percentage of the requests served within a certain time (ms)
50% 646
66% 722
75% 753
80% 773
90% 845
95% 1466
98% 1846
99% 2805
100% 16408 (longest request)
Finished 50000 requests
Server Software: swoole-http-server
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /api/benchmark/
Document Length: 121 bytes
Concurrency Level: 500
Time taken for tests: 30.930 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 15100000 bytes
HTML transferred: 6050000 bytes
Requests per second: 1616.57 [#/sec] (mean)
Time per request: 309.296 [ms] (mean)
Time per request: 0.619 [ms] (mean, across all concurrent requests)
Transfer rate: 476.76 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 5.4 0 58
Processing: 42 306 31.9 303 453
Waiting: 36 306 31.9 303 453
Total: 94 308 29.6 304 453
Percentage of the requests served within a certain time (ms)
50% 304
66% 312
75% 318
80% 324
90% 343
95% 364
98% 386
99% 401
100% 453 (longest request)