Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between…

Rust and Golang Web APIs Performance Testing Results — Data points to try and evaluate project decisions!

In this post, I’ll be sharing an objective comparison for an API written identically in Golang, and Rust. Before I proceed, I would suggest you to please read the following two articles as this one continues upon these two.

In the first one, I’ve described theoretically how to choose between Rust and Golang for any Web API Development Project.

Golang vs Rust for Web API Development Projects: A quick comparison

Following that, I looked at implementing a simple REST API using Rust to get a baseline for my comparison. My assumption is that Rust will be faster than go in terms of compute performance and that goes without saying.

Go vs Rust Web API Performance Testing — Rust Baseline Part #1

Both the APIs were the same in terms of implementation. I even kept dynamic arrays instead of fixed allocation for some structs to keep the code as similar as possible.

Implementation Summary

In summary, I used a web Framework called Echo to write my API. And Dockerized the Deployment to ensure I can standardize the remote testing environment.

All machines and test setups were fresh and the same.

Load Testing Script

The testing script remains the same in this case. Below is the script for your reference. I’ll use this to load test our APIs with 3000 virtual users concurrently hitting our service and measure the performance.

import http from 'k6/http';export default function () {
const url = 'http://x.x.x.x:3000/experiments';
const payload = JSON.stringify({
"name":"new_home_page",
"variants":[
{
"name":"blue_button",
"allocation_percent":50.0
},
{
"name":"red_button",
"allocation_percent":50.0
}
]
}
);const params = {
headers: {
'Content-Type': 'application/json',
},
};http.post(url, payload, params);
}

Test Command

k6 run --vus 3000 --iterations 1000000 script.js

Results — Scenario #1

The first scenario is a basic API call without any additional CPU Computation. An interesting observation with Golang was that the peak CPU hit 164% for this one. While it was 85% for when I ran the same test using Rust. The good thing is that the requests didn’t fail at this point as well. So, in terms of a consumer using these APIs, there were no request failures.

https://medium.com/media/16f4a27f4352c4b1c4e78392c288a59d/href

Results Scenario #2

In this scenario, I updated the code to add computation of MD5 hash based off experiment name and current Unix timestamp in seconds. And then update the name field with this new hash. The goal was to see if there was any notable change due to a secure hash. This was mostly for my curiosity.

CPU Peaked at 180% — 190% for this one too. I’m assuming GCP has a way of reporting over 100% CPUs for e2-medium instances. Still need to research on this. In the case of Rust, this was still around 100% CPU.

Below are the detailed results.

https://medium.com/media/2371ac42df9a1f0fcb359f241b0b5976/href

Conclusion

While I’m still doing some research on the CPU usage, the results for both the tests don’t show a considerable difference. Rust Does have an improved RPS overall, but I don’t think it’s considerable enough for this setup.

+----------+----------+----------+----------+----------+------+
| Scenario | Latency | Latency | Latency | Requests | CPU |
| #1 | (P95) | (P90) | (P50) | /Sec | Peak |
+----------+----------+----------+----------+----------+------+
| Rust | 821.16ms | 580.66ms | 253.49ms | 9254 | 85% |
+----------+----------+----------+----------+----------+------+
| Golang | 828.11ms | 585.68ms | 257.11ms | 9115 | 160% |
+----------+----------+----------+----------+----------+------+

Disclaimer — This test was run on a Simple API without a lot of code. I’m quite sure that the performance would vary as I build upon this application, and we have more Memory and CPU computations.

I can say one thing for sure. Unless I’m building a complex CPU Intensive / Memory efficient / highly secure platform as a service, I would go for Golang without thinking about it a lot. I love Rust and I do believe it is a general-purpose programming language to a great extent.

When we’re building backend systems, the requirements do tend to become complex over time, and so does the code complexity. Additionally, for the reasons mentioned in this post, I would choose Golang for all starter projects. As my application grows, and if I notice significant Spikes / resource impact for my application, I would plan for migrating those parts to Rust.

Please feel free to share your thoughts by commenting or writing a private note.

And please don’t forget to follow me for more posts like this 🙂


Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between… was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

Rust and Golang Web APIs Performance Testing Results — Data points to try and evaluate project decisions!

In this post, I’ll be sharing an objective comparison for an API written identically in Golang, and Rust. Before I proceed, I would suggest you to please read the following two articles as this one continues upon these two.

In the first one, I've described theoretically how to choose between Rust and Golang for any Web API Development Project.

Golang vs Rust for Web API Development Projects: A quick comparison

Following that, I looked at implementing a simple REST API using Rust to get a baseline for my comparison. My assumption is that Rust will be faster than go in terms of compute performance and that goes without saying.

Go vs Rust Web API Performance Testing — Rust Baseline Part #1

Both the APIs were the same in terms of implementation. I even kept dynamic arrays instead of fixed allocation for some structs to keep the code as similar as possible.

Implementation Summary

In summary, I used a web Framework called Echo to write my API. And Dockerized the Deployment to ensure I can standardize the remote testing environment.

All machines and test setups were fresh and the same.

Load Testing Script

The testing script remains the same in this case. Below is the script for your reference. I’ll use this to load test our APIs with 3000 virtual users concurrently hitting our service and measure the performance.

import http from 'k6/http';export default function () {
const url = 'http://x.x.x.x:3000/experiments';
const payload = JSON.stringify({
"name":"new_home_page",
"variants":[
{
"name":"blue_button",
"allocation_percent":50.0
},
{
"name":"red_button",
"allocation_percent":50.0
}
]
}
);const params = {
headers: {
'Content-Type': 'application/json',
},
};http.post(url, payload, params);
}

Test Command

k6 run --vus 3000 --iterations 1000000 script.js

Results — Scenario #1

The first scenario is a basic API call without any additional CPU Computation. An interesting observation with Golang was that the peak CPU hit 164% for this one. While it was 85% for when I ran the same test using Rust. The good thing is that the requests didn’t fail at this point as well. So, in terms of a consumer using these APIs, there were no request failures.

Results Scenario #2

In this scenario, I updated the code to add computation of MD5 hash based off experiment name and current Unix timestamp in seconds. And then update the name field with this new hash. The goal was to see if there was any notable change due to a secure hash. This was mostly for my curiosity.

CPU Peaked at 180% — 190% for this one too. I’m assuming GCP has a way of reporting over 100% CPUs for e2-medium instances. Still need to research on this. In the case of Rust, this was still around 100% CPU.

Below are the detailed results.

Conclusion

While I’m still doing some research on the CPU usage, the results for both the tests don’t show a considerable difference. Rust Does have an improved RPS overall, but I don’t think it’s considerable enough for this setup.

+----------+----------+----------+----------+----------+------+
| Scenario | Latency | Latency | Latency | Requests | CPU |
| #1 | (P95) | (P90) | (P50) | /Sec | Peak |
+----------+----------+----------+----------+----------+------+
| Rust | 821.16ms | 580.66ms | 253.49ms | 9254 | 85% |
+----------+----------+----------+----------+----------+------+
| Golang | 828.11ms | 585.68ms | 257.11ms | 9115 | 160% |
+----------+----------+----------+----------+----------+------+

Disclaimer — This test was run on a Simple API without a lot of code. I’m quite sure that the performance would vary as I build upon this application, and we have more Memory and CPU computations.

I can say one thing for sure. Unless I’m building a complex CPU Intensive / Memory efficient / highly secure platform as a service, I would go for Golang without thinking about it a lot. I love Rust and I do believe it is a general-purpose programming language to a great extent.

When we’re building backend systems, the requirements do tend to become complex over time, and so does the code complexity. Additionally, for the reasons mentioned in this post, I would choose Golang for all starter projects. As my application grows, and if I notice significant Spikes / resource impact for my application, I would plan for migrating those parts to Rust.

Please feel free to share your thoughts by commenting or writing a private note.

And please don’t forget to follow me for more posts like this 🙂


Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between… was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Shanmukh Sista | Sciencx (2024-03-29T10:28:11+00:00) » Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between…. Retrieved from https://www.scien.cx/2022/10/03/rust-and-golang-web-apis-performance-testing-results-objective-data-points-to-choose-between/.
MLA
" » Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between…." Shanmukh Sista | Sciencx - Monday October 3, 2022, https://www.scien.cx/2022/10/03/rust-and-golang-web-apis-performance-testing-results-objective-data-points-to-choose-between/
HARVARD
Shanmukh Sista | Sciencx Monday October 3, 2022 » Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between…., viewed 2024-03-29T10:28:11+00:00,<https://www.scien.cx/2022/10/03/rust-and-golang-web-apis-performance-testing-results-objective-data-points-to-choose-between/>
VANCOUVER
Shanmukh Sista | Sciencx - » Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between…. [Internet]. [Accessed 2024-03-29T10:28:11+00:00]. Available from: https://www.scien.cx/2022/10/03/rust-and-golang-web-apis-performance-testing-results-objective-data-points-to-choose-between/
CHICAGO
" » Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between…." Shanmukh Sista | Sciencx - Accessed 2024-03-29T10:28:11+00:00. https://www.scien.cx/2022/10/03/rust-and-golang-web-apis-performance-testing-results-objective-data-points-to-choose-between/
IEEE
" » Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between…." Shanmukh Sista | Sciencx [Online]. Available: https://www.scien.cx/2022/10/03/rust-and-golang-web-apis-performance-testing-results-objective-data-points-to-choose-between/. [Accessed: 2024-03-29T10:28:11+00:00]
rf:citation
» Rust and Golang Web APIs Performance Testing Results — Objective data points to choose between… | Shanmukh Sista | Sciencx | https://www.scien.cx/2022/10/03/rust-and-golang-web-apis-performance-testing-results-objective-data-points-to-choose-between/ | 2024-03-29T10:28:11+00:00
https://github.com/addpipe/simple-recorderjs-demo