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

How Swoole Blows Up Your Mind

Albert Chen
December 20, 2022
4

How Swoole Blows Up Your Mind

PHPCon Okinawa 2019

Albert Chen

December 20, 2022
Tweet

Transcript

  1. How Swoole Blows Up Your Mind? @PHPCon Okinawa 2019 By

    Albert Chen
  2. About Me • Albert Chen • Software Architect • M17

    HandsUp • Open Source Maintainer
  3. What is Swoole?

  4. None
  5. What is Swoole? • A C extension for PHP •

    An asynchronous network engine for PHP • Features: • Event-driven non-blocking I/O • HTTP / HTTP2 / Websocket / TCP / UDP • Coroutine (CSP Model) • High performance for high concurrency
  6. Why PHP Performs Poorly in High Concurrency?

  7. Lifecycle in PHP

  8. Lifecycle in PHP PHP
 Files Tokenizing Semantic Parsing AST Generate

    Bytecode Execute Bytecode Output Load Bytecode From Memory Check Bytecode Cache
  9. Blocking I/O in PHP Request Request Request Request PHP FPM

    FPM Pool FPM Pool Worker Worker Worker Worker Blocking I/O
  10. Eventloop in NodeJS

  11. There’s no Eventloop in Traditional PHP

  12. Let’s See What Changes in Swoole?

  13. Structure in Swoole Server

  14. Structure in Swoole Server • Code Is Persistent in Memory

    • Swoole Takes Lifecycle Over PHP • Reduce Initialization Steps • Reduce Response Latency • Event-driven Non-blocking I/O for Requests
  15. Benchmark Comparison of Go with Swoole

  16. Server Benchmark in Go

  17. Server Benchmark in Go

  18. Server Benchmark in Swoole

  19. Server Benchmark in Swoole

  20. This Doesn’t Mean PHP with Swoole is Faster Than Go

    (Of Course Not)
  21. Coroutine in Swoole

  22. Coroutine in Swoole • Blocking I/Os Are Scheduled By Coroutine

    Automatically.
  23. Coroutine in Swoole • Blocking I/Os Are Scheduled By Coroutine

    Automatically.
  24. Coroutine in Swoole • CSP Model

  25. Does This Mean Every Blocking I/O Doesn’t Block Anymore?

  26. Coroutine in Swoole • How about This?

  27. Coroutine in Swoole • How about This?

  28. Runtime Hooks Transform Blocking I/Os into Coroutines with Magic!

  29. Use Case In HandsUp

  30. Use Case Facebook Webhooks Message Load Balancer Server Server Server

    Queue PubNub
 Service Analysis Service Swoole Service PubSub Many to Many Queue Workers High Concurrent Requests
  31. Use Case • Replace Guzzle Handler with Swoole Client

  32. Q&A