<?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>DEV Community: emilioSp</title>
    <description>The latest articles on DEV Community by emilioSp (@emiliosp).</description>
    <link>https://dev.to/emiliosp</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%2F283711%2Fbd1cccc8-0a82-49be-9ca4-4732c9dbe1f3.jpeg</url>
      <title>DEV Community: emilioSp</title>
      <link>https://dev.to/emiliosp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emiliosp"/>
    <language>en</language>
    <item>
      <title>NodeJS vs Apache performance battle for the conquest of my ❤️ ⚔️</title>
      <dc:creator>emilioSp</dc:creator>
      <pubDate>Sun, 02 Feb 2020 16:43:23 +0000</pubDate>
      <link>https://dev.to/emiliosp/nodejs-vs-apache-performance-battle-for-the-conquest-of-my-5c4n</link>
      <guid>https://dev.to/emiliosp/nodejs-vs-apache-performance-battle-for-the-conquest-of-my-5c4n</guid>
      <description>&lt;p&gt;I declare it immediately, JavaScript is my favorite programming language. Along with JavaScript in the last three years, PHP has gained the second position in my heart, mainly thanks to my job. However, I currently use both of them at work: JavaScript within the NodeJS runtime and PHP within Apache webserver.&lt;/p&gt;

&lt;p&gt;IMHO Apache + PHP is simpler for a newbie programmer, NodeJS is a little harder due to its asynchronous architecture. In few words, Apache manages a request forking a process and assigning it to the request, NodeJS, on the other hand, has an event-driven architecture which allows it to be able to manage concurrent requests with a single main thread called the event loop. The event loop manages all the requests but delegates IO tasks to other threads. &lt;br&gt;
The asynchronous approach could be harder to understand and if not properly managed could generate a mess in the code (the famous callback hell). However, when you start to rule the event loop properly, NodeJS returns back great satisfaction. I advise you to have a look at the async/await syntax if you are not familiar with it.&lt;/p&gt;

&lt;p&gt;I have heard and read many times this sentence &lt;em&gt;NodeJS is super fast&lt;/em&gt;, so I have decided to test its performance, and I'll show you the results, comparing it with Apache + PHP stack.&lt;/p&gt;
&lt;h2&gt;
  
  
  Methodology
&lt;/h2&gt;

&lt;p&gt;The tests consist of a series of calls bombing, raising progressively the concurrency level in order to analyze the behavior of the server.&lt;/p&gt;

&lt;p&gt;The requests belong to two categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A simulated IO operation that takes 100ms to complete. For instance, it could be an execution of a query in the database or a call to a rest API. &lt;/li&gt;
&lt;li&gt;A CPU intensive task (compute the prime numbers from 0 to 5000 with a naive algorithm).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The KPI under observation are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The number of requests per second successfully managed.&lt;/li&gt;
&lt;li&gt;The time required to manage a request (mean).&lt;/li&gt;
&lt;li&gt;The longest time required to fulfill a request (worst case).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The environment for the test is made up of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Docker container with &lt;a href="https://httpd.apache.org/docs/2.4/programs/ab.html" rel="noopener noreferrer"&gt;Apache Benchmark&lt;/a&gt; software that represents the client.&lt;/li&gt;
&lt;li&gt;A Docker container with the NodeJS server (v12.14.0).&lt;/li&gt;
&lt;li&gt;A Docker container with the Apache (v2.4.41) + PHP (v7.3.13) server.&lt;/li&gt;
&lt;li&gt;All the Docker images are based on the &lt;a href="https://alpinelinux.org/" rel="noopener noreferrer"&gt;alpine v3.11&lt;/a&gt; Linux distribution.&lt;/li&gt;
&lt;li&gt;The Docker engine is v19.03.5.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Docker containers run on my laptop, a MacBook Pro (13-inch, 2019, Two Thunderbolt 3 ports) with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1,4 GHz Quad-Core Intel Core i5&lt;/li&gt;
&lt;li&gt;8 GB 2133 MHz LPDDR3&lt;/li&gt;
&lt;li&gt;SSD 256 GB&lt;/li&gt;
&lt;li&gt;macOS Catalina (v10.15.2)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Docker images use the official versions of NodeJS and Apache, no tuning is allowed.&lt;/p&gt;
&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Requests per second
&lt;/h4&gt;

&lt;p&gt;The NodeJS runtime is designed very well to manage requests which involved IO operation, thanks to its asynchronous model.&lt;br&gt;
In fact, NodeJS was able to increase the managed requests per second in accordance with the increasing of concurrency level.&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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FSimulated%2520IO%2520requests%2520per%2520second.svg" 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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FSimulated%2520IO%2520requests%2520per%2520second.svg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the CPU task, both technologies reached the maximum result with the minimum concurrency level.&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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FCPU%2520intensive%2520requests%2520per%2520second.svg" 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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FCPU%2520intensive%2520requests%2520per%2520second.svg"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Time per requests (mean)
&lt;/h4&gt;

&lt;p&gt;Here again, the asynchronous approach shows its strengths.&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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FSimulated%2520IO%2520time%2520per%2520request.svg" 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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FSimulated%2520IO%2520time%2520per%2520request.svg"&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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FCPU%2520intensive%2520time%2520per%2520request.svg" 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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FCPU%2520intensive%2520time%2520per%2520request.svg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The response time is almost the same with the lowest concurrency level, but with 250 concurrent requests, NodeJS was five times faster than Apache.&lt;/p&gt;
&lt;h4&gt;
  
  
  Worst case
&lt;/h4&gt;

&lt;p&gt;In this scenario is visible the maximum time waited by a user.&lt;br&gt;
Analyzing the results with the maximum concurrency level:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IO task: NodeJS -&amp;gt; 1.2 sec, Apache -&amp;gt; 7.8 sec&lt;/li&gt;
&lt;li&gt;CPU task: NodeJS -&amp;gt; 2.7 sec Apache -&amp;gt; 13.4 sec&lt;/li&gt;
&lt;/ul&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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FSimulated%2520IO%2520longest%2520request%2520time%2520%28worst%2520case%29.svg" 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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FSimulated%2520IO%2520longest%2520request%2520time%2520%28worst%2520case%29.svg"&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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FCPU%2520intensive%2520longest%2520request%2520time%2520%28worst%2520case%29.svg" 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%2Fraw.githubusercontent.com%2FemilioSp%2Fnode-vs-apache%2Fmaster%2Fresults%2Fgraphs%2FCPU%2520intensive%2520longest%2520request%2520time%2520%28worst%2520case%29.svg"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;...and the winner is...NodeJS.&lt;br&gt;
Yes, it's true, NodeJS is superfast! Now I have the proof.&lt;/p&gt;

&lt;p&gt;For code details have a look here:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/emilioSp" rel="noopener noreferrer"&gt;
        emilioSp
      &lt;/a&gt; / &lt;a href="https://github.com/emilioSp/node-vs-apache" rel="noopener noreferrer"&gt;
        node-vs-apache
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Performance test of a NodeJS server vs a PHP Apache server
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;node-vs-apache&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;A performance benchmark using &lt;a href="https://httpd.apache.org/docs/2.4/programs/ab.html" rel="nofollow noopener noreferrer"&gt;Apache Benchmark&lt;/a&gt;.
You need Docker engine to execute tests.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://dev.to/emiliosp/nodejs-vs-apache-performance-battle-for-the-conquest-of-my-5c4n" rel="nofollow"&gt;https://dev.to/emiliosp/nodejs-vs-apache-performance-battle-for-the-conquest-of-my-5c4n&lt;/a&gt;&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;NodeJS&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;yarn test-node-simulated-io
yarn test-node-cpu-intensive&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Apache + PHP&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;yarn test-php-simulated-io
yarn test-php-cpu-intensive&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/emiliosp" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/7b8f7343bfc6e3c65c7901846637b603fd812f1a5f768d8b0572558bde859eb9/68747470733a2f2f63646e2e6275796d6561636f666665652e636f6d2f627574746f6e732f76322f64656661756c742d79656c6c6f772e706e67" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/emilioSp/node-vs-apache" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


</description>
      <category>node</category>
      <category>apache</category>
      <category>performance</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
