【问题标题】:How to force Apache Bench to use IP addresses specified in /etc/hosts?如何强制 Apache Bench 使用 /etc/hosts 中指定的 IP 地址?
【发布时间】:2015-01-11 00:25:22
【问题描述】:

我正在使用 ApacheBench 进行一些负载测试。我希望 ab 使用我 Mac 上 /etc/hosts 中指定的 IP 地址解析主机名。我怎么能强迫它? curl 有一个 --resolve 选项可以做到这一点,正如 here 所指定的那样。我正在为 ab 寻找类似的东西。

【问题讨论】:

    标签: hosts apachebench hosts-file


    【解决方案1】:

    反过来想一想:您可以告诉 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 的答案,您要确保您使用正确的协议(httphttps)并达到正确的 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

    【讨论】:

    • 谢谢,这行得通! (我省略了 IP 地址中的尾随 /,所以它给了我一个错误,大喊“无效的 URL”,所以我花了一段时间才弄清楚错误是什么。)
    【解决方案2】:

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

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

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

    (ps 没有足够的积分,只能将其添加为评论)。

    【讨论】:

    • 谢谢,这是一个有用的评论。
    • 我必须注意,当我最近将我的 ab 结果与“真实”结果进行比较时,存在差异。对于实际结果,我指的是浏览器中显示的第一个字节的时间(f12> 网络工具)。对于某些网站,AB 能够达到 250 毫秒,而浏览器将显示 900 毫秒。所以这个方法还是不完美的。不过,AB 的值确实可以随着 CPU 功率的增加而很好地扩展。所以也不是没用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多