1 简要概述
最近接触到go lang,真的被go的goroutine(协程)惊艳到了,一句 go function(){#todo},即可完成一个并发的工作。
看到gin这个web框架时,突然就特别想拿它和springboot来做个性能对比,马上撸一遍。
请求:/ping
返回:{"message":"pong"}
先透露下对比报告:
qps | CPU | 内存 | 包大小 | 依赖 | |
---|---|---|---|---|---|
gin | 14900 | 150% | 0.4% | 9M | 无 |
springboot | 11536 | 143% | 12% | 24M | JVM |
2 环境准备
3 代码工程及打包
3.1 gin
关键代码:
func main() {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
打包:
上传linux环境:
3.2 springboot
关键代码:
@RestController
public class DemoController {
Result result = new Result("pong");
@RequestMapping("/ping")
public Result hello(){
return result;
}
}
class Result{
String Message;
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
public Result(String message) {
Message = message;
}
}
编译上传:
运行:
4 benchmark
模拟20个用户,发出20万个请求
ab -c 20 -n 200000 http://172.16.60.211:8080/ping
4.1 gin benchmark
ab -c 20 -n 200000 http://172.16.60.211:8080/ping
benchmark结果:
Concurrency Level: 20
Time taken for tests: 13.423 seconds
Complete requests: 200000
Failed requests: 0
Write errors: 0
Total transferred: 28200000 bytes
HTML transferred: 3600000 bytes
Requests per second: 14900.02 [#/sec] (mean)
Time per request: 1.342 [ms] (mean)
Time per request: 0.067 [ms] (mean, across all concurrent requests)
Transfer rate: 2051.66 [Kbytes/sec] received
benchmark过程中,服务器CPU、内存状态:
4.2 springboot benchmark
ab -c 10 -n 200000 http://172.16.60.211:8090/ping
Concurrency Level: 20
Time taken for tests: 17.336 seconds
Complete requests: 200000
Failed requests: 0
Write errors: 0
Total transferred: 24600000 bytes
HTML transferred: 3600000 bytes
Requests per second: 11536.65 [#/sec] (mean)
Time per request: 1.734 [ms] (mean)
Time per request: 0.087 [ms] (mean, across all concurrent requests)
Transfer rate: 1385.75 [Kbytes/sec] received
benchmark过程中,服务器CPU、内存状态:
4.3 对比
qps | CPU | 内存 | 包大小 | 依赖 | |
---|---|---|---|---|---|
gin | 14900 | 150% | 0.4% | 9M | 无 |
springboot | 11536 | 143% | 12% | 24M | JVM |
结论:
从这些硬指标看,gin有具备比springboot更多的优势。但从社区看,springboot依然是个王者。springboot也做了webflow的支持,后续也可期待下这块性能的突破。
Gin,看起来特别的不错,欢迎有兴趣的朋友一起讨论。