The aim of this experiment is to compare the languages that I daily use with Rust to understand better its potential.

I build the same webserver in four languages (php, node, java and rust) and I exposed 3 apis:

  • “/” that replies with a json payload
  • “/html” that replies with a static html page
  • “/cpu” that replies after an intense backend job that calculate an approximation of PI using a pair of 100_000_000 cartesian points coordination (Ref.)

Is out of scope doing server optimization or research which language framework is the best for the purpose; this is just a briefly overview to discover what is possible to do with common framework used “AS-IS” from official website sources.

Execution environment

  • Ubuntu 20.04.5 LTS, 64 bits
  • Kernel: 5.15.0-53-generic
  • Processor: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    • 8 vCPU
  • 16 GB RAM
  • Docker version 20.10.3, build 48d30b5
  • Docker compose version 1.27.3, build 4092ae5d
  • Apache benchmark

Language details

In details programming languages used, docker images and frameworks

  • Java code runs under eclipse-temurin:16 docker image, built using spring boot 2.7.1 framework
  • Node code runs under node:18.12-alpine docker image, built using express 4.18.2 framework
  • Php code runs into php:8.1-apache docker image that uses httpd as webserver, built using codeigniter 3 framework
  • Rust code runs into rust:1.65 docker image, built using actix-web 4.2.1 framework

Graph legend

  • rps: request per second. Higher is better
  • cpu: max cpu usage in %. 800 means 8 cpu 100% usage. Lower is better
  • ram: max ram usage in MB. Lower is better
  • ms: milliseconds to answer. Lower is better
  • 1/ms: reverse ms. Higher is better

Test 1: JSON api high traffic simulation

10000 requests (1000 concurrent); are measured the request per seconds, CPU usage and RAM consumption

Test 2: HTML static homepage high traffic simulation

10000 requests (1000 concurrent); are measured the request per seconds, CPU usage and RAM consumption

For results correctness it is added the column “httpd” because serving a static html page using a php framework is unlikely and a best practice should be serve directly using the webserver.

Note: httpd is used “as I found” in the docker image (mpm_prefork). Results may differs using mpm event driven compilation or using nginx.

Test 3: intense CPU task

1 request hits API “/cpu” which calculate an approx of PI using 100_000_000 points; are measured the milliseconds to answer, 1/ms , CPU usage and RAM consumption

Test 4: concurrent intense CPU task

100 requests (10 concurrent) hits the API “/cpu”; are measured the request per seconds and CPU usage.

NB: PHP failed the test due to timeout.

Test 5: environment startup

It’s measured the CPU usage and RAM consuption to startup each docker environment.

Benchmarks results

Apache benchmarks results are downloadable. In the logs are included also experiments not shown in the graph above.

Personal notes

Rust it’s incredibly fast and surely needs to be study deeply due to it’s stunning performances and low memory consumption.

Rust achieves this result because it’s an operating system language that uses machine resources in a more efficient way; it does not have a garbage collector so memory is release immediately once it goes out of scope; it is also safe, so the programmer does not have the pain to free the memory manually like c and c++.

By the way I found some cons:

  • slow compile time (up to 8 minutes for a simple webserver with 3 apis)
  • steep learning curve, compare to high level programming languages (well, at least for me)

and lastly some test limitations:

  • node.js results in which CPU usage is low, could be improved doing a proper replica of the container.
  • httpd results could be improved using a different compile build.
  • codeigniter php framework could be not best for the scope
  • java springboot framework is heavy, could be used other lighter solutions
  • rust.. well.. I’m new in this

The aim of this test is not be an 100% scientific approach, calculating all the possible variables… it’s a briefly overview of the rust enormous potential over high level language solutions.

Cheers


1 Comment

Fr4ncx · 6 March 2023 at 4:26 pm

Next time use fastify to have better results in NodeJS 🙂

Leave a Reply

Avatar placeholder

Your email address will not be published.