原文链接 [Web Framework Test - Rust vs Go](https://calmops.com/golang/web-framework-test-rust-vs-go/)
## Summary
Go is better than Rust overall. Go's development experience is far better than Rust. Go's concurrency ability is better than Rust.
| | Gin(Go) | Rocket(Rust) |
| ----------------------------- | --------------------------- | ---------------------------------------------- |
| Build Time | instant or in seconds | around 1 minute |
| Memory Usage | initial:10M; after test:70M | initial:10M; after test: 100M |
| Request Processing | 100000 reqs in 6s | 100000 reqs in 15~30s |
| Deps Management | shared | no need to download again, not shared in disk, |
| Deps Size(Project Disk Usage) | 200M | ~1000M |
| QPS | 20,000 | 5,000 |
By the way, several other frameworks(Actix(not compiled)/Salvo) are also tested, the data is similar to the result of Rocket's.
## Test Environment
- OS: Linux/Ubuntu
- Hardware: 4 Core/ 8GB RAM / SSD
- Go Framework(Gin)
- Rust Framework(Rocket)
- Test Software: ab(apache)
## Source Code
### Gin(Go)
File `main.go`
```go
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// album represents data about a record album.
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}
// albums slice to seed record album data.
var albums = []album{
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
}
func main() {
router := gin.Default()
router.GET("/albums", getAlbums)
router.GET("/albums/:id", getAlbumByID)
router.POST("/albums", postAlbums)
router.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
router.Run("0.0.0.0:9090")
}
// getAlbums responds with the list of all albums as JSON.
func getAlbums(c *gin.Context) {
c.IndentedJSON(http.StatusOK, albums)
}
// postAlbums adds an album from JSON received in the request body.
func postAlbums(c *gin.Context) {
var newAlbum album
// Call BindJSON to bind the received JSON to
// newAlbum.
if err := c.BindJSON(&newAlbum); err != nil {
return
}
// Add the new album to the slice.
albums = append(albums, newAlbum)
c.IndentedJSON(http.StatusCreated, newAlbum)
}
// getAlbumByID locates the album whose ID value matches the id
// parameter sent by the client, then returns that album as a response.
func getAlbumByID(c *gin.Context) {
id := c.Param("id")
// Loop through the list of albums, looking for
// an album whose ID value matches the parameter.
for _, a := range albums {
if a.ID == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
}
```
### Rocket(Rust)
File `src/main.rs`
```rs
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
```
## Test Results
### Gin
#### Test 1
100000 reqs, concurrency 5000, completed in 5.282 seconds
```bash
root@host:~/go/src/github.com/user/test# ab -n 100000 -c 5000 http://localhost:9090/ping
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software:
Server Hostname: localhost
Server Port: 9090
Document Path: /ping
Document Length: 18 bytes
Concurrency Level: 5000
Time taken for tests: 5.282 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 14100000 bytes
HTML transferred: 1800000 bytes
Requests per second: 18931.16 [#/sec] (mean)
Time per request: 264.115 [ms] (mean)
Time per request: 0.053 [ms] (mean, across all concurrent requests)
Transfer rate: 2606.73 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 115 21.9 113 178
Processing: 49 143 29.5 139 254
Waiting: 1 103 25.1 97 206
Total: 129 258 28.8 255 361
Percentage of the requests served within a certain time (ms)
50% 255
66% 268
75% 276
80% 280
90% 300
95% 310
98% 316
99% 327
100% 361 (longest request)
```
#### Test 2
1000000 reqs, concurrency 5000, completed in 50.816 seconds
```bash
root@host:~/go/src/github.com/user/test# ab -n 1000000 -c 5000 http://localhost:9090/ping
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests
Server Software:
Server Hostname: localhost
Server Port: 9090
Document Path: /ping
Document Length: 18 bytes
Concurrency Level: 5000
Time taken for tests: 50.816 seconds
Complete requests: 1000000
Failed requests: 0
Total transferred: 141000000 bytes
HTML transferred: 18000000 bytes
Requests per second: 19678.74 [#/sec] (mean)
Time per request: 254.081 [ms] (mean)
Time per request: 0.051 [ms] (mean, across all concurrent requests)
Transfer rate: 2709.67 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 114 15.2 113 186
Processing: 47 140 22.9 135 257
Waiting: 0 99 19.7 95 224
Total: 124 253 18.3 249 369
Percentage of the requests served within a certain time (ms)
50% 249
66% 257
75% 264
80% 268
90% 279
95% 288
98% 299
99% 307
100% 369 (longest request)
```
### Rocket
#### Test 1
100000 reqs, concurrency 3000, completed in 20.218 seconds
```bash
root@host-2:~/hello# ab -n 100000 -c 3000 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
^C
Server Software: Rocket
Server Hostname: localhost
Server Port: 8000
Document Path: /
Document Length: 13 bytes
Concurrency Level: 3000
Time taken for tests: 20.218 seconds
Complete requests: 88307
Failed requests: 0
Total transferred: 21900136 bytes
HTML transferred: 1147991 bytes
Requests per second: 4367.66 [#/sec] (mean)
Time per request: 686.866 [ms] (mean)
Time per request: 0.229 [ms] (mean, across all concurrent requests)
Transfer rate: 1057.79 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 10.1 0 76
Processing: 17 672 76.2 681 735
Waiting: 1 672 76.2 680 735
Total: 54 674 67.7 681 735
Percentage of the requests served within a certain time (ms)
50% 681
66% 689
75% 695
80% 699
90% 707
95% 717
98% 725
99% 728
100% 735 (longest request)
root@host-2:~/hello# ab -n 100000 -c 5000 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: Rocket
Server Hostname: localhost
Server Port: 8000
Document Path: /
Document Length: 13 bytes
Concurrency Level: 5000
Time taken for tests: 23.299 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 24800000 bytes
HTML transferred: 1300000 bytes
Requests per second: 4291.95 [#/sec] (mean)
Time per request: 1164.973 [ms] (mean)
Time per request: 0.233 [ms] (mean, across all concurrent requests)
Transfer rate: 1039.46 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 27.2 0 166
Processing: 37 1129 145.4 1163 1259
Waiting: 3 1129 145.4 1163 1259
Total: 112 1135 124.2 1163 1263
Percentage of the requests served within a certain time (ms)
50% 1163
66% 1173
75% 1179
80% 1183
90% 1203
95% 1227
98% 1245
99% 1255
100% 1263 (longest request)
```
#### Test 2
1000000 reqs, concurrency 5000, completed in 231.292 seconds
```bash
root@host-2:~/hello# ab -n 1000000 -c 5000 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests
Server Software: Rocket
Server Hostname: localhost
Server Port: 8000
Document Path: /
Document Length: 13 bytes
Concurrency Level: 5000
Time taken for tests: 231.292 seconds
Complete requests: 1000000
Failed requests: 0
Total transferred: 248000000 bytes
HTML transferred: 13000000 bytes
Requests per second: 4323.55 [#/sec] (mean)
Time per request: 1156.458 [ms] (mean)
Time per request: 0.231 [ms] (mean, across all concurrent requests)
Transfer rate: 1047.11 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 6.8 0 129
Processing: 23 1153 55.6 1153 1241
Waiting: 1 1153 55.6 1153 1241
Total: 91 1153 50.3 1153 1241
Percentage of the requests served within a certain time (ms)
50% 1153
66% 1163
75% 1171
80% 1176
90% 1193
95% 1205
98% 1221
99% 1226
100% 1241 (longest request)
```
有疑问加站长微信联系(非本文作者))