golang的coroutine好用,但是coroutine本身切换是需要有代价的,主要体现在内存栈的上下文切换。之前看过一个C++的go风格的库,叫libgo, 根据其github主页的benchmark,协程切换速率是要优于go的。但是这篇文章并非要和libgo比较,由于笔者之前接触过scylladb,对seastar这个c++框架念念不忘,下面就来对比一下两者差异
OS:Ubuntu 18.04
go: 1.12
seastar: 18.08 (非dpdk模式)https://github.com/miaomiao3/seastar (从seastar fork出来的可以编译通过的分支)
wrk: 4,1,0
CPU: Ryzen 1700 8core 16 thread
Mem: 16GB 2400MHz
http server单线程模式
- wrk 命令
wrk -t1 -c30 -d10 http://localhost:10000/
-t 用于指定线程数
-c 用于指定并发连接数
seastar: 使用例程apps/httpd, 指定-c 1参数
golang 测试代码:
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
"runtime"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func main() {
router := httprouter.New()
runtime.GOMAXPROCS(1)
router.GET("/", Index)
log.Fatal(http.ListenAndServe(":8082", router))
}
下面是qps比较:
wrk 并发连接 | 30 | 50 | 100 |
---|---|---|---|
seastar 1thread | 105716 | 107852 | 110265 |
golang 1thread | 55106 | 55235 | 53679 |
http server 2线程模式
- wrk 命令
wrk -t1 -c30 -d10 http://localhost:10000/
-t 用于指定线程数
-c 用于指定并发连接数
seastar: 使用例程apps/httpd, 指定-c 2参数
golang 测试代码:
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
"runtime"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func main() {
router := httprouter.New()
runtime.GOMAXPROCS(2)
router.GET("/", Index)
log.Fatal(http.ListenAndServe(":8082", router))
}
下面是qps比较:
wrk 并发连接 | 30 | 50 | 100 |
---|---|---|---|
seastar 2thread | 167313 | 170687 | 171913 |
golang 2thread | 92529 | 95436 | 92773 |
小结
上面只是一个简单的测试,足以看到seastar的威力,性能接近go的两倍。对于http api服务,seastar的性能基本无敌,所以阿里的x-deeplearning推荐框架用了seastar来做参数服务器的后端框架(ps: 但是x-deeplearning用的blaze推理后端框架是libevent, 这操作就有点昏了~~)但是开发效率不如go。go的性能虽然稍微差一点,但是胜在开发效率优于c++。由于实际开发的时候,网络栈的处理延时远小于rpc/dbquery的等待时间,从实用的角度来看,go其实还是很棒的。