https://karnwong.me/posts/rss.xml

Hello World API Performance Benchmark (Go, Node, Python, Rust)

2024-09-20

Correction 2024-09-21: after using multi-stage build, Node image size dropped from 1.1GB to 130MB.


The first programming language I achieved proficiency was Python, so for the longest time I've been using it to do most stuff. Last year I picked up Go, and I had a blast with it. This month I picked up Rust for data/ml works, and so far I was very impressed.

It got me thinking - it's been said multiple times is Python is slower than compiled languages, and Node is very easy to use but uses a lot of memory. Since I can code in multiple languages, why not do a simple API benchmark? So here's the results.

Docker image size

It's a given that when you compile your code into a binary, it would be smaller than adding the whole runtime and dependencies into an image.

LanguageImage Size (MB)
Go13.4
Node130
Python146
Rust8.63

Resources utilization on startup

Once we deploy the APIs, this is how much resources they consume on startup.

LanguageCPU UtilizationMemory Utilization
Go11
Node156
Python235
Rust10*

*This is a round-down, which means it utilizes really low amount of RAM.

API Performance

Total Requests and Response Time

Given concurrent 5000 VUs for 120s, here is total requests you can make in a given duration, with response time.

LanguageTotal RequestsAVG RTMIN RTMEDIAN RTMAX RTP95 RT
Go898,131335.77ms1.78ms239.55ms33.38s914.37ms
Node*383,091823.61ms2.29ms393.98ms33.75s1.41s
Python*225,9291.42s3.15ms861.22ms35.37s1.98s
Rust901,038408.57ms2ms274.13ms40.77s901.35ms

*Container restarted during peak load


For total requests, we can see that Go and Rust have very similar performance, while being the most performant of all. And Node is faster than Python, but still significantly slower than Go/Rust.

total-requests.webp


But if we are talking about response time, it is very apparent that python is the least performant of all. Go and Rust is a tie.

response-time.webp

Peak Resources utilization

Turns out Node is the most resource hungry, and Rust uses slightly more CPU than Go.

LanguageCPU (Mi)Memory (MB)
Go41052.48
Node1000203.17
Python100067.01
Rust49245.49

peak-resources-all.webp

Zooming in on Go and Rust, we can see that Go uses slightly less CPU but more memory than Rust.

Conclusion

  • Python and Node take up the most image size.
  • Go and Rust consume the least amount of resources and have very similar performance. While the differences are almost negligible, Rust can serve more requests.
  • Node is faster than Python, but utilizes more resources.