8

我正在使用 ApacheBench 进行一些负载测试。我希望 ab 使用我 Mac 上 /etc/hosts 中指定的 IP 地址解析主机名。我怎么能强迫它?curl 有一个 --resolve 选项来执行此操作,如此处所指定。我正在为 ab 寻找类似的东西。

4

2 回答 2

12

反过来想一想:您可以告诉 Curl 命中一个 IP 地址,并指定Host标头以触发所需的域。

curl example.com
curl --header "Host: example.com" 93.184.216.34

同样的方法适用于ab使用-H标志,因此

ab -n 10 -c 10 http://example.com/
ab -n 10 -c 10 -H "Host: example.com" http://93.184.216.34/

希望这可以帮助。

编辑:

背负@Magistar 的答案,您要确保使用正确的协议(httpvs https)并达到正确的 FQD。也就是说,如果您的网站响应www.example.com但不响应example.com(没有www),请务必使用有效的网站。

要注意的另一件事是确保您没有对重定向进行负载测试。例如,ab针对yahoo.com(我不建议这样做)运行将不是一个好的测试,因为它会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.

于 2015-04-17T14:46:18.663 回答
3

对于基于 SSL 的网站,以防止获得 0 字节:

ab -n 10 -c 10 -H "Host: www.example.com" https://93.184.216.34/

诀窍是在主机名中包含子域 (www)。

(ps 没有足够的分数来添加它作为评论)。

于 2017-09-25T02:50:33.163 回答