<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dzmitry Kazbiarovich</title>
    <description>The latest articles on Forem by Dzmitry Kazbiarovich (@dimdev).</description>
    <link>https://forem.com/dimdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F995185%2F0f0d7802-2314-487d-8130-91225f0475c3.png</url>
      <title>Forem: Dzmitry Kazbiarovich</title>
      <link>https://forem.com/dimdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dimdev"/>
    <language>en</language>
    <item>
      <title>Performance benchmark of PHP runtimes</title>
      <dc:creator>Dzmitry Kazbiarovich</dc:creator>
      <pubDate>Wed, 17 Jan 2024 19:51:40 +0000</pubDate>
      <link>https://forem.com/dimdev/performance-benchmark-of-php-runtimes-2lmc</link>
      <guid>https://forem.com/dimdev/performance-benchmark-of-php-runtimes-2lmc</guid>
      <description>&lt;p&gt;There are some fairly new players in the PHP runtimes team. It’s interesting to understand how good they are in comparison to well-known and widely used.&lt;/p&gt;

&lt;p&gt;The main questions I wanted to get answers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How many requests per second the runtime can process?&lt;/li&gt;
&lt;li&gt;What’s the average response time each runtime can provide?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To have fair competition, all runtimes are given the same resources. Almost. If the runtime consists of only 1 container(ex. Nginx Unit), it is run with 1CPU and 1GB of RAM. If the runtime consists of 2 containers(ex. Nginx + PHP-FPM), each container is run with 1 CPU and 1GB of RAM.&lt;/p&gt;

&lt;h3&gt;
  
  
  The testing environment
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Each runtime is run in 1 or 2 Docker containers.&lt;/li&gt;
&lt;li&gt;Each container has 1 CPU and 1GB of RAM.&lt;/li&gt;
&lt;li&gt;The load testing tool is in the same Docker network&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Testing application.
&lt;/h3&gt;

&lt;p&gt;Let’s try something more complex than single-file Hello-World. So, we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://symfony.com/" rel="noopener noreferrer"&gt;Symfony 7&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;1 controller&lt;/li&gt;
&lt;li&gt;1 view&lt;/li&gt;
&lt;li&gt;no database or any other external service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Symfony provides the runtime component. According to the official doc it “decouples the bootstrapping logic from any global state to make sure the application can run with runtimes like”. It means, that you can develop the application using any runtime you like, but in production run the most performant. So, we can run the application not only with traditional servers, but with libraries like Swoole, AMPHP, and ReactPHP. Looking ahead, the last two were excluded from the competition.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Load testing tool
&lt;/h3&gt;

&lt;p&gt;K6 was used to run load tests. It was run 3 times for each runtime, with 10, 100, and 1000 concurrent connections within 30 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Runtimes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://httpd.apache.org/" rel="noopener noreferrer"&gt;Apache&lt;/a&gt;(prefork mode) + mod_php.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://httpd.apache.org/" rel="noopener noreferrer"&gt;Apache&lt;/a&gt;(event mode) + &lt;a href="https://php-fpm.org/" rel="noopener noreferrer"&gt;PHP-FPM&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://nginx.org/" rel="noopener noreferrer"&gt;Nginx&lt;/a&gt; + &lt;a href="https://php-fpm.org/" rel="noopener noreferrer"&gt;PHP-FPM&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://unit.nginx.org/" rel="noopener noreferrer"&gt;Nginx Unit&lt;/a&gt; application server.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://roadrunner.dev/[](url)" rel="noopener noreferrer"&gt;Roadrunner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://nginx.org/" rel="noopener noreferrer"&gt;Nginx&lt;/a&gt; + Roadrunner (fcgi mode)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://roadrunner.dev/" rel="noopener noreferrer"&gt;FrankenPHP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://frankenphp.dev/docs/worker/" rel="noopener noreferrer"&gt;FrankenPHP (worker mode)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/swoole/swoole-src" rel="noopener noreferrer"&gt;Swoole&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All runtimes are based on &lt;a href="https://hub.docker.com/_/php" rel="noopener noreferrer"&gt;official docker images&lt;/a&gt;. Volumes are not used, the code of the application is copied to the image during the build. PHP 8.3 is everywhere, except Nginx Unit. At the beginning of 2024 the &lt;a href="https://unit.nginx.org/installation/#docker-images" rel="noopener noreferrer"&gt;highest PHP version supported by Nginx Unit is 8.2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Unfortunately, I didn’t find the up-to-date versions of ReactPHP and AMPHP runtimes compatible with Symfony 7. PHPPM both GitHub and Dockerhub look abandoned.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The following PHP and Symfony settings are applied:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;php.ini-production (comes with official docker containers) is used&lt;/li&gt;
&lt;li&gt;Opcache enabled&lt;/li&gt;
&lt;li&gt;JIT enabled&lt;/li&gt;
&lt;li&gt;preload is configured according to Symfony best practices&lt;/li&gt;
&lt;li&gt;Composer autoloader is optimized&lt;/li&gt;
&lt;li&gt;Symfony is run in production mode&lt;/li&gt;
&lt;li&gt;Service container is dumped to a single file&lt;/li&gt;
&lt;li&gt;Symfony cache is warmed up during container build&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The application code, as well as container configs, can be found on &lt;a href="https://github.com/DimDev/php-runtimes-benchmark" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbee64wpjmmthvbdqo2x0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbee64wpjmmthvbdqo2x0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv9dcgz3sht9n4ceuvsqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv9dcgz3sht9n4ceuvsqh.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3b0azhii387a16g52vhe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3b0azhii387a16g52vhe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvqtrfdxwancocnntdliw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvqtrfdxwancocnntdliw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhg5j240rijk7pqlvzcfe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhg5j240rijk7pqlvzcfe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn9q1vn62ddi7t59t8inp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn9q1vn62ddi7t59t8inp.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;001_Apache+mod_php and 002_Apache + PHP-FPM showed almost the same results.&lt;/li&gt;
&lt;li&gt;003_Nginx+PHP-FPM is very close to 001_Apache+mod_php and 002_Apache + PHP-FPM when the workload is low (concurrency 10 and 100 ).&lt;/li&gt;
&lt;li&gt;003_Nginx+PHP-FPM was able to serve ~2 times more requests than Apache-based stacks when the concurrency is 1000&lt;/li&gt;
&lt;li&gt;004_Nginx Unit. First of all — WOW!!! It is ~3 times faster than the traditional and most popular 003_Nginx+PHP-FPM.&lt;/li&gt;
&lt;li&gt;I don’t see a big difference between 005_Roadrunner and 006_Nginx+Roadrunner.&lt;/li&gt;
&lt;li&gt;Nevertheless, Roadrunner-based stacks are more than ~2 times faster than 003_Nginx+PHP-FPM.&lt;/li&gt;
&lt;li&gt;007_FrankenPHP(non-worker mode) is pretty close to 003_Nginx+PHP-FPM from performance point of view&lt;/li&gt;
&lt;li&gt;008_FrankenPHP(worker mode). Double WOW!!! I didn’t believe from the first attempt and ran load tests 3 or 4 times. So, when sending 1000 concurrent requests FrankenPHP(worker mode) is &amp;gt;10 times faster than Nginx+PHP-FPM. Also, faced an issue. Couldn’t start the container with php-ini.production.&lt;/li&gt;
&lt;li&gt;009_swoole — also double WOW!. Almost the same speed as 008_FrankenPHP(wm). Extremely fast. But, keep in mind that your code should be adjusted to be run with Swoole. For Symfony we have a bundle, which extends symfony/runtime component.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Personal opinion.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;FrankenPHP — amazing job, the first candidate to become standard de facto in PHP world.&lt;/li&gt;
&lt;li&gt;Swoole — I want it to be out of the box (but disabled) PHP extension.&lt;/li&gt;
&lt;li&gt;Nginx Unit — my personal choice. Despite it doesn’t support HTTP2 and many other features traditional Nginx provides, it is super easy to configure, light, and fast.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  What’s next?
&lt;/h3&gt;

&lt;p&gt;It is worth trying the same tests with an application connected to the database.&lt;/p&gt;

&lt;p&gt;Try ReactPHP, AMPHP, and PHPPM-based runtimes. But before that, the appropriate packages need to be updated to work with Symfony 7.&lt;/p&gt;

&lt;p&gt;Run containers with more resources, let’s say 2–4 CPU and 4–8GB of RAM. It would be interesting to see how well the runtimes can scale.&lt;/p&gt;

</description>
      <category>php</category>
      <category>symfony</category>
      <category>performance</category>
      <category>nginx</category>
    </item>
  </channel>
</rss>
