在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 简要概述 最近看起go lang,真的被go的goroutine(协程)惊艳到了,一句 go function(){#todo},即可完成一个并发的工作。
看到gin这个web框架时,突然就特别想拿它和springboot来做个性能对比,马上撸一遍。
请求:/ping
返回:{"message":"pong"}
先透露下对比报告:
2 环境准备
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 对比
结论:
从这些硬指标看,gin有具备比springboot更多的优势。但从社区看,springboot依然是个王者。springboot也做了webflow的支持,后续也可期待下这块的发展。
|
请发表评论