Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Swoole Love PHP

2,621 views

Published on

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

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

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

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

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

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

➌ 其他。

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Swoole Love PHP

  1. 1. SWOOLE PHP Ant 2018-03-30
  2. 2. 2/39 Introduction
  3. 3. 3/39 Swoole is a C extension for PHP Apache-2.0 + =
  4. 4. 4/39 Ref: https://www.swoole.co.uk/
  5. 5. 5/39 Ref: https://www.swoole.co.uk/
  6. 6. 6/39 Architecture
  7. 7. 7/39 Ref: https://www.yanshuo.me/p/11565 Apache / Nginx + PHP-FPM Swoole
  8. 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)
  9. 9. 9/39 Ref: http://slides.com/albertcht/swoole-redefine-php#/3/6 Multi-threaded (Java) Nginx with PHP-FPM
  10. 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
  11. 11. 11/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ PHP Files Nginx with PHP-FPM
  12. 12. 12/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ PHP Files Nginx with PHP-FPM Swoole PHP Files
  13. 13. 13/39 Example
  14. 14. 14/39 Swoole Installation Ref: https://www.swoole.co.uk/
  15. 15. 15/39 Swoole HTTP Server Ref: https://www.swoole.co.uk/
  16. 16. 16/39 Swoole WebSocket Server Ref: https://www.swoole.co.uk/
  17. 17. 17/39 Swoole TCP Server Ref: https://www.swoole.co.uk/
  18. 18. 18/39 Swoole TCP Client Ref: https://www.swoole.co.uk/
  19. 19. 19/39 Swoole Async Ref: https://www.swoole.co.uk/
  20. 20. 20/39 Ref: https://www.swoole.co.uk/ Swoole Task
  21. 21. 21/39 Performance
  22. 22. 22/39 Performance Demo
  23. 23. 23/39 Connection pool
  24. 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.
  25. 25. 25/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ Swoole PHP Files
  26. 26. 26/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ Swoole PHP Files DBconnDBconn
  27. 27. 27/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ Swoole PHP Files DBconn TaskDBconn
  28. 28. 28/39 Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/ Swoole PHP Files DBconn Task vendor/autoload.php DBconn
  29. 29. 29/39 Async
  30. 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(); }); });
  31. 31. 31/39
  32. 32. 32/39 Memory leaking !!
  33. 33. 33/39 Memory leaking !!
  34. 34. 34/39 Ref: https://github.com/guzzle/guzzle/issues/1407
  35. 35. 35/39 Coroutine
  36. 36. 36/39 Ref: https://wiki.swoole.com/wiki/page/604.html Coroutine: HTTP Client $httpclient = new SwooleCoroutineHttpClient('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();
  37. 37. 37/39 Why not Go(lang)
  38. 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.
  39. 39. 39/39 yftzeng@gmail.com https://www.facebook.com/yftzeng.tw https://twitter.com/yftzeng

×