More Related Content

Similar to The Integration of Laravel with Swoole(20)

The Integration of Laravel with Swoole

  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. What is Swoole?
  4. Swoole is… a C extension for Open Source Based on Apache2.0 License
  5. Naming of Swoole + =
  6. 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
  7. Laravel is awesome But can it be even faster?
  8. PHP’s Lifecycle Request PHP File PHP File PHP File PHP File PHP File Check Parse Compile Execute (Zend Engine)
  9. How many files are required for one request in Laravel? 218
  10. Laravel’s Lifecycle Autoload Load App Bootstrap Register Service Providers Boot Service Providers Http Kernel Middleware Dispatch by Router Routes Match ControllerResponse Terminate
 Middleware Request public/ index.php
  11. 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.
  12. Why try to integrate with ?
  13. Swoole Frameworks Swoole Framework Swoole Distributed Easy Swoole Blink FastD TSF zhttp MixPHP GroupCo
  14. Swoole’s Model
  15. 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
  16. Integrating Solutions ① Only use Swoole like PHP-FPM.
  17. Integrating Solutions ② Preload and share one Laravel application.
  18. Integrating Solutions Autoload Load App Bootstrap Register Service Providers Boot Service Providers Http Kernel Middleware Dispatch by Router Routes Match ControllerResponse Terminate
 Middleware Request public/ Laravel will be only booted at the first time. ② Preload and share one Laravel application.
  19. Integrating Solutions ② Preload and share one Laravel application. Login Access Protected Resource Authenticate User A ? ? Protected Resource ? User B
  20. Integrating Solutions
  21. 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.
  22. Integrating Issues ③ Reset necessary variables based on ②.
  23. Integrating Issues From dingo package
  24. 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…
  25. Laravel’s Service Container Illuminate Container $resolved = []; $aliases = []; $bindings = []; $instances = []; $serviceProviders = []; $loadedProviders = [];
  26. 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
  27. Sandbox App Container Illuminate Container $resolved = []; $aliases = []; $bindings = []; $instances = []; $serviceProviders = []; $loadedProviders = []; Sandbox ContainerClone
  28. Instances Outside of Sandbox Shared Instances view db session routes cache config cookie encrypt hash router url log event
  29. Sandbox App Container Request Sandbox Reset Config Clear Instances Bind Request Rebind Router’s Container Reset Service Providers Redirect Container and Facades Dispatch
  30. Integrating Issues Http Kernel App Router Container Routes Collection Route Container Route Container
  31. Integrating Issues From pagination service provider
  32. Integrating Issues
  33. Integrating Issues
  34. Laravel’s New Lifecycle Autoload Load App Bootstrap Sandbox
 Reset Reset Service Providers Http Kernel Middleware Dispatch by Router Routes Match ControllerResponse Terminate
 Middleware Request public/ index.php • Save I/O for requiring files. • Make app container isolated.
  35. Laravel Swoole 940+ Stars on Github
  36. swooletw/laravel-swoole
  37. 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).
  38. Connection Pool Database Worker Worker Worker Worker • Persistent Connections • Auto Reconnect
  39. Websocket in Laravel Broadcast Event Pub / Sub Publish Data Emit Broadcasted Data
  40. Websocket in Laravel Swoole Emit Data Parser Layer 2probe 42["add user","test"]
  41. Websocket Server
  42. Websocket Server
  43. 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
  44. 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
  45. 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
  46. 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
  47. Coroutine
  48. Coroutine hello go1 hello man hello go2
  49. Coroutine Clients in Swoole • TCP / UDP Client • Http Client • Http2 Client • Redis Client • MySQL Client • PostgreSQL Client
  50. Connection Pool for Coroutine • Each pool maintains several connections. Connection Connection Connection Connection Connection Connection Connection Pool Worker Database
  51. Connection Pool for Coroutine Database Worker Worker Worker Worker Pool Pool Pool Pool • Each worker has its own connection pool.
  52. MySQL Coroutine Swoole Mysql Connector Eloquent ORM Swoole PDO Swoole PDO Statement
  53. MySQL Coroutine
  54. 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
  55. 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
  56. Q&A