In this article, we will learn how to build a simple REST API using Go, Python, and Node.js. We will also learn how to benchmark the performance of our API endpoints using Apache Benchmark tool.
- Create a new directory for our Go project:
mkdir go-rest-api
cd go-rest-api
- Create a new file named
main.goand add the following code:
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http. Request) {
w.Write([]byte("pong"))
})
http.ListenAndServe(":8080", nil)
}- Run the following command to build and run our Go project:
go run main.go
- Open a new terminal and run the following command to test our
/pingendpoint:
curl http://localhost:8080/ping
- The output should be:
pong
- Create a new directory for our Python project:
mkdir python-rest-api
cd python-rest-api
- Create a new file named
main.pyand add the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/ping", methods=["GET"])
def ping():
return "pong"
if __name__ == "__main__":
app.run()- Run the following command to install Flask:
pip install flask
- Run the following command to build and run our Python project:
python main.py
- Open a new terminal and run the following command to test our
/pingendpoint:
curl http://localhost:5000/ping
- The output should be:
pong
- Create a new directory for our Node.js project:
mkdir nodejs-rest-api
cd nodejs-rest-api
- Create a new file named
main.jsand add the following code:
const express = require("express");
const app = express();
app.get("/ping", (req, res) => {
res.send("pong");
});
app.listen(3000, () => {
console.log("Server is running on port 3000.");
});- Run the following command to install Express:
npm install express
- Run the following command to build and run our Node.js project:
node main.js
- Open a new terminal and run the following command to test our
/pingendpoint:
curl http://localhost:3000/ping
- The output should be:
pong
We conducted a benchmark test for our /ping endpoint using Apache Benchmark tool with the following specifications:
- Document Path:
/ping - Document Length: 4 bytes
- Concurrency Level: 10
- Total requests: 1,000,000
- Here are the results of our benchmark test:
These results indicate that our /ping endpoint is performing well under high load with a high number of concurrent requests. However, it is important to note that the performance may vary depending on the system specifications and network conditions.
| Framework | Concurrency Level | Time taken for tests (seconds) | Complete requests | Failed requests | Total transferred (bytes) | HTML transferred (bytes) | Requests per second (mean) | Time per request (mean) | Time per request (mean, across all concurrent requests) | Transfer rate (Kbytes/sec) |
|---|---|---|---|---|---|---|---|---|---|---|
| Go (net/http) | 10 | 40.917 | 1000000 | 0 | 120000000 | 4000000 | 24439.90 | 0.409 ms | 0.041 ms | 2864.05 |
| Python (Flask) | 10 | 1123.621 | 1000000 | 0 | 176000000 | 4000000 | 889.98 | 11.236 ms | 1.124 ms | 152.97 |
| Node.js (Express) | 10 | 166.324 | 1000000 | 0 | 202000000 | 4000000 | 6012.35 | 1.663 ms | 0.166 ms | 1186.03 |
- NOTE: The results may vary depending on the system specifications and network conditions.
In this article, we have learned how to build a simple REST API using Go, Python, and Node.js. We have also learned how to benchmark the performance of our API endpoints using Apache Benchmark tool. We hope that this article will help you to build your own REST API.