Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Integration of Laravel with Swoole

Albert Chen
December 20, 2022

The Integration of Laravel with Swoole

LaravelConf Taiwan 2018

Albert Chen

December 20, 2022
Tweet

More Decks by Albert Chen

Other Decks in Programming

Transcript

  1. The Integration of Laravel with Swoole @ L a r

    a v e l C o n f Ta i w a n 2 0 1 8 B y A l b e r t C h e n
  2. About Me { "data": { "human": { "name": "Albert Chen",

    "occupation": "Software Engineer", "company": "Unisharp Techonology", "website": "https://albert-chen.com", "interests": [ "traveling", "programming", "cooking" ], "age": 26 } } }
  3. Features • Written in C, extremely high performance • Event

    driven, non-blocking I/O • Supports TCP / UDP / UnixSock • Supports Async / Sync / Coroutine • Supports IPv4 / IPv6 Network • Multi-process, multi-thread structure • Supports daemon
  4. PHP’s Lifecycle Request PHP File PHP File PHP File PHP

    File PHP File Check Parse Compile Execute (Zend Engine)
  5. Laravel’s Lifecycle Autoload Load App Bootstrap Register Service Providers Boot

    Service Providers Http Kernel Middleware Dispatch by Router Routes Match Controller Response Terminate
 Middleware Request public/ index.php
  6. What Makes Laravel Slow? • A large amount of files

    are required. • Each file needs its parsing and compiling. • Compiled results will be destroyed after the request. • The default session driver of Laravel is file. • Laravel is a full-stack framework. • All the resource can not be reused.
  7. Integrating Solutions ① Only use Swoole like PHP-FPM. ② Preload

    and share single Laravel application. ③ Reset necessary classes/variables based on ②. ④ Build sandbox app for request process based on ③. Running on Package
  8. Integrating Solutions Autoload Load App Bootstrap Register Service Providers Boot

    Service Providers Http Kernel Middleware Dispatch by Router Routes Match Controller Response Terminate
 Middleware Request public/ Laravel will be only booted at the first time. ② Preload and share one Laravel application.
  9. Integrating Solutions ② Preload and share one Laravel application. Login

    Access Protected Resource Authenticate User A ? ? Protected Resource ? User B
  10. Integrating Issues ① Laravel application will be booted only at

    the first time. ② All the singleton classes, global or static properties will be preserved in the memory. ③ Developers need to reset these polluted variables manually.
  11. Integrating Issues ① There are too many unpredictable singleton instances.

    ② Some code will make app become dirty. ③ Some dependency properties are not easy to reset. ④ Damned static variables…
  12. Laravel’s Service Container Illuminate Container $resolved = []; $aliases =

    []; $bindings = []; $instances = []; $serviceProviders = []; $loadedProviders = [];
  13. Laravel’s Facades protected static $app; protected static $resolvedInstance; Facades Service

    Container • $app->singleton(‘event’, …) • $app->singleton(‘db’, …) • $app->singleton(‘auth’, …) Service Provider event db auth
  14. Sandbox App Container Illuminate Container $resolved = []; $aliases =

    []; $bindings = []; $instances = []; $serviceProviders = []; $loadedProviders = []; Sandbox Container Clone
  15. Instances Outside of Sandbox Shared Instances view db session routes

    cache config cookie encrypt hash router url log event
  16. Sandbox App Container Request Sandbox Reset Config Clear Instances Bind

    Request Rebind Router’s Container Reset Service Providers Redirect Container and Facades Dispatch
  17. Laravel’s New Lifecycle Autoload Load App Bootstrap Sandbox
 Reset Reset

    Service Providers Http Kernel Middleware Dispatch by Router Routes Match Controller Response Terminate
 Middleware Request public/ index.php • Save I/O for requiring files. • Make app container isolated.
  18. Laravel Swoole • Run Laravel/Lumen application on top of Swoole.

    • Outstanding performance boosting up to 5x faster. • Sandbox mode to isolate app container. • Support running websocket server in Laravel. • Support Socket.io protocol. • Support Swoole table for cross-process data sharing. • Support Coroutine (in develop).
  19. Benchmark • Provider: AWS EC2 • Instance Type: General t2.2xlarge

    • Hardware Specs: 8 CPUs / 32 G Memory • OS: Ubuntu 16.04.4 x64 • PHP Version: 7.2 • Laravel Version: 5.6.3 • Benchmark Tool: wrk
  20. Benchmark wrk -t4 -c10 http://laravel-swoole.tw Running 10s test @ http://laravel-swoole.tw

    4 threads and 10 connections Thread Stats Avg Stdev Max +/- Stdev Latency 5.49ms 5.62ms 110.36ms 97.98% Req/Sec 396.43 71.29 444.00 86.87% 15671 requests in 10.01s, 38.05MB read Requests/sec: 1565.63 Transfer/sec: 3.80MB
  21. Benchmark wrk -t4 -c10 http://laravel-swoole.tw:1215 Running 10s test @ http://laravel-swoole.tw:1215

    4 threads and 10 connections Thread Stats Avg Stdev Max +/- Stdev Latency 0.87ms 1.59ms 40.76ms 98.57% Req/Sec 2.71k 140.31 2.89k 88.09% 108606 requests in 10.10s, 261.53MB read Requests/sec: 10753.42 Transfer/sec: 25.89MB
  22. Coroutine Swoole Swoole 2.0 Swoole 4.0 • async clients •

    PHP yield + generator • coroutine clients • setjmp/longjmp • limited use cases • call_user_func • array_map • _destruct • coroutine clients • boost.context 1.60 • support all use cases
  23. Coroutine Clients in Swoole • TCP / UDP Client •

    Http Client • Http2 Client • Redis Client • MySQL Client • PostgreSQL Client
  24. Connection Pool for Coroutine • Each pool maintains several connections.

    Connection Connection Connection Connection Connection Connection Connection Pool Worker Database
  25. Connection Pool for Coroutine Database Worker Worker Worker Worker Pool

    Pool Pool Pool • Each worker has its own connection pool.
  26. Benchmark wrk -t4 -c10 http://laravel-swoole.tw:1215/sync Running 10s test @ http://laravel-swoole.tw:1215/sync

    4 threads and 10 connections Thread Stats Avg Stdev Max +/- Stdev Latency 1.08s 0.00us 1.08s 100.00% Req/Sec 0.11 0.33 1.00 88.89% 9 requests in 10.10s, 8.14KB read Socket errors: connect 0, read 0, write 0, timeout 8 Requests/sec: 0.89 Transfer/sec: 825.20B
  27. Benchmark wrk -t4 -c10 http://laravel-swoole.tw:1215/coro Running 10s test @ http://laravel-swoole.tw:1215/coro

    4 threads and 10 connections Thread Stats Avg Stdev Max +/- Stdev Latency 1.03s 7.91ms 1.05s 72.22% Req/Sec 2.17 2.84 9.00 85.71% 72 requests in 10.10s, 65.00KB read Requests/sec: 7.13 Transfer/sec: 6.43KB
  28. Q&A