package main
import (
"io"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!\n")
}
func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":8000", nil)
}
我有几个非常基本的HTTP服务器,并且所有这些服务器都出现了这个问题。
$ ab -c 1000 -n 10000 http://127.0.0.1:8000/
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
apr_socket_recv: Connection refused (61)
Total of 5112 requests completed
使用较小的并发值,事情仍然会出现问题。对我来说,问题似乎通常出现在5k-6k左右:
$ ab -c 10 -n 10000 http://127.0.0.1:8000/
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
apr_socket_recv: Operation timed out (60)
Total of 6277 requests completed
事实上,你可以完全放弃并发,问题仍然(有时)发生:
$ ab -c 1 -n 10000 http://127.0.0.1:8000/
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
apr_socket_recv: Operation timed out (60)
Total of 6278 requests completed
我无法帮助但想知道我是否在某处遇到某种操作系统限制?我怎么说?我将如何缓解?
答案 0 :(得分:35)
简而言之,您正在耗尽端口。
osx上的默认临时端口范围是49152-65535,它只有16,383个端口。由于每个ab
请求都是http/1.0
(在第一个示例中没有keepalive),因此每个新请求都需要另一个端口。
当使用每个端口时,它会被放入队列,等待tcp&#34;最大段寿命&#34;,在osx上配置为15秒。因此,如果您在15秒内使用超过16,383个端口,则在进一步连接时,您将有效地受到操作系统的限制。根据哪个进程首先用完了端口,您将从服务器获取连接错误,或者从ab
挂起。
您可以使用支持http/1.1
的负载生成器(如wrk
)或使用-k
的keepalive(ab
)选项来缓解此问题,以便重新连接基础关于工具的并发设置。
现在,您进行基准测试的服务器代码做得很少,负载生成器的负担与服务器本身一样多,本地操作系统和网络堆栈可能会做出很好的贡献。如果您想对http服务器进行基准测试,那么从不在同一台机器上运行的多个客户端做一些有意义的工作会更好。