Opinionated amalgamation of libraries for creating blazing fast, reliable and modern REST API's with PHP using RoadRunner

0.0.1-rc1 2022-06-19 03:56 UTC

This package is auto-updated.

Last update: 2024-04-20 02:38:52 UTC


README

Static Analysis PHP Version Coding Style License: WTFPL

About

Spdy PHP is an opinionated amalgamation of libraries for creating blazing fast, reliable and modern REST API's with PHP using RoadRunner.

By using sane industry standard defaults, Spdy lets you focus on your application business value, instead of wasting cycles choosing and re-choosing various technologies and configurations.

On the most high level definition, Spdy is just implementing battle tested, industry standard libraries for PHP and bundling it together to form a fast and reliable skeleton for your REST API's.

Getting Stared

  1. Install Spdy
composer require nickstarlight/spdy
  1. Implement your application basic structure:
<?php declare(strict_types=1);

include_once './vendor/autoload.php';

/** Create a new app instance */
$app = new Nickstarlight\Spdy\App();

/** Get the default router instance */
$router = $app->getRouter();

/** Declare a route */
$router->get('/', fn () => [ 'message' => 'Hello World' ]);

/** Kickstart the event-loop */
$app->run();
  1. Get the RoadRunner binary:
./vendor/bin/rr get-binary
  1. Implement a basic Roadrunner PHP Worker configuration, create a new file named .rr.yaml with the following contents:
version: "2.7"

server:
  command: "php yourscript.php"

http:
  address: 0.0.0.0:8080
  1. Start your application using the rr binary:
./rr serve

And you're good to go! Hit localhost:8080 and check your new route!

Documentation reference

As mentioned, Spdy does not try to reinvent the wheel, all libraries used are well known libraries, if you'd like to tweak anything or change the provided defaults, the bellow documentation is all you need:

Module Functions What is used for? Documentation
PHP League Router 5.x Router Route declaration, HTTP error handling, JSON encoding, Route resolution and response strategy https://route.thephpleague.com/5.x/
Nyholm PSR-7 1.5.x Response and Request factories Parsing incomming requests and generating outgoing responses https://github.com/Nyholm/psr7
Roadrunner 2.x PHP Event Loop, HTTP Server, Load balancer and process manager Creates blazing fast PHP Workers, acts as a replacement for Nginx and Apache and controls the lifecycle of workers https://roadrunner.dev/docs

FAQS

  1. How do I declare routes?

    Check the League Route documentation for examples.

  2. How do I configure the worker to fit my server needs? How do I setup healthchecks? Database connections? Environment variables?

    Roadrunner provides production-ready out-of-the-box configurations for those questions and many other features, check the Roadrunner configuration file reference.

  3. I would like a robust ORM, broadcasting, views, integration with ReactJS/Vue/Svelte, etc...

    Spdy main goal is to provide a reliable, small and effient base for creating simple yet powerful REST API's, it does not attempt to replace complete frameworks. If you need more features, you should defintely aim for fully-fledged frameworks! Some good examples are Symfony, Laravel, Yii, Laminas and Phalcon.

Benchmarks

Check the benchmarks standalone repository for more information.

PHP 8.1 - Spdy C++ 17 - uWebsockets Go 1.18 - Gin Python 3.9 - FastAPI NodeJS 16 - Fastify
Time taken for tests 57.129 seconds 9.208 seconds 14.929 seconds 90.420 seconds 18.484 seconds
Complete requests 100000 100000 100000 100000 100000
Failed requests 0 0 0 0 0
Requests per second (mean) 1750.41 10859.95 6698.22 1105.94 5410.07
Time per request (mean) 57.129 [ms] 9.208 [ms] 14.929 [ms] 90.420 [ms] 18.484 [ms]
Transfer rate 241.02 [Kbytes/sec] 901.46 [Kbytes/sec] 974.64 [Kbytes/sec] 183.60 [Kbytes/sec] 887.59 [Kbytes/sec]
Percentage of requests served in miliseconds 50% 57
66% 58
75% 59
80% 60
90% 61
95% 63
98% 65
99% 66
100% 82 (longest request)
50% 9
66% 10
75% 11
80% 12
90% 13
95% 14
98% 16
99% 18
100% 77 (longest request)
50% 14
66% 16
75% 18
80% 19
90% 22
95% 25
98% 28
99% 30
100% 68 (longest request)
50% 88
66% 91
75% 93
80% 95
90% 101
95% 105
98% 110
99% 113
100% 162 (longest request)
50% 19
66% 20
75% 20
80% 21
90% 23
95% 26
98% 30
99% 33
100% 55 (longest request)

License

This work is licensed under the WTFPL license.