我尝试在本地计算机上测量Go默认HTTP服务器实现的带宽。服务器仅接受任何HTTP请求,使用sync.atomic
递增计数器并发送200 OK
响应。此外,服务器每秒收集一次请求量,将其打印并将计数器重置为零:
type hand struct {
cnt int32
}
func (h *hand) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
atomic.AddInt32(&h.cnt, 1)
rsp.WriteHeader(200)
if req.Body != nil {
req.Body.Close()
}
}
func main() {
h := new(hand)
s := &http.Server{
Addr: ":8080",
Handler: h,
}
ticker := time.NewTicker(1 * time.Second)
go func() {
for tick := range ticker.C {
val := atomic.SwapInt32(&h.cnt, 0)
fmt.Printf("(%v) %d RPS\n", tick, val)
}
}()
log.Fatal(s.ListenAndServe())
}
目标客户端正在尝试同时发送100000
GET
请求:const total = 100000
func main() {
var r int32
var rsp int32
r = total
rsp = r
for r > 0 {
go func() {
p, err := http.Get("http://localhost:8080")
atomic.AddInt32(&rsp, -1)
if err != nil {
fmt.Printf("error: %s\n", err)
return
}
if p.StatusCode != 200 {
fmt.Printf("status %d\n", p.StatusCode)
}
}()
r--
}
for {
x := atomic.LoadInt32(&rsp)
fmt.Printf("sent : %d\n", total-x)
if x == 0 {
return
}
time.Sleep(1 * time.Second)
}
}
我正在使用具有
5.3.2-gentoo
内核的Linux机器。我将ulimits
的soft
(hard
和nofile
都)更改为100000
。当我运行此测试时,所有其他用户应用程序均已停止。我不希望得到准确的结果,但是只需要知道此阈值的水平即可,例如
X000
或X0000
或X00000
。但是服务器每秒不能处理超过
4000
请求,它看起来太低了:# removed timestamps
0 RPS
0 RPS
0 RPS
3953 RPS
3302 RPS
387 RPS
37 RPS
1712 RPS
如何提高HTTP服务器的带宽?还是我的测试方法或本地配置有问题?
最佳答案
问题出在测试方法上:
当我使用
wrk
在专用主机上测试此服务器时,它显示285900.73
RPS。
关于performance - 无法克服localhost HTTP Golang服务器上的4000 RS阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59333721/