http - 为什么我的代码需要这么长时间才能返回结果?

标签 http go

运行此代码时,我必须等待 10 秒才能打印 s.Locations 并等待 60 秒以上才能打印 n.Titles。这是什么原因造成的?

有关如何解决此问题的提示会很有帮助,即查看完成某些代码行需要多长时间。 Go 的新手,所以不确定如何准确地执行此操作。

我已确保关闭连接。由于我计算机上的其他所有内容加载速度都非常快,我不认为通过 http.Get 访问互联网应该很慢。

package main

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "net/http"
    "strings"
)

// SitemapIndex is the root xml
type SitemapIndex struct {
    Locations []string `xml:"sitemap>loc"`
}

// News is the individual categories
type News struct {
    Titles    []string `xml:"url>news>title"`
    Keywords  []string `xml:"url>news>keywords"`
    Locations []string `xml:"url>loc"`
}

// NewsMap is the
type NewsMap struct {
    Keywords string
    Location string
}

func main() {
    var s SitemapIndex
    var n News
    // np := make(map[string]NewsMap)
    resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
    bytes, _ := ioutil.ReadAll(resp.Body)
    xml.Unmarshal(bytes, &s)
    resp.Body.Close()

    for i := range s.Locations {
        s.Locations[i] = strings.TrimSpace(s.Locations[i])
    }

    fmt.Println(s.Locations) // slice of data

    for _, Location := range s.Locations {
        resp, _ := http.Get(Location)
        bytes, _ := ioutil.ReadAll(resp.Body)
        xml.Unmarshal(bytes, &n)
        resp.Body.Close()
    }

    fmt.Println(n.Titles)
}

我得到了输出,但我必须为 s.Locations 等待 10 秒,为 n.Titles 等待 60 秒以上

最佳答案

Tips on how to troubleshoot this would be helpful.


从简单的事情开始,一次测量一件事情,一个科学实验。


使用 curl 测量基本响应时间。

$ curl https://www.google.com/robots.txt -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7246    0  7246    0     0  94103      0 --:--:-- --:--:-- --:--:-- 94103
$ curl https://www.nytimes.com/robots.txt -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   934  100   934    0     0     61      0  0:00:15  0:00:15 --:--:--   230
$ curl https://www.washingtonpost.com/robots.txt -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3360  100  3360    0     0    133      0  0:00:25  0:00:25 --:--:--   869
$

谷歌没有延迟。纽约时报有 15 秒的延迟。华盛顿邮报有 25 秒的延迟。

在 Go 中,确认华盛顿邮报有 25 秒的延迟。

$ go run wapo.go
25.174366651s
$ cat wapo.go
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "time"
)

func main() {
    start := time.Now()
    resp, err := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
    _, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
    resp.Body.Close()
    fmt.Fprintln(os.Stderr, time.Since(start))
}
$

接下来,从另一台计算机尝试不同的 ISP。

$ curl https://www.google.com/robots.txt -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7246    0  7246    0     0  27343      0 --:--:-- --:--:-- --:--:-- 27343
$ curl https://www.nytimes.com/robots.txt -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   934  100   934    0     0   2017      0 --:--:-- --:--:-- --:--:--  2017
$ curl https://www.washingtonpost.com/robots.txt -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3360  100  3360    0     0    356      0  0:00:09  0:00:09 --:--:--   840
$ curl https://www.washingtonpost.com/news-sitemaps/index.xml -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1101  100  1101    0     0    104      0  0:00:10  0:00:10 --:--:--   266
$ 

$ go run wapo.go
8.378458882s
$ 

谷歌没有延迟。 《纽约时报》稍有延迟。华盛顿邮报有 9 秒的延迟。


Go 代码和编译器是一样的:

$ go version
go version devel +a25c2878c7 Sat Jul 27 23:29:18 2019 +0000 linux/amd64
$ cat wapo.go
package main

import (
    "fmt"
    "net/http"
    "os"
    "time"
)

func main() {
    start := time.Now()
    resp, err := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
    resp.Body.Close()
    fmt.Fprintln(os.Stderr, time.Since(start))
}
$

因此,关注网络和站点因素。


关于http - 为什么我的代码需要这么长时间才能返回结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246203/

相关文章:

go - 不能使用 ResponseErrorWriter 文字(类型 ResponseErrorWriter)作为 GO 中的类型错误

http - GWT RequestBuilder 将客户端日期设置为 HTTP 日期 POST header

带 cookie 的 iPhone NSData/NSUrl

node.js 检索 http csv 文件并加载到 mongoose

go - 如何设置 go-swagger 以从注释生成规范

Golang 接口(interface)的好处

java - 如何在单个帖子中发送文件和数据?

http - groupcache 的 peers 之间的通信

go - 从 QR 码编码计算 Reed-Solomon 纠错码字

regex - 有没有办法在正则表达式中插入变量?