Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

About Me { "data": { "human": { "name": "Albert Chen", "occupation": "Software Engineer", "company": "Unisharp Techonology", "website": "https://albert-chen.com", "interests": [ "traveling", "programming", "cooking" ], "age": 26 } } }

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

What is Swoole?

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Naming of Swoole + =

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Laravel is awesome But can it be even faster?

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

Why try to integrate with ?

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Swoole’s Model

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Integrating Solutions ① Only use Swoole like PHP-FPM.

Slide 18

Slide 18 text

Integrating Solutions ② Preload and share one Laravel application.

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Integrating Solutions

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

Integrating Issues ③ Reset necessary variables based on ②.

Slide 24

Slide 24 text

Integrating Issues From dingo package

Slide 25

Slide 25 text

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…

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Integrating Issues From pagination service provider

Slide 33

Slide 33 text

Integrating Issues

Slide 34

Slide 34 text

Integrating Issues

Slide 35

Slide 35 text

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.

Slide 36

Slide 36 text

Laravel Swoole 940+ Stars on Github

Slide 37

Slide 37 text

swooletw/laravel-swoole

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Websocket Server

Slide 43

Slide 43 text

Websocket Server

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

Coroutine

Slide 49

Slide 49 text

Coroutine hello go1 hello man hello go2

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

MySQL Coroutine

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Q&A