gpt4 book ai didi

http - 为什么我的 Hello World go 服务器被 ApacheBench 压垮了?

转载 作者:数据小太阳 更新时间:2023-10-29 03:27:52 25 4
gpt4 key购买 nike

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

我不禁想知道我是否在某个地方遇到了某种操作系统限制?我怎么说?我该如何缓解?

最佳答案

简而言之,您的端口用完了。

osx 上默认的临时端口范围是 49152-65535,也就是只有 16,383 个端口。由于每个 ab 请求都是 http/1.0(在您的第一个示例中没有 keepalive),每个新请求都需要另一个端口。

当使用每个端口时,它会被放入队列中等待 tcp“最大段生命周期”,这在 osx 上配置为 15 秒。因此,如果您在 15 秒内使用超过 16,383 个端口,您将有效地在进一步的连接上受到操作系统的限制。根据哪个进程首先用完端口,您将从服务器收到连接错误,或者从 ab 挂起。

您可以通过使用支持 http/1.1 的负载生成器(如 wrk)或使用 keepalive (-k) 选项来缓解这种情况ab,以便根据工具的并发设置重用连接。

现在,您要进行基准测试的服务器代码做的太少了,以至于负载生成器和服务器本身一样受到负担,本地操作系统和网络堆栈可能做出了很好的贡献。如果您想对 HTTP 服务器进行基准测试,最好从不在同一台机器上运行的多个客户端做一些有意义的工作。

关于http - 为什么我的 Hello World go 服务器被 ApacheBench 压垮了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34636517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com