Slide 1

Slide 1 text

SWOOLE PHP Ant 2018-03-30

Slide 2

Slide 2 text

2/39 Introduction

Slide 3

Slide 3 text

3/39 Swoole is a C extension for PHP Apache-2.0 + =

Slide 4

Slide 4 text

4/39 Ref: https://www.swoole.co.uk/

Slide 5

Slide 5 text

5/39 Ref: https://www.swoole.co.uk/

Slide 6

Slide 6 text

6/39 Architecture

Slide 7

Slide 7 text

7/39 Ref: https://www.yanshuo.me/p/11565 Apache / Nginx + PHP-FPM Swoole

Slide 8

Slide 8 text

8/39 Ref: https://www.spaceotechnologies.com/why-node-js-best-option-develop-chat-based-mobile-application/ Multi-threaded (Java) Single-thread / Event loop (Node)

Slide 9

Slide 9 text

9/39 Ref: http://slides.com/albertcht/swoole-redefine-php#/3/6 Multi-threaded (Java) Nginx with PHP-FPM

Slide 10

Slide 10 text

10/39 Ref: http://slides.com/albertcht/swoole-redefine-php#/3/6 Multi-threaded (Java) Nginx with PHP-FPM PHP-FPM is PHP FastCGI Process Manager

Slide 11

Slide 11 text

11/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ PHP Files Nginx with PHP-FPM

Slide 12

Slide 12 text

12/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ PHP Files Nginx with PHP-FPM Swoole PHP Files

Slide 13

Slide 13 text

13/39 Example

Slide 14

Slide 14 text

14/39 Swoole Installation Ref: https://www.swoole.co.uk/

Slide 15

Slide 15 text

15/39 Swoole HTTP Server Ref: https://www.swoole.co.uk/

Slide 16

Slide 16 text

16/39 Swoole WebSocket Server Ref: https://www.swoole.co.uk/

Slide 17

Slide 17 text

17/39 Swoole TCP Server Ref: https://www.swoole.co.uk/

Slide 18

Slide 18 text

18/39 Swoole TCP Client Ref: https://www.swoole.co.uk/

Slide 19

Slide 19 text

19/39 Swoole Async Ref: https://www.swoole.co.uk/

Slide 20

Slide 20 text

20/39 Ref: https://www.swoole.co.uk/ Swoole Task

Slide 21

Slide 21 text

21/39 Performance

Slide 22

Slide 22 text

22/39 Performance Demo

Slide 23

Slide 23 text

23/39 Connection pool

Slide 24

Slide 24 text

24/39 Ref: https://docs.oracle.com/cd/B28359_01/appdev.111/b28395/oci09adv.htm#LNOCI87728 Connection pool ➊ Reduces the number of times new connection objects are created. ➋ Promotes connection object reuse. ➌ Quickens the process of getting a connection. ➍ Minimizes the number of stale connections.

Slide 25

Slide 25 text

25/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ Swoole PHP Files

Slide 26

Slide 26 text

26/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ Swoole PHP Files DBconn DBconn

Slide 27

Slide 27 text

27/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ Swoole PHP Files DBconn Task DBconn

Slide 28

Slide 28 text

28/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ Swoole PHP Files DBconn Task vendor/autoload.php DBconn

Slide 29

Slide 29 text

29/39 Async

Slide 30

Slide 30 text

30/39 Ref: https://wiki.swoole.com/wiki/page/517.html Async MySQL Client $db = new swoole_mysql; $server = array( 'host' => '192.168.1.1', 'port' => 3306, 'user' => 'test', 'password' => 'test', 'database' => 'test', 'charset' => 'utf8', ); $db->connect($server, function ($db, $r) { if ($r === false) { var_dump($db->connect_errno, $db->connect_error); die; } $db->query('show tables', function(swoole_mysql $db, $r) { if ($r === false) { var_dump($db->error, $db->errno); } elseif ($r === true ) { var_dump($db->affected_rows, $db->insert_id); } var_dump($r); $db->close(); }); });

Slide 31

Slide 31 text

31/39

Slide 32

Slide 32 text

32/39 Memory leaking !!

Slide 33

Slide 33 text

33/39 Memory leaking !!

Slide 34

Slide 34 text

34/39 Ref: https://github.com/guzzle/guzzle/issues/1407

Slide 35

Slide 35 text

35/39 Coroutine

Slide 36

Slide 36 text

36/39 Ref: https://wiki.swoole.com/wiki/page/604.html Coroutine: HTTP Client $httpclient = new Swoole\Coroutine\Http\Client('127.0.0.1', 80); $httpclient->setHeaders(['Host' => "api.mp.qq.com"]); $httpclient->set([ 'timeout' => 1]); $httpclient->setDefer(); $httpclient->get('/'); // to do something $http_res = $httpclient->recv();

Slide 37

Slide 37 text

37/39 Why not Go(lang)

Slide 38

Slide 38 text

38/39 Why not Go(lang) ➊ Not most developers in company are familiar with Go(lang). ➋ Not so many Go(lang) developers in Taiwan (future?). ➌ Training time. ➍ Swoole performance is close to Go(lang) in some cases.

Slide 39

Slide 39 text

39/39 [email protected] https://www.facebook.com/yftzeng.tw https://twitter.com/yftzeng