$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

  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
    }
    }
    }

    View Slide

  3. View Slide

  4. What is Swoole?

    View Slide

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

    View Slide

  6. Naming of Swoole
    + =

    View Slide

  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

    View Slide

  8. Laravel is awesome
    But can it be even faster?

    View Slide

  9. PHP’s Lifecycle
    Request
    PHP File
    PHP File
    PHP File
    PHP File
    PHP File
    Check
    Parse
    Compile
    Execute
    (Zend Engine)

    View Slide

  10. How many files are required
    for one request in Laravel?
    218

    View Slide

  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

    View Slide

  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.

    View Slide

  13. Why try to integrate
    with ?

    View Slide

  14. Swoole Frameworks
    Swoole Framework
    Swoole Distributed
    Easy Swoole
    Blink
    FastD
    TSF
    zhttp
    MixPHP
    GroupCo

    View Slide

  15. Swoole’s Model

    View Slide

  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

    View Slide

  17. Integrating Solutions
    ① Only use Swoole like PHP-FPM.

    View Slide

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

    View Slide

  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.

    View Slide

  20. Integrating Solutions
    ② Preload and share one Laravel application.
    Login Access
    Protected
    Resource
    Authenticate
    User A
    ? ?
    Protected
    Resource
    ?
    User B

    View Slide

  21. Integrating Solutions

    View Slide

  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.

    View Slide

  23. Integrating Issues
    ③ Reset necessary variables based on ②.

    View Slide

  24. Integrating Issues
    From dingo package

    View Slide

  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…

    View Slide

  26. Laravel’s Service Container
    Illuminate Container
    $resolved = [];
    $aliases = [];
    $bindings = [];
    $instances = [];
    $serviceProviders = [];
    $loadedProviders = [];

    View Slide

  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

    View Slide

  28. Sandbox App Container
    Illuminate
    Container
    $resolved = [];
    $aliases = [];
    $bindings = [];
    $instances = [];
    $serviceProviders = [];
    $loadedProviders = [];
    Sandbox
    Container
    Clone

    View Slide

  29. Instances Outside of Sandbox
    Shared Instances
    view db session routes
    cache config cookie encrypt
    hash
    router
    url log event

    View Slide

  30. Sandbox App Container
    Request
    Sandbox
    Reset Config
    Clear Instances
    Bind Request
    Rebind Router’s
    Container
    Reset Service Providers
    Redirect Container and
    Facades
    Dispatch

    View Slide

  31. Integrating Issues
    Http Kernel
    App
    Router
    Container
    Routes Collection
    Route
    Container
    Route
    Container

    View Slide

  32. Integrating Issues
    From pagination service provider

    View Slide

  33. Integrating Issues

    View Slide

  34. Integrating Issues

    View Slide

  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.

    View Slide

  36. Laravel Swoole
    940+ Stars
    on Github

    View Slide

  37. swooletw/laravel-swoole

    View Slide

  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).

    View Slide

  39. Connection Pool
    Database
    Worker
    Worker
    Worker
    Worker
    • Persistent Connections
    • Auto Reconnect

    View Slide

  40. Websocket in Laravel
    Broadcast
    Event
    Pub / Sub
    Publish
    Data
    Emit
    Broadcasted
    Data

    View Slide

  41. Websocket in Laravel Swoole
    Emit Data
    Parser Layer
    2probe 42["add user","test"]

    View Slide

  42. Websocket Server

    View Slide

  43. Websocket Server

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  48. Coroutine

    View Slide

  49. Coroutine
    hello go1
    hello man
    hello go2

    View Slide

  50. Coroutine Clients in Swoole
    • TCP / UDP Client
    • Http Client
    • Http2 Client
    • Redis Client
    • MySQL Client
    • PostgreSQL Client

    View Slide

  51. Connection Pool for Coroutine
    • Each pool maintains several connections.
    Connection Connection
    Connection Connection
    Connection Connection
    Connection Pool
    Worker Database

    View Slide

  52. Connection Pool for Coroutine
    Database
    Worker
    Worker
    Worker
    Worker
    Pool
    Pool
    Pool
    Pool
    • Each worker has its own connection pool.

    View Slide

  53. MySQL Coroutine
    Swoole Mysql Connector
    Eloquent ORM
    Swoole PDO
    Swoole PDO
    Statement

    View Slide

  54. MySQL Coroutine

    View Slide

  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

    View Slide

  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

    View Slide

  57. Q&A

    View Slide