Http 为什么我的代码要花这么长时间才能返回结果?

Http 为什么我的代码要花这么长时间才能返回结果?,http,go,Http,Go,当运行此代码时,我必须等待10秒,等待s.位置打印,等待60秒以上,等待n.标题打印。这是什么原因造成的 有关如何对此进行故障排除的提示将很有帮助,即查看完成某些代码行需要多长时间。新的去,所以不知道如何确切地做到这一点 我已经确保我关闭了我的连接。因为我电脑上的其他东西都加载得很快,所以我不认为通过http.Get访问互联网应该很慢 package main import ( "encoding/xml" "fmt" "io/ioutil" "net/http

当运行此代码时,我必须等待10秒,等待s.位置打印,等待60秒以上,等待n.标题打印。这是什么原因造成的

有关如何对此进行故障排除的提示将很有帮助,即查看完成某些代码行需要多长时间。新的去,所以不知道如何确切地做到这一点

我已经确保我关闭了我的连接。因为我电脑上的其他东西都加载得很快,所以我不认为通过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)
}
我得到了输出,但我必须等待10秒的美国位置和60秒以上的n.标题

有关如何解决此问题的提示将非常有用


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


使用
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请求并获得一个响应。有没有办法对此进行优化?我只发出2个get请求,所以我担心在添加代码、更多http请求和扩展时会发生什么。我知道客户端速度很慢。应用程序总共发出12个请求。服务器的响应速度很慢。“所以可能”--衡量一下。负载测试他人的站点/服务是一种糟糕的形式,可能会阻止您的IP,这是有充分理由的。不要这样做。