Bench-marking RESTful APIs
Tags: benchmarks, python, go, node
Published: 2021-09-25 04:43:19

Are your RESTful services ready to handle a surge in traffic? One way to find out....

Do you know if your RESTful services will take the heat when your service/website gains multi-fold momentum in traffic?

>Dave works for an eCommerce start-up, one day, he was given a task to find out if the micro-services which he has been writing is scalable and optimized. The marketing team wants to run a big flash sale for a couple of days and expects a massive influx of online traffic to the website and the mobile app.

![Your friendly neighbourhood dev - Dave](https://gochronicles.com/content/images/2021/08/Tired-man-sleeping-on-laptop-keyboard-small.jpg)

>Dave's task is to figure out how services like order, checkout and other product-related services would perform in such situations.
In this article, we will load-test three REST API frameworks written in ***[NodeJS](https://github.com/nikhilakki/benchmark-rest-frameworks/tree/main/rest-express-nodejs)***, ***[Python](https://www.python.org/downloads/%20)*** & ***[Golang](https://golang.org/dl/)*** and, the benchmark will be using the Apache Benchmark tool.

The beauty of ***[micro-services](https://gochronicles.com/microservices/)*** is that different teams could write services backed by business logic in whichever programming language & Eco-system conducive to their use case.

### Our Rigg -
[Raspberry PI 3 Model B](https://en.wikipedia.org/wiki/Raspberry_Pi)

1. 1.2Ghz Quad-core Cortex A53
1. whopping 1GB RAM
1. 16GB Class 10 SD Card.

>*Well, you could run this benchmark on any compatible system. Benchmarks make sense as long as you test them on the same hardware environment.*

### Environment -
**Server machine [mighty rpi3] -**
  - OS - [Raspberry PI OS](https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-32-bit) on armv7l
  - Kernel - 5.10.52-v7+
  - Application runtimes -
      - [Python 3.8.9](https://www.python.org/downloads/%20)
      - [Node v14.17.4](https://nodejs.org/en/download/)
      - [Go v1.16.6](https://golang.org/dl/)

**Client machine [my laptop ;-)]-**

  - OS - [Fedora 34](https://getfedora.org/)
  - Kernel - 5.13.5-200.fc34.x86_64
  - [Ryzen 5500U (6 Cores & 8 GB DDR4 3200 MHz RAM)](https://www.amd.com/en/products/apu/amd-ryzen-5-5500u)
  - [Apache Benchmark tool (AB tool)](https://httpd.apache.org/docs/2.4/programs/ab.html)

### And the test -
```bash
ab -k -c 350 -n 5000 http://<rpi ip>:3000/
```
The above command sends 5k requests in total with a concurrency of 350 requests. One could tweak these numbers as per requirements.

The entire testing session ran for a few minutes and, we have the results in the below screenshots. We will start with FastAPI, followed by ExpressJS and then Fiber.

### FastAPI (Sync) - Python

![FastAPI Sync](https://gochronicles.com/content/images/2021/08/fastapi-bench-sync.png)

FastAPI in synchronous mode clocks in at ~178 requests per second.

### FastAPI (Async) - Python

![FastAPI Async](https://gochronicles.com/content/images/2021/08/fastapi-bench-async.png)

FastAPI in asynchronous mode clocks in at ~228 requests per second. One important thing to point out, that we are running FastAPI's ASGI server (uvicorn) with default settings (1 worker). If we tweak the worker count to the number of available CPUs, there should be a significant jump in the performance.

### Express JS (Sync) - NodeJS

![ExpressJS Sync](https://gochronicles.com/content/images/2021/08/express-bench-sync.png)

Express JS in the synchronous mode does 447 requests per second which are over ~2x jump from the FastAPI.

### Express JS (Async) - NodeJS

![ExpressJS Async](https://gochronicles.com/content/images/2021/08/express-bench-async.png)

A similar trend is apparent in asynchronous mode, ~509 requests per second. Again, we must note that FastAPI and Express JS apps are running on one worker by default. In both frameworks, we see a single-core performance.

### Now comes our star performer - Fiber (Golang)
**Fiber - 5k requests**

![fiber 5k req](https://gochronicles.com/content/images/2021/08/fiber-bench.png)

Fiber takes less than a second to process 5k requests! Since Fiber framework is implemented in Golang, ***[concurrency is handled out of the box](https://www.golangprograms.com/go-language/concurrency.html)*** which means there is no need to specify async await syntax in your code.

### CPU utilization by Fiber (Golang)

![htop fiber preview](https://gochronicles.com/content/images/2021/08/fiber-bench-500k-.png)
Now, you may be wondering Golang is using all CPU cores, but FastAPI and ExpressJS are utilizing only one CPU core per instance. Let's see how they perform when running on all CPU cores.

#### FastAPI Async with 4 workers on 4 Cores
![fastapi async multicore](https://gochronicles.com/content/images/2021/08/fastapi-4-workers-async.png)

#### ExpressJS Async with 4 workers on 4 Cores
![expressjs async multicore](https://gochronicles.com/content/images/2021/08/nodejs-4-workers.png)

ExpressJS is 1.5x faster than FastAPI. However, Fiber is still ~7.5 and ~11 times faster than ExpressJS and FastAPI.

### What do Sync and Async mean?

![async image - traffic on the road](https://gochronicles.com/content/images/2021/08/27265-small.jpg)

>Dave is also a champion chess player and, he has been challenged to play chess with not one but ten different players all at once! He has essentially two approaches

>**Approach 1- Dave decides to play one player at a time*** and, if the average time per game is 10minutes (assuming other players are making quick decisions like our champ!), even then it takes 10 mins X 10 players = at least 100 minutes (i.e. over 1.6 hours)

>**Approach 2 - Dave plays all players at once!** How is that possible? Every time an opponent takes time to think about their next move, Dave moves on to the next player and the next. It continues till the last move of the last remaining game.

>Now swap the crazy madness of a guy playing chess with multiple people with a [RESTful service](https://www.redhat.com/en/topics/api/what-is-a-rest-api). The opponents are external DB or service/APIs with which the RESTful service will communicate. Yes, that's how Sync & Async works and, since we are dealing with computers and not humans, they always perform actions as instructed!

### Our thoughts -
Well, it is going to be a long one and yet may not be complete. Let's get to it, shall we?

***[Fiber](https://docs.gofiber.io/)*** (Golang) is way ahead of the game in terms of speed and raw performance. ***[Express](https://expressjs.com/)*** (NodeJS) is ~1.5x faster than ***[FastAPI](https://fastapi.tiangolo.com/features/)*** (Python) and, ***these frameworks are ~7.5 and ~11.35 times slower than Fiber*** (as per multi-core async performance).

However, ***this doesn't mean that you no longer use Python or NodeJS based frameworks***. Factors like existing software stack, dev skills & experience in the team will play a significant role in making such decisions. ***Remember, good Go devs are hard to find compared to Python & JavaScript devs*** (at the time of writing this article) simply because these languages and their ecosystem have been here for longer.

>A few salient features FastAPI brings like Automatic data model documentation, JSON validations, serialization and more. One could achieve the same results in the other two frameworks by writing more code as they are minimalistic by design.

We have used FastAPI extensively. Absolute performance becomes relevant when you are doing things at scale. ***These benchmarks are solely in terms of raw speed/throughput and do not consider development speed, DB I/O operations, JSON serialization and de-serialization etc.***

A general recommendation is that you choose FastAPI when working with AI/ML-based services or, your team knows Python well. And same goes for NodeJS based framework decisions. There is no silver bullet here!

>If you are starting a new project and you are willing to give GoLang a try. Fiber is a great place to start and, ***[there are many frameworks to choose from](https://github.com/avelino/awesome-go#web-frameworks)***

We hope this gave you some perspective. Feel free to share comments and feedback. Let us know if we should do another post comparing full-feature frameworks from NodeJS & Golang eco-system such as ***[NestJS](https://docs.nestjs.com/first-steps)*** & ***[Go-buffalo.](https://gobuffalo.io/en/)***

### Source code -

[GitHub - gochronicles/benchmark-rest-frameworks: Benchmarking RESTful APIs written in Python, NodeJS and Golang](https://github.com/gochronicles/benchmark-rest-frameworks)

If you want to follow further development on this project, make sure you give the repo a star! :-)

*This article was originally posted on* ***[GoChronicles.com](https://gochronicles.com/benchmark-restful-apis/)*** *by the same author and has been repost with permission.*

**For latest updates & posts, [please subscribe to GoChronicles](https://gochronicles.com/membership/#/portal/signup/).**