Swoole Love PHP

Swoole Love PHP

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

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

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

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

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

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

➌ 其他。

Ffe61c981651f09952d858fea7eaccd3?s=128

Yi-Feng Tzeng

April 26, 2018
Tweet

Transcript

  1. 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.
  2. 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(); }); });
  3. 31.
  4. 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();
  5. 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.