我一直在用 http_load 测试一个用 Go 编写的简单网络服务器。当以 100 个并行运行测试 1 秒时,我看到 16k 个请求已完成。但是,运行 10 秒的测试会导致类似数量的请求以大约 1 秒测试速率的 1/10 完成。
此外,如果我同时运行多个 1 秒测试,第一个测试将完成 16k 个请求,而后续测试将仅完成 100-200 个请求。
package main
import "net/http"
func main() {
bytes := make([]byte, 1024)
for i := 0; i < len(bytes); i++ {
bytes[i] = 100
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(bytes)
})
http.ListenAndServe(":8000", nil)
}
我想知道在处理如此多的请求时性能会达到上限是否有任何原因,以及我在上述 Web 服务器的实现中是否遗漏了什么。
最佳答案
这可能是你自己系统的限制而不是go服务器的限制。如果您尝试使用 http_load 访问类似 google 的内容,则会发生相同类型的降级:
$> http_load -parallel 100 -seconds 10 google.txt
1000 fetches, 100 max parallel, 219000 bytes, in 10.0006 seconds
219 mean bytes/connection
99.9944 fetches/sec, 21898.8 bytes/sec
msecs/connect: 410.409 mean, 4584.36 max, 16.949 min
msecs/first-response: 279.595 mean, 3647.74 max, 35.539 min
HTTP response codes:
code 301 -- 1000
$> http_load -parallel 100 -seconds 50 google.txt
729 fetches, 100 max parallel, 159213 bytes, in 50.0008 seconds
218.399 mean bytes/connection
14.5798 fetches/sec, 3184.21 bytes/sec
msecs/connect: 1588.57 mean, 36192.6 max, 17.944 min
msecs/first-response: 237.376 mean, 33816.7 max, 33.092 min
2 bad byte counts
HTTP response codes:
code 301 -- 727
$> http_load -parallel 100 -seconds 100 google.txt
1091 fetches, 100 max parallel, 223161 bytes, in 100 seconds
204.547 mean bytes/connection
10.91 fetches/sec, 2231.61 bytes/sec
msecs/connect: 1652.16 mean, 35860.4 max, 17.825 min
msecs/first-response: 319.259 mean, 35482.1 max, 31.892 min
HTTP response codes:
code 301 -- 1019
如您所见,您点击 google 的时间越长,速度就会下降很多(google.txt 包含单个 url“http://google.com”)。这很可能是由于您的系统限制(您的程序可以拥有的最大打开连接数、内存、CPU 等...)。
关于go - 测试 Go 语言时性能下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16701966/