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 |
2 環境準備
- 2臺2C4G的雲主機(172.16.60.211,172.16.60.210),這個自己到阿里雲上購買即可。一小時0.8元左右。
- gin的helloworld程式碼:https://github.com/qinxiongzhou/gin-vs-springboot/tree/main/springboot
- springboot的helloworld程式碼:https://github.com/qinxiongzhou/gin-vs-springboot/tree/main/gin/src/http_gin
- 172.16.60.211機器上,上次gin和springboot編譯好的包,並啟動執行。gin執行在8080埠,springboot執行在8090埠
- 172.16.60.210機器上,安裝AB 工具包,做壓測控制
3 程式碼工程及打包
3.1 gin
關鍵程式碼:
1 func main() { 2 gin.SetMode(gin.ReleaseMode) 3 gin.DefaultWriter = ioutil.Discard 4 r := gin.Default() 5 r.GET("/ping", func(c *gin.Context) { 6 c.JSON(200, gin.H{ 7 "message": "pong", 8 }) 9 }) 10 r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") 11 }
打包:
1 set GOOS=linux #windows環境需要設定GOOS,才能build成linux環境的可執行二進位制檔案 2 go build http_gin.go
上傳linux環境:
1 修改成可執行檔案 chmod +x http_gin 2 執行 ./http_gin &
3.2 springboot
關鍵程式碼:
1 @RestController 2 public class DemoController { 3 Result result = new Result("pong"); 4 5 @RequestMapping("/ping") 6 public Result hello(){ 7 return result; 8 } 9 } 10 11 12 class Result{ 13 String Message; 14 public String getMessage() { 15 return Message; 16 } 17 public void setMessage(String message) { 18 Message = message; 19 } 20 public Result(String message) { 21 Message = message; 22 } 23 }
編譯上傳:
1 maven編譯 :mvn install
執行:
1 java -jar demo-0.0.1-SNAPSHOT.jar &
4 benchmark
模擬20個使用者,發出20萬個請求
1 ab -c 20 -n 200000 http://172.16.60.211:8080/ping
4.1 gin benchmark
1 ab -c 20 -n 200000 http://172.16.60.211:8080/ping
benchmark結果:
1 Concurrency Level: 20 2 Time taken for tests: 13.423 seconds 3 Complete requests: 200000 4 Failed requests: 0 5 Write errors: 0 6 Total transferred: 28200000 bytes 7 HTML transferred: 3600000 bytes 8 Requests per second: 14900.02 [#/sec] (mean) 9 Time per request: 1.342 [ms] (mean) 10 Time per request: 0.067 [ms] (mean, across all concurrent requests) 11 Transfer rate: 2051.66 [Kbytes/sec] received
benchmark過程中,伺服器CPU、記憶體狀態:
4.2 springboot benchmark
1 ab -c 10 -n 200000 http://172.16.60.211:8090/ping
1 Concurrency Level: 20 2 Time taken for tests: 17.336 seconds 3 Complete requests: 200000 4 Failed requests: 0 5 Write errors: 0 6 Total transferred: 24600000 bytes 7 HTML transferred: 3600000 bytes 8 Requests per second: 11536.65 [#/sec] (mean) 9 Time per request: 1.734 [ms] (mean) 10 Time per request: 0.087 [ms] (mean, across all concurrent requests) 11 Transfer rate: 1385.75 [Kbytes/sec] received
benchmark過程中,伺服器CPU、記憶體狀態:
4.3 對比
qps | CPU | 記憶體 | 包大小 | |
gin | 14900 | 150% | 0.4% | 9M |
springboot | 11536 | 143% | 12% | 24M |
結論:
- qps上,gin 比 springboot 高出1.3倍。別看只有1.3倍,如果公司現在有10000臺伺服器呢?
- CPU上,兩者持平
- 記憶體上,gin比springboot 小30倍。這個差距是真有點大。
- 包大小上,gin比springboot 小2.6倍。別看磁碟只是小了2.6倍,流水線持續部署時,磁碟大小和每次傳包的時間,也是相當可觀的節省
從這些硬指標看,gin有具備比springboot更多的優勢。但從社群看,springboot依然是個王者。springboot也做了webflow的支援,後續也可期待下這塊的發展。