【发布时间】:2015-01-11 00:25:22
【问题描述】:
我正在使用 ApacheBench 进行一些负载测试。我希望 ab 使用我 Mac 上 /etc/hosts 中指定的 IP 地址解析主机名。我怎么能强迫它? curl 有一个 --resolve 选项可以做到这一点,正如 here 所指定的那样。我正在为 ab 寻找类似的东西。
【问题讨论】:
标签: hosts apachebench hosts-file
我正在使用 ApacheBench 进行一些负载测试。我希望 ab 使用我 Mac 上 /etc/hosts 中指定的 IP 地址解析主机名。我怎么能强迫它? curl 有一个 --resolve 选项可以做到这一点,正如 here 所指定的那样。我正在为 ab 寻找类似的东西。
【问题讨论】:
标签: hosts apachebench hosts-file
反过来想一想:您可以告诉 Curl 命中一个 IP 地址,并指定 Host 标头以触发所需的域。
curl example.com
curl --header "Host: example.com" 93.184.216.34
同样的方法适用于使用-H 标志的ab,因此
ab -n 10 -c 10 http://example.com/
ab -n 10 -c 10 -H "Host: example.com" http://93.184.216.34/
希望这会有所帮助。
编辑:
捎带@Magistar 的答案,您要确保您使用正确的协议(http 与 https)并达到正确的 FQD。也就是说,如果您的网站响应 www.example.com 而不是 example.com(没有 www),请务必使用有效的。
要注意的另一件事是确保您没有对重定向进行负载测试。例如,针对yahoo.com 运行ab(我不建议这样做)将不是一个好的测试,因为yahoo.com 会立即重定向到www.yahoo.com,您可以在curl 中查看它详细模式:
# curl yahoo.com
redirect
# curl yahoo.com -v
* About to connect() to yahoo.com port 80 (#0)
* Trying 98.139.180.180...
* Connected to yahoo.com (98.139.180.180) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: yahoo.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Wed, 07 Mar 2018 13:46:53 GMT
< Connection: keep-alive
< Via: http/1.1 media-router-fp29.prod.media.bf1.yahoo.com (ApacheTrafficServer [c s f ])
< Server: ATS
< Cache-Control: no-store, no-cache
< Content-Type: text/html
< Content-Language: en
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=2592000
< Location: https://www.yahoo.com/
< Content-Length: 8
<
* Connection #0 to host yahoo.com left intact
redirect
或者更具体地说,因为这是关于将主机欺骗到目标 IP 地址:
# curl --header "Host: yahoo.com" 98.139.180.180 -v
* About to connect() to 98.139.180.180 port 80 (#0)
* Trying 98.139.180.180...
* Connected to 98.139.180.180 (98.139.180.180) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Accept: */*
> Host: yahoo.com
>
< HTTP/1.1 301 Moved Permanently
< Date: Wed, 07 Mar 2018 13:51:26 GMT
< Connection: keep-alive
< Via: http/1.1 media-router-fp82.prod.media.bf1.yahoo.com (ApacheTrafficServer [c s f ])
< Server: ATS
< Cache-Control: no-store, no-cache
< Content-Type: text/html
< Content-Language: en
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=2592000
< Location: https://www.yahoo.com/
< Content-Length: 8
<
* Connection #0 to host 98.139.180.180 left intact
redirect
所以请务必先curl url,以确保它返回您期望的数据,然后继续ab。
【讨论】:
对于基于 SSL 的网站,防止获取 0 字节:
ab -n 10 -c 10 -H "Host: www.example.com" https://93.184.216.34/
诀窍是在主机名中包含子域 (www)。
(ps 没有足够的积分,只能将其添加为评论)。
【讨论】: