【问题标题】:WGET - HTTPS vs HTTP = HTTPS SlowerWGET - HTTPS 与 HTTP = HTTPS 较慢
【发布时间】:2015-04-07 06:49:22
【问题描述】:

我正在尝试使用 wget 进行一些测试,我注意到在 wget 中加载 HTTPS 页面比在同一服务器上加载 http 需要更多的时间。 这似乎与任何网络差异无关。在名称解析之前 wget 需要大约 5 秒的额外时间。 任何人都可以帮忙吗?我该如何克服呢?当我注意到这一点时,我正打算使用带有 -p 和 -H 选项的 wget 来评估网络性能。

xbian@xbian ~ $ wget -V
GNU Wget 1.13.4 built on linux-gnueabihf.

+digest +https +ipv6 +iri +large-file +nls -ntlm +opie +ssl/gnutls

Wgetrc:
    /etc/wgetrc (system)
Locale: /usr/share/locale
Compile: gcc -DHAVE_CONFIG_H -DSYSTEM_WGETRC="/etc/wgetrc"
    -DLOCALEDIR="/usr/share/locale" -I. -I../lib -I../lib
    -D_FORTIFY_SOURCE=2 -Iyes/include -g -O2 -fstack-protector
    --param=ssp-buffer-size=4 -Wformat -Werror=format-security
    -DNO_SSLv2 -D_FILE_OFFSET_BITS=64 -g -Wall
Link: gcc -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat
    -Werror=format-security -DNO_SSLv2 -D_FILE_OFFSET_BITS=64 -g -Wall
    -Wl,-z,relro -Lyes/lib -lgnutls -lgcrypt -lgpg-error -lz -lidn -lrt
    ftp-opie.o gnutls.o ../lib/libgnu.a

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://www.gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Originally written by Hrvoje Niksic <hniksic@xemacs.org>.
Please send bug reports and questions to <bug-wget@gnu.org>.
xbian@xbian ~ $ time wget -d -v --no-check-certificate --delete-after -4 http://www.google.pt 2>&1  | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
2015-02-07 01:10:57 Setting --verbose (verbose) to 1
2015-02-07 01:10:57 Setting --check-certificate (checkcertificate) to 0
2015-02-07 01:10:57 Setting --delete-after (deleteafter) to 1
2015-02-07 01:10:57 Setting --inet4-only (inet4only) to 1
2015-02-07 01:10:57 DEBUG output created by Wget 1.13.4 on linux-gnueabihf.
2015-02-07 01:10:57
2015-02-07 01:10:57 URI encoding = `UTF-8'
2015-02-07 01:10:57 --2015-02-07 01:10:57--  http://www.google.pt/
2015-02-07 01:10:57 Resolving www.google.pt (www.google.pt)... 213.30.5.52, 213.30.5.24, 213.30.5.18, ...
2015-02-07 01:10:57 Caching www.google.pt => 213.30.5.52 213.30.5.24 213.30.5.18 213.30.5.25 213.30.5.59 213.30.5.31 213.30.5.45 213.30.5.46 213.30.5.39 213.30.5.53 213.30.5.32 213.30.5.38
2015-02-07 01:10:57 Connecting to www.google.pt (www.google.pt)|213.30.5.52|:80... connected.
2015-02-07 01:10:57 Created socket 3.
2015-02-07 01:10:57 Releasing 0x003b8040 (new refcount 1).
2015-02-07 01:10:57
2015-02-07 01:10:57 ---request begin---
2015-02-07 01:10:57 GET / HTTP/1.1
2015-02-07 01:10:57 User-Agent: Wget/1.13.4 (linux-gnueabihf)
2015-02-07 01:10:57 Accept: */*
2015-02-07 01:10:57 Host: www.google.pt
2015-02-07 01:10:57 Connection: Keep-Alive
2015-02-07 01:10:57
2015-02-07 01:10:57 ---request end---
2015-02-07 01:10:58 HTTP request sent, awaiting response...
2015-02-07 01:10:58 ---response begin---
2015-02-07 01:10:58 HTTP/1.1 200 OK
2015-02-07 01:10:58 Date: Sat, 07 Feb 2015 01:10:58 GMT
2015-02-07 01:10:58 Expires: -1
2015-02-07 01:10:58 Cache-Control: private, max-age=0
2015-02-07 01:10:58 Content-Type: text/html; charset=ISO-8859-1
2015-02-07 01:10:58 Set-Cookie: PREF=ID=98608883e4031983:FF=0:TM=1423271458:LM=1423271458:S=BnwaLDxFbjCUyPnF; expires=Mon, 06-Feb-2017 01:10:58 GMT; path=/; domain=.google.pt
2015-02-07 01:10:58 Set-Cookie: NID=67=AkXpY2nJPDDcH7xKJkslxdCtflnhOZJiNwZdu4YBAIc2FnjIZIAYHzFuln5boxiOHq1WWBdbcTnLXwPqOrfxOxkLXtO2U5UAVBCU0nVcgyC61_YLZLXGR0Fmdi9M_fIp; expires=Sun, 09-Aug-2015 01:10:58 GMT; path=/; domain=.google.pt; HttpOnly
2015-02-07 01:10:58 P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
2015-02-07 01:10:58 Server: gws
2015-02-07 01:10:58 X-XSS-Protection: 1; mode=block
2015-02-07 01:10:58 X-Frame-Options: SAMEORIGIN
2015-02-07 01:10:58 Alternate-Protocol: 80:quic,p=0.02
2015-02-07 01:10:58 Accept-Ranges: none
2015-02-07 01:10:58 Vary: Accept-Encoding
2015-02-07 01:10:58 Transfer-Encoding: chunked
2015-02-07 01:10:58
2015-02-07 01:10:58 ---response end---
2015-02-07 01:10:58 200 OK
2015-02-07 01:10:58 cdm: 1 2 3 4 5 6 7 8
2015-02-07 01:10:58 Stored cookie google.pt -1 (ANY) / <permanent> <insecure> [expiry 2017-02-06 01:10:58] PREF ID=98608883e4031983:FF=0:TM=1423271458:LM=1423271458:S=BnwaLDxFbjCUyPnF
2015-02-07 01:10:58 cdm: 1 2 3 4 5 6 7 8
2015-02-07 01:10:58 Stored cookie google.pt -1 (ANY) / <permanent> <insecure> [expiry 2015-08-09 02:10:58] NID 67=AkXpY2nJPDDcH7xKJkslxdCtflnhOZJiNwZdu4YBAIc2FnjIZIAYHzFuln5boxiOHq1WWBdbcTnLXwPqOrfxOxkLXtO2U5UAVBCU0nVcgyC61_YLZLXGR0Fmdi9M_fIp
2015-02-07 01:10:58 Registered socket 3 for persistent reuse.
2015-02-07 01:10:58 URI content encoding = `ISO-8859-1'
2015-02-07 01:10:58 Length: unspecified [text/html]
2015-02-07 01:10:58 Saving to: `index.html'
2015-02-07 01:10:58
2015-02-07 01:10:58      0K .......... .......                                     17.6M=0.001s
2015-02-07 01:10:58
2015-02-07 01:10:58 2015-02-07 01:10:58 (17.6 MB/s) - `index.html' saved [18301]
2015-02-07 01:10:58
2015-02-07 01:10:58 Removing file due to --delete-after in main():
2015-02-07 01:10:58 Removing index.html.

real    0m0.350s
user    0m0.038s
sys     0m0.027s
xbian@xbian ~ $ time wget -d -v --no-check-certificate --delete-after -4 https://www.google.pt 2>&1  | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
2015-02-07 01:11:01 Setting --verbose (verbose) to 1
2015-02-07 01:11:01 Setting --check-certificate (checkcertificate) to 0
2015-02-07 01:11:01 Setting --delete-after (deleteafter) to 1
2015-02-07 01:11:01 Setting --inet4-only (inet4only) to 1
2015-02-07 01:11:01 DEBUG output created by Wget 1.13.4 on linux-gnueabihf.
2015-02-07 01:11:01
2015-02-07 01:11:01 URI encoding = `UTF-8'
2015-02-07 01:11:01 --2015-02-07 01:11:01--  https://www.google.pt/
2015-02-07 01:11:06 Resolving www.google.pt (www.google.pt)... 213.30.5.25, 213.30.5.53, 213.30.5.38, ...
2015-02-07 01:11:06 Caching www.google.pt => 213.30.5.25 213.30.5.53 213.30.5.38 213.30.5.32 213.30.5.24 213.30.5.46 213.30.5.39 213.30.5.18 213.30.5.52 213.30.5.31 213.30.5.59 213.30.5.45
2015-02-07 01:11:06 Connecting to www.google.pt (www.google.pt)|213.30.5.25|:443... connected.
2015-02-07 01:11:06 Created socket 4.
2015-02-07 01:11:06 Releasing 0x00b53d48 (new refcount 1).
2015-02-07 01:11:06
2015-02-07 01:11:06 ---request begin---
2015-02-07 01:11:06 GET / HTTP/1.1
2015-02-07 01:11:06 User-Agent: Wget/1.13.4 (linux-gnueabihf)
2015-02-07 01:11:06 Accept: */*
2015-02-07 01:11:06 Host: www.google.pt
2015-02-07 01:11:06 Connection: Keep-Alive
2015-02-07 01:11:06
2015-02-07 01:11:06 ---request end---
2015-02-07 01:11:06 HTTP request sent, awaiting response...
2015-02-07 01:11:06 ---response begin---
2015-02-07 01:11:06 HTTP/1.1 200 OK
2015-02-07 01:11:06 Date: Sat, 07 Feb 2015 01:11:06 GMT
2015-02-07 01:11:06 Expires: -1
2015-02-07 01:11:06 Cache-Control: private, max-age=0
2015-02-07 01:11:06 Content-Type: text/html; charset=ISO-8859-1
2015-02-07 01:11:06 Set-Cookie: PREF=ID=579b1dd2360c9122:FF=0:TM=1423271466:LM=1423271466:S=9zOSotidcZWjJfXX; expires=Mon, 06-Feb-2017 01:11:06 GMT; path=/; domain=.google.pt
2015-02-07 01:11:06 Set-Cookie: NID=67=Jetj6llJijt09db9ekqGS6cBo3DE0CDqfQkp9Sh8xtLyYnNGU5zHoMED0whNkToP_w6mk6-oLTSRVdYIDekUEZH02oBYQPQhHmhpQzENI08zGNg9Jxn4EkXTIVApLCAG; expires=Sun, 09-Aug-2015 01:11:06 GMT; path=/; domain=.google.pt; HttpOnly
2015-02-07 01:11:06 P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
2015-02-07 01:11:06 Server: gws
2015-02-07 01:11:06 X-XSS-Protection: 1; mode=block
2015-02-07 01:11:06 X-Frame-Options: SAMEORIGIN
2015-02-07 01:11:06 Accept-Ranges: none
2015-02-07 01:11:06 Vary: Accept-Encoding
2015-02-07 01:11:06 Transfer-Encoding: chunked
2015-02-07 01:11:06
2015-02-07 01:11:06 ---response end---
2015-02-07 01:11:06 200 OK
2015-02-07 01:11:06 cdm: 1 2 3 4 5 6 7 8
2015-02-07 01:11:06 Stored cookie google.pt -1 (ANY) / <permanent> <insecure> [expiry 2017-02-06 01:11:06] PREF ID=579b1dd2360c9122:FF=0:TM=1423271466:LM=1423271466:S=9zOSotidcZWjJfXX
2015-02-07 01:11:06 cdm: 1 2 3 4 5 6 7 8
2015-02-07 01:11:06 Stored cookie google.pt -1 (ANY) / <permanent> <insecure> [expiry 2015-08-09 02:11:06] NID 67=Jetj6llJijt09db9ekqGS6cBo3DE0CDqfQkp9Sh8xtLyYnNGU5zHoMED0whNkToP_w6mk6-oLTSRVdYIDekUEZH02oBYQPQhHmhpQzENI08zGNg9Jxn4EkXTIVApLCAG
2015-02-07 01:11:06 Registered socket 4 for persistent reuse.
2015-02-07 01:11:06 URI content encoding = `ISO-8859-1'
2015-02-07 01:11:06 Length: unspecified [text/html]
2015-02-07 01:11:06 Saving to: `index.html'
2015-02-07 01:11:06
2015-02-07 01:11:06      0K .......... .......                                      670K=0.03s
2015-02-07 01:11:06
2015-02-07 01:11:06 2015-02-07 01:11:06 (670 KB/s) - `index.html' saved [18319]
2015-02-07 01:11:06
2015-02-07 01:11:06 Removing file due to --delete-after in main():
2015-02-07 01:11:06 Removing index.html.

real    0m5.371s
user    0m4.083s
sys     0m0.280s

在 curl 中,差异似乎并没有那么大......

xbian@xbian ~ $ curl -V
curl 7.26.0 (arm-unknown-linux-gnueabihf) libcurl/7.26.0 OpenSSL/1.0.1e zlib/1.2.7 libidn/1.25 libssh2/1.4.2 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap pop3 pop3s rtmp rtsp scp sftp smtp smtps telnet tftp
Features: Debug GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP
xbian@xbian ~ $ time curl -s http:///www.google.pt > /dev/null

real    0m0.140s
user    0m0.056s
sys     0m0.034s
xbian@xbian ~ $ time curl -s https:///www.google.pt > /dev/null

real    0m0.294s
user    0m0.060s
sys     0m0.031s

【问题讨论】:

  • 为什么不直接使用 curl?
  • 因为我实际上想使用 -p 和 -H 选项。我不认为 curl 支持这一点。
  • 经过一些测试和与 wget 开发人员的讨论,我得出的结论是,这是由于 gnutls 库造成的。如果 wget 使用 openssl 编译,则行为更像 curl。

标签: linux ssl curl raspberry-pi wget


【解决方案1】:

设置 SSL/TSL 有一些开销,因为必须建立会话密钥,但这往往可以忽略不计,所以我怀疑这是否是真正的原因,但人们永远不知道。

【讨论】:

    【解决方案2】:

    我该如何克服这个问题?

    this,不是 GNU Wget 的问题。我尝试运行你的命令:

    $ time wget -d -v --no-check-certificate --delete-after -4 http://www.google.pt 2>&1  | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
    $ time wget -d -v --no-check-certificate --delete-after -4 https://www.google.pt 2>&1  | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
    

    每个运行它们大约 10 次。最终结果?正如预期的那样,由于 SSL/TSL 协商协议,时间差异很小。这符合我对 GNU Wget 行为的期望。那么为什么你会看到这么大的差异呢?

    让我们看看https 版本的输出:

    2015-02-07 01:11:01 Setting --verbose (verbose) to 1
    2015-02-07 01:11:01 Setting --check-certificate (checkcertificate) to 0
    2015-02-07 01:11:01 Setting --delete-after (deleteafter) to 1
    2015-02-07 01:11:01 Setting --inet4-only (inet4only) to 1
    2015-02-07 01:11:01 DEBUG output created by Wget 1.13.4 on linux-gnueabihf.
    2015-02-07 01:11:01
    2015-02-07 01:11:01 URI encoding = `UTF-8'
    2015-02-07 01:11:01 --2015-02-07 01:11:01--  https://www.google.pt/
    2015-02-07 01:11:06 Resolving www.google.pt (www.google.pt)... 213.30.5.25, 213.30.5.53, 213.30.5.38, ...
    2015-02-07 01:11:06 Caching www.google.pt => 213.30.5.25 213.30.5.53 213.30.5.38 213.30.5.32 213.30.5.24 213.30.5.46 213.30.5.39 213.30.5.18 213.30.5.52 213.30.5.31 213.30.5.59 213.30.5.45
    2015-02-07 01:11:06 Connecting to www.google.pt (www.google.pt)|213.30.5.25|:443... connected.
    2015-02-07 01:11:06 Created socket 4.
    2015-02-07 01:11:06 Releasing 0x00b53d48 (new refcount 1).
    2015-02-07 01:11:06
    2015-02-07 01:11:06 ---request begin---
    2015-02-07 01:11:06 GET / HTTP/1.1
    2015-02-07 01:11:06 User-Agent: Wget/1.13.4 (linux-gnueabihf)
    2015-02-07 01:11:06 Accept: */*
    2015-02-07 01:11:06 Host: www.google.pt
    2015-02-07 01:11:06 Connection: Keep-Alive
    2015-02-07 01:11:06
    2015-02-07 01:11:06 ---request end---
    

    在 Wget 发送第一个请求之前,我只考虑了输出。至此,大家声称引起时间剧增的SSL/TSL协商,甚至还没有开始。然而,如果你仔细观察,所花费的时间是 > 5 秒!

    因此,您注意到的这种行为绝对不是由使用 HTTPS 的开销引起的。那么,那是什么?再次,仔细查看输出。最长时间在哪几行之间过去了?

    2015-02-07 01:11:01 --2015-02-07 01:11:01--  https://www.google.pt/
    2015-02-07 01:11:06 Resolving www.google.pt (www.google.pt)... 213.30.5.25, 213.30.5.53, 213.30.5.38, ...
    

    这意味着 Wget 大约需要 5 秒才能从域名解析 IP 地址。然而,DNS 解析根本不是 Wget 处理的事情。 Wget 请求系统获取 IP 地址。这可以在文件host.c:329中看到:

    static void
    gethostbyname_with_timeout_callback (void *arg)
    {
      struct ghbnwt_context *ctx = (struct ghbnwt_context *)arg;
      ctx->hptr = gethostbyname (ctx->host_name);
    }
    

    因此,在您的案例中真正发生的是您的系统需要一些额外的时间来解析主机名。发生这种情况的原因多种多样。但是,您没有多次运行测试,而是爱上了 Hasty Generalization 并简单地假设 Wget 执行 HTTPS 的速度非常慢。

    【讨论】:

    • 问题是在出现“正在解决...”行之前花费了 5 秒(而不是在解决时)。此延迟始终为 5 秒。它似乎与名称解析之前的soem任务有关......
    • 当然。这是由编译器和库优化引起的。现代编译器会优化代码并稍微改变打印语句以提高性能。最重要的是,已知 printf() 可以缓冲整行的输出。由于 IP 地址与“正在解析...”打印在同一行,因此您完全有可能看不到输出,因为它尚未刷新到标准输出。 (准确地说是标准错误)
    • 我将把它添加到主机文件中以确保。但这仅适用于 https,并且 https 和 http 的名称解析没有什么不同。
    • 每次都会发生吗?你能重复测试并可靠地说在https上完成dns解析需要5秒吗?
    • 我刚刚用 hosts 文件中的名称进行了测试,仍然会发生 5 秒的额外延迟...
    【解决方案3】:

    我该如何克服这个问题?

    你不能。

    HTTP 和 HTTPS 的区别在于后者使用 SSL/TLS 来保护连接。 SSL/TLS 的开销很大:

    • 在启动时,客户端和服务器交换证书等,以便(至少)客户端可以验证服务器不是冒名顶替者。

      启动协商需要许多客户端服务器消息交换。如果 TCP/IP 级别的连接有明显的延迟,这将表现为明显的延迟。

    • 一旦建立连接,通过连接的数据在发送时被加密,在接收时被解密。


    我不认为 HTTPS 有任何实用的替代方案是您想安全地与常规的当前一代 Web 服务器通信。我认为它不会随着“下一代”HTTP 而改变。即 HTTP/2。

    要加快速度(HTTP/1.1 或 HTTP/2),您唯一能做的就是为多个 GET 重用“持久连接”。 SSL/TLS 协商仅在建立连接时发生。但是,持久连接在“单次”用例中无济于事。例如当您使用wgetcurl 获取一个文件时。

    【讨论】:

    • 实际上,即使使用 HTTP/1.1,您也可以使用持久连接并将同一连接重用于 HTTPS 上的多个请求。只有在创建新连接时才需要进行 SSL/TLS 协商。
    • @damir - 谢谢。 (实际上,我知道... :-))
    • 但似乎 5 秒的开销发生在连接开始之前(实际上在名称解析之前)......这有意义吗?
    • 可能。它实际上可能是 DNS 解析......结合基于 DNS 的负载平衡器。
    • 肯定是因为DNS解析。至于@husvar 关于名称解析之前发生的开销的查询,请看一下我刚刚在我的答案上留下的评论。
    【解决方案4】:

    在与 wget 开发人员进行了一些测试和讨论后,我得出的结论是,这是由于 gnutls 库造成的。如果 wget 使用 openssl 编译,则行为更像 curl。

    【讨论】:

      【解决方案5】:

      您的计算机可能正在尝试 IPv6 DNS 查找但由于配置不正确而失败。它在超时后回退到 IPv4,然后连接成功。如果这是问题所在,您需要修复 IPv6 配置或完全禁用 IPv6。

      要测试这个理论,请使用“ping6”尝试 ping 您尝试连接的主机。我的猜测是“ping6”会失败,而“ping”会成功。

      如何测试:

      greg@mycomputer:~$ ping6 www.google.pt
      PING www.google.pt(ord30s26-in-x03.1e100.net) 56 data bytes
      64 bytes from ord30s26-in-x03.1e100.net: icmp_seq=1 ttl=53 time=19.5 ms
      64 bytes from ord30s26-in-x03.1e100.net: icmp_seq=2 ttl=53 time=18.3 ms
      ^C
      --- www.google.pt ping statistics ---
      2 packets transmitted, 2 received, 0% packet loss, time 1001ms
      rtt min/avg/max/mdev = 18.342/18.970/19.599/0.643 ms
      greg@mycomputer:~$ ping www.google.pt
      PING www.google.pt (216.58.192.227) 56(84) bytes of data.
      64 bytes from ord30s26-in-f3.1e100.net (216.58.192.227): icmp_seq=1 ttl=54 time=19.0 ms
      64 bytes from ord30s26-in-f3.1e100.net (216.58.192.227): icmp_seq=2 ttl=54 time=18.3 ms
      

      【讨论】:

        猜你喜欢
        • 2010-11-30
        • 2011-03-09
        • 2010-09-10
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        • 2017-11-05
        • 1970-01-01
        • 2017-12-14
        相关资源
        最近更新 更多