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

Swoole Love PHP

Swoole Love PHP

今天公司內訓的簡報摘錄。

研究 Swoole 多年,也確實在上線環境正式運行很久且穩定,所以與同事分享點滴與技巧。

除了一點範例程式外,分享著重的是【架構】差異,延伸出去再與 Nginx/PHP, Node 及 Go 進行一些比較。

主要是提出幾個問題,希望引發思考:

➊ 很多人都說 Swoole 快,是哪裡快?(其實我覺得網路上的資訊不夠全面,講得也不一定為真)

➋ 猜想 Nginx/PHP, Swoole, Node, Go 基礎效能比較?還能最佳化嗎?何處著手?

➌ 其他。

Yi-Feng Tzeng

April 26, 2018
Tweet

More Decks by Yi-Feng Tzeng

Other Decks in Technology

Transcript

  1. SWOOLE PHP
    Ant
    2018-03-30

    View Slide

  2. 2/39
    Introduction

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. 6/39
    Architecture

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  13. 13/39
    Example

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  21. 21/39
    Performance

    View Slide

  22. 22/39
    Performance
    Demo

    View Slide

  23. 23/39
    Connection pool

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  29. 29/39
    Async

    View Slide

  30. 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();
    });
    });

    View Slide

  31. 31/39

    View Slide

  32. 32/39
    Memory leaking !!

    View Slide

  33. 33/39
    Memory leaking !!

    View Slide

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

    View Slide

  35. 35/39
    Coroutine

    View Slide

  36. 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();

    View Slide

  37. 37/39
    Why not Go(lang)

    View Slide

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

    View Slide

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

    View Slide