Swoole in 5 Minutes [en]

Swoole in 5 Minutes [en]

Blazingly-Fast, Event-Driven, Asynchronous PHP. Berlin PHP Usergroup 02/06/2018

6e15b56fcb16fae0747810827be8ce8f?s=128

Christoph Lühr

February 06, 2018
Tweet

Transcript

  1. Christoph Lühr @chluehr / bephpug 2018 "Fast, faster, Swoole" Teaser:

    Swoole in 5 Minutes
  2. None
  3. SIN CE 2000

  4. None
  5. "Blazingly-Fast, Event-Driven, Asynchronous PHP."

  6. None
  7. $ pecl install swoole

  8. $ brew install swoole

  9. <?php $server = new \Swoole\Http\Server('0.0.0.0', 9000); $server->on('Request', function ($request, $response)

    { $response->end('OK'); }); $server->start();
  10. <?php $loop = React\EventLoop\Factory::create(); $server = stream_socket_server('tcp://0.0.0.0:9000'); stream_set_blocking($server, 0); $loop->addReadStream($server,

    function ($server) use ($loop) { $conn = stream_socket_accept($server); $data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n"; $loop->addWriteStream($conn, function ($conn) use (&$data, $loop) { $written = fwrite($conn, $data); if ($written === strlen($data)) { fclose($conn); $loop->removeStream($conn); } else { $data = substr($data, $written); } }); }); $loop->run();
  11. PHP-PM

  12. None
  13. Sugar and Cream? (PHP is not enough ...)

  14. Async IO

  15. File I/O (and some tagline to go along)

  16. TCP Clients MySQL / Redis / HTTP / WebSocket

  17. "Reality Distortion" BENCHMARKS

  18. 300.000 Req/Sec

  19. wrk -t4 -c400 -d10s http://127.0.0.1:1337/ Running 10s test @ http://127.0.0.1:1337/

    4 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 1.46ms 1.42ms 27.96ms 85.66% Req/Sec 75.29k 36.43k 183.55k 72.75% 3007806 requests in 10.06s, 605.25MB read Requests/sec: 299103.32 Transfer/sec: 60.19MB
  20. wrk -t16 -c400 -d30s http://localhost:9000/ Running 30s test @ http://localhost:9000/

    16 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 6.97ms 7.07ms 226.95ms 93.28% Req/Sec 4.00k 0.96k 15.36k 76.15% 1909179 requests in 30.09s, 282.21MB read Requests/sec: 63442.77 Transfer/sec: 9.38MB
  21. Demo

  22. Real World(tm) References?

  23. None
  24. None
  25. To good to be true...? DRAWBACKS

  26. 中文

  27. None
  28. Thanks! Questions? Christoph Lühr luehr@r-pentomino.de christoph.luehr@basilicom.de @chluehr Slides license Attribution-NonCommercial-ShareAlike

    3.0 http://creativecommons.org/licenses/by-nc-sa/3.0/
  29. • Swoole 2.0 https://www.swoole.co.uk/