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

The Integration of Laravel with Swoole

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. None
  4. What is Swoole?

  5. Swoole is… a C extension for Open Source Based on

    Apache2.0 License
  6. Naming of Swoole + =

  7. 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
  8. Laravel is awesome But can it be even faster?

  9. PHP’s Lifecycle Request PHP File PHP File PHP File PHP

    File PHP File Check Parse Compile Execute (Zend Engine)
  10. How many files are required for one request in Laravel?

    218
  11. 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
  12. 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.
  13. Why try to integrate with ?

  14. Swoole Frameworks Swoole Framework Swoole Distributed Easy Swoole Blink FastD

    TSF zhttp MixPHP GroupCo
  15. Swoole’s Model

  16. 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
  17. Integrating Solutions ① Only use Swoole like PHP-FPM.

  18. Integrating Solutions ② Preload and share one Laravel application.

  19. 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.
  20. Integrating Solutions ② Preload and share one Laravel application. Login

    Access Protected Resource Authenticate User A ? ? Protected Resource ? User B
  21. Integrating Solutions

  22. 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.
  23. Integrating Issues ③ Reset necessary variables based on ②.

  24. Integrating Issues From dingo package

  25. 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…
  26. Laravel’s Service Container Illuminate Container $resolved = []; $aliases =

    []; $bindings = []; $instances = []; $serviceProviders = []; $loadedProviders = [];
  27. 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
  28. Sandbox App Container Illuminate Container $resolved = []; $aliases =

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

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

    Request Rebind Router’s Container Reset Service Providers Redirect Container and Facades Dispatch
  31. Integrating Issues Http Kernel App Router Container Routes Collection Route

    Container Route Container
  32. Integrating Issues From pagination service provider

  33. Integrating Issues

  34. Integrating Issues

  35. 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.
  36. Laravel Swoole 940+ Stars on Github

  37. swooletw/laravel-swoole

  38. 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).
  39. Connection Pool Database Worker Worker Worker Worker • Persistent Connections

    • Auto Reconnect
  40. Websocket in Laravel Broadcast Event Pub / Sub Publish Data

    Emit Broadcasted Data
  41. Websocket in Laravel Swoole Emit Data Parser Layer 2probe 42["add

    user","test"]
  42. Websocket Server

  43. Websocket Server

  44. 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
  45. 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
  46. 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
  47. 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
  48. Coroutine

  49. Coroutine hello go1 hello man hello go2

  50. Coroutine Clients in Swoole • TCP / UDP Client •

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

    Connection Connection Connection Connection Connection Connection Connection Pool Worker Database
  52. Connection Pool for Coroutine Database Worker Worker Worker Worker Pool

    Pool Pool Pool • Each worker has its own connection pool.
  53. MySQL Coroutine Swoole Mysql Connector Eloquent ORM Swoole PDO Swoole

    PDO Statement
  54. MySQL Coroutine

  55. 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
  56. 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
  57. Q&A