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

Beyond Laravel Octane - Hyperf for Laravel Arti...

Albert Chen
September 28, 2024

Beyond Laravel Octane - Hyperf for Laravel Artisans

PHPCon Okinawa 2024

Albert Chen

September 28, 2024
Tweet

More Decks by Albert Chen

Other Decks in Programming

Transcript

  1. 02 Limitation of Laravel Octane Outline 01 Introduction to Laravel

    Octane 04 Introduction to Laravel Hyperf 05 Design Principles 07 Q&A 06 Live Demo of Laravel Hyperf 03 What is Hyperf framework?
  2. 🙍‍♂️ I’m Albert Chen 💻 Software Architect in Taiwan 🐘

    Laravel Artisan ⚡ Swoole Enthusiast 🧙 Hyperf Contributor 🧙 Creator of Laravel Swoole (archived) 🧙 Creator of Laravel Hyperf X: albert_cht Who am I?
  3. Published in April 2021 Maintained by official Laravel team Supports

    Swoole, Roadrunner and FrankenPHP as drivers Requires Laravel 10 and PHP 8.1 in 2.x Supercharge performance of Laravel Hot reload support Additional features in Swoole driver: Concurrent Tasks Ticks and Intervals The Octane Cache Swoole Tables What is Laravel Octane?
  4. What is Laravel Octane? More Details about Laravel Octane LaravelConf

    Taiwan 2021 Laracon US 2024 https://youtu.be/nSeZ7-dz2tk https://youtu.be/dOXXL-98Zbs
  5. Boost your Laravel application up to 5x faster in QPS

    With lower response latency What is Laravel Octane? Is Laravel Octane Always Fast? (Mac M1 Pro 2021, 8 CPU, 16G Ram)
  6. Consider building an OpenAI powered chatbot with Laravel Octane If

    each request takes about 3-5 seconds How many concurrent requests can Laravel Octane provide? What is Laravel Octane?
  7. Blocking I/O in PHP I/O model in PHP is blocking

    80% time cost on blocking I/O Workers can’t be reused during I/O
  8. Blocking I/O in PHP Almost all I/O functions in PHP

    are blocking That means the workers can’t be reused during I/O I/O bound concurrency depends on process number 100 concurrent requests need 100 processes Cost for context switch in processes is expensive QPS drops dramatically if I/O is slow Blocking I/O is extremely inefficient The only way to increase QPS is to scale your machines
  9. Coroutine in Laravel Octane Does Octane support coroutine with Swoole?

    No, it’s high risky! All components in Laravel are not designed for coroutine States among coroutines will be a mess in Laravel There’s no official announcement to support coroutine yet Need to consider 3rd packages and backward compatibility How about RoadRunner and FrankenPHP? Event if Laravel supports coroutines within single request , the worker is still occupied until the end of all I/O operations
  10. Hyperf is a high-performance framework built on Swoole With over

    6,000 stars on GitHub and more than 350 contributors since 2019 All components natively support coroutines, and strictly follow PSR standards (Some components in Laravel don’t) Developers can easily build high-concurrency applications with out-of-the-box support for non-blocking I/O. Strong community engagement and consistent development progress What is Hyperf?
  11. Laravel Hyperf is a Laravel style Hyperf framework To help

    Laravel artisans enjoy the high performance of Hyperf while maintaining familiar Laravel development practices Adopted same directory structure as Laravel Migrated bunch of packages from Laravel and support coroutine Reduce the learning cost for a new framework Share the same package ecosystem with Hyperf What is Laravel Hyperf? https://github.com/swooletw/laravel-hyperf
  12. What is Laravel Hyperf? Components migrated from Laravel Auth, Cache,

    Config, Container, Command, Database, Event, Facade, Router, Middleware, Log, Encryption, Hashing, Error Handling, Service Provider, Blade, Eloquent, Validation, String, Array, Collection, Translation, Testing etc Some components provide similar usage while keeping compatibility with Hyperf framework Requests, Responses, Cookies, Session
  13. What is Laravel Hyperf? Similar Artisan Commands list migrate serve

    tinker migrate:fresh|refresh|reset|rollback db:seed route:list schedule:run vendor:publish make:command|command|controller|event..., etc
  14. Coroutine in Laravel Hyperf Coroutine Supports Automated yield an resume

    while switching I/O states Runtime Hooks Channel (CSP Model) Defer WaitGroup Parallel Coroutine Context
  15. 1 2 2 Coroutine in Laravel Hyperf Global States Pollution

    in Coroutines Global states are shared in coroutines
  16. How to Migrate a Package? Design Principles Replace dependencies to

    Hyperf components Remove states from Laravel packages Keep compatibility with Hyperf if possible Package can be used in Hyperf as well Adapter for bridging components between Hyperf Share the same message protocol with Laravel
  17. How to Migrate a Package? Adapting PSR-15 middleware to Laravel

    middleware Laravel Middleware PSR-15 to Laravel Middleware Adpater Laravel Middleware Middleware Dispatcher
  18. How to Migrate a Package? Cache and locks can be

    shared between Laravel Cache Payload Laravel Cache Laravel Hyperf Cache Laravel Hyperf Lock Lock Payload Laravel Cache Same Communication Protocol
  19. Benchmark Comparison More than 10x faster than Laravel Octane in

    QPS Lower response latency (Mac M1 Pro 2021, 8 CPU, 16G Ram) Laravel Octane Laravel Hyperf Hello World Test
  20. 1s I/O Test Benchmark Comparison Laravel Hyperf is excellent at

    I/O requests with coroutines (Mac M1 Pro 2021, 8 CPU, 16G Ram) Laravel Octane Laravel Hyperf
  21. Q&A